Make draggable undraggable
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>
);
You can disable dragging of a Draggable by using the isDragDisabled prop.
https://github.com/atlassian/react-beautiful-dnd#props-1
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
Can you please post a link to an example of the behaviour you are talking about?
@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,
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
trueorfalse - 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?
interesting @jamesalexmorgan. Can you please raise a seperate issue for your idea?
I used the prop disabled not isDragDisabled.
It worked for me.
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 ?
Right now that is not possible @gregorybellencontre But it might be possible with #1239
isDragDisabled this is working for disable drag in react beautiful dnd yes this is working .
<Draggable index={i} isDragDisabled={true}>
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.
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.
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);
}
}
};