react-beautiful-dnd icon indicating copy to clipboard operation
react-beautiful-dnd copied to clipboard

Make draggable undraggable

Open nicubarbaros opened this issue 9 years ago • 13 comments

Is there a way to make draggable undraggable based on a boolean?

There is a boolean in dragHandleProps called draggable. My attempt is this however it is not working.

const { post } = this.props;
    return (
      <Draggable draggableId={post._id}>
        {(provided, snapshot) => (
          <div>
            <div
              ref={provided.innerRef}
              style={getItemStyle(provided.draggableStyle, snapshot.isDragging)}
              {...provided.dragHandleProps}
              draggable={post.draggable}
              role="presentation"
            >
              <div className="postchip-container">
                <div
                  className={classnames('post-status', {
                    'post-status-approved': post.approved
                  })}
                />
                adad
                {provided.dragHandleProps.draggable}
                <div className="post-container">
                  <div className="post-title">{post.plainText || post.mediaType}</div>
                </div>
              </div>
            </div>
            {provided.placeholder}
          </div>
        )}
      </Draggable>
    );

nicubarbaros avatar Nov 08 '17 16:11 nicubarbaros

You can disable dragging of a Draggable by using the isDragDisabled prop.

https://github.com/atlassian/react-beautiful-dnd#props-1

alexreardon avatar Nov 08 '17 21:11 alexreardon

Its not working. <Draggable draggableId={postId} isDragDisabled={published}> where published is a boolean false or true. This will disable drag to all of my divs. And yes the boolean comes true and false for the right posts.

My workaround is to rewrite the onClick event for the top div in draggable. But would love a clean way :D Thank you

nicubarbaros avatar Nov 17 '17 15:11 nicubarbaros

Can you please post a link to an example of the behaviour you are talking about?

alexreardon avatar Nov 17 '17 20:11 alexreardon

@nicubarbaros were you able to resolve the problem you were facing and do you have the example? I'm facing similar issue and not sure how to work it out. Thanks,

rfiroz avatar Oct 16 '18 15:10 rfiroz

Btw, Thanks so much for this library awesome Atlassian team!!! It's an absolute joy to work with!

@alexreardon Quick question!

The animation behaviour that the escape key triggers is perfect for me! I need to trigger that awesome animation, reverting their drag depending where they've tried to drop. If they dropped the item in an incorrect place in my list, it would be awesome for it to animate back to it's original position.

What could be awesome is a new hook, maybe called onBeforeDragEnd or onCheckCancel which could...

  • run just before drag end, (maybe immediately on mouse up, so you can check before you start animating)
  • expect a return value of true or false
  • If the function returns false, that would tell the DragDropContext to cancel and fire the cool escape reverting behaviour

This would let users write their own custom cancel logic! Yay! 🎉

Any thoughts?

jamesalexmorgan avatar Nov 07 '18 05:11 jamesalexmorgan

interesting @jamesalexmorgan. Can you please raise a seperate issue for your idea?

alexreardon avatar Nov 07 '18 08:11 alexreardon

I used the prop disabled not isDragDisabled. It worked for me.

jomelmartyguiao avatar Sep 10 '19 08:09 jomelmartyguiao

Hello I'm trying to avoid an element to be dropped at index 0. I used isDragDisabled to avoid the first element to be moved, but I still can move another element to the first place. Is it possible ?

gregorybellencontre avatar Nov 06 '19 11:11 gregorybellencontre

Right now that is not possible @gregorybellencontre But it might be possible with #1239

alexreardon avatar Nov 06 '19 22:11 alexreardon

isDragDisabled this is working for disable drag in react beautiful dnd yes this is working . <Draggable index={i} isDragDisabled={true}>

chatchathu199162 avatar Mar 29 '22 09:03 chatchathu199162

How to disable element movement in the list, when dropped another element in the same list?

e.g.: There are two columns, one with items and the other empty. In the first column there is an element that, when it dropped in the second column, should always take the first place in the list and not move when dropping other elements.

ayub-a avatar Mar 10 '23 14:03 ayub-a

How to disable element movement in the list, when dropped another element in the same list?

e.g.: There are two columns, one with items and the other empty. In the first column there is an element that, when it dropped in the second column, should always take the first place in the list and not move when dropping other elements.

You could handle that in your onDragEnd code. Just because the result says it wants to put something that was index=x into the index=y slot, doesn't mean that you have to. You can see if the special element is already in position 0, and if so, move anything trying to go to position 0 to position 1.

roroth avatar Apr 14 '23 18:04 roroth

I disabled moving items from 2 and 3 columns to 1 column something like this. You can have a look at this function on line with this check if ((start.id === 'in-progress' || start.id === 'done') && end.id === 'todo') {.

const handleDragEnd = ({ source, destination }) => {
    if (!destination) {
      return null;
    }

    if (source.droppableId === destination.droppableId && destination.index === source.index) {
      return null;
    }

    // Set start and end variables
    const start = columns[source.droppableId];
    const end = columns[destination.droppableId];

    if (start === end) {
      // Start by making a new list without the dragged item
      const newList = start.list.filter((_, idx) => idx !== source.index);

      // Then insert the item at the right location
      newList.splice(destination.index, 0, start.list[source.index]);

      // Update the store
      updateColumnTasks({ columnId: start.id, newList });
      return null;
    } else {
      try {
        // If start is different from end, we need to update multiple columns
        // Filter the start list like before
        const newStartList = start.list.filter((_, idx) => idx !== source.index);

        // Make a new end list array
        const newEndList = [...end.list];

        // Insert the item into the end list
        newEndList.splice(destination.index, 0, start.list[source.index]);

        // disable moving items from 2 and 3 columns to 1 column
        if ((start.id === 'in-progress' || start.id === 'done') && end.id === 'todo') {
          return null;
        }
        // Update the store
        updateMultiColumnTasks({
          startColId: start.id,
          endColId: end.id,
          startTasks: newStartList,
          endTasks: newEndList,
        });
      } catch (e) {
        // eslint-disable-next-line no-console
        console.error(e);
      }
    }
  };

And of course, you can clone this repository and see the implementation

dnd-todo repository

ArtemFedorchuk avatar Aug 30 '23 22:08 ArtemFedorchuk