Search code examples
reactjsreact-beautiful-dnd

Remove Draggable from Parent while Maintaining Draggable Child in react-beautiful-dnd


I have columns with items in them. Currently, based on the code I copied from the react-beautiful-dnd, the columns and items are both draggable, and it's working well, but I don't want the columns to be draggable.

If I remove the <Draggable/> component, React gets upset about using a function as a child. I've tried a number of other things, without any luck. Any suggestions for how to remove the column drag functionality from this component?

// @flow
import React from 'react';
import PropTypes from 'prop-types';
import styled from 'react-emotion';
import { Draggable } from 'react-beautiful-dnd';
import { grid, colors, borderRadius } from './dnd/constants';
import PlayerList from './dnd/player-list';
import Title from './dnd/title';

const Container = styled('div')`
  margin: ${grid}px;
`;

const Header = styled('div')`
  display: flex;
  align-items: center;
  justify-content: center;
  border-top-left-radius: ${borderRadius}px;
  border-top-right-radius: ${borderRadius}px;
  background-color: ${({ isDragging }) => (isDragging ? colors.blue.lighter : colors.blue.light)};
  transition: background-color 0.1s ease;

  &:hover {
    background-color: ${colors.blue.lighter};
  }
`;

const Column = ({ title, players, index }) => (
  <Draggable draggableId={title} index={index}>
    {(provided, snapshot) => (
      <Container innerRef={provided.innerRef} {...provided.draggableProps}>
        <Header isDragging={snapshot.isDragging}>
          <Title
            isDragging={snapshot.isDragging}
            {...provided.dragHandleProps}
          >
            {title}
          </Title>
        </Header>
        <PlayerList
          listId={title}
          players={players}
        />
      </Container>
    )}
  </Draggable>
);

Column.propTypes = {
  title: PropTypes.string.isRequired,
  players: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
  index: PropTypes.number.isRequired,
};

export default Column;

Solution

    1. Remove the Draggable entirely from your Column
    const Column = ({ title, players, index }) => (
     <Container>
       <Header isDragging={false}>
         <Title isDragging={false}>
            {title}
         </Title>
      </Header>
      <PlayerList
         listId={title}
         players={players}
       />
    </Container>
    );
    
    1. You will probably want to remove the Droppable the wraps all of the Columns. This will most likely be in the Board component which you have not included here