-
Notifications
You must be signed in to change notification settings - Fork 986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: HOC to prevent drag from starting on given element #47
Comments
I ran into this problem too. I have a list of draggable items with inputs inside. Using the inputs is impossible since a click on an input is caught by SortableElement. |
The best workaround I've found so far is to directly attach DOM handlers on This prevents the event from bubbling to the container's handler that then
|
I think the best solution would be to add a I can work on a PR if needed. |
There are indeed a number of ways to approach this, between a I'm still debating which would be the most practical, though I think the HOC approach would become cumbersome if you have multiple different elements that need to be excluded. Would love to hear people's thoughts on the |
Just published Default implementation: function shouldCancelStart(e) {
// Cancel sorting if the event target is an `input`, `textarea`, `select` or `option`
if (['input', 'textarea', 'select', 'option'].indexOf(e.target.tagName.toLowerCase()) !== -1) {
return true; // Return true to cancel sorting
}
} |
Nice, will try to use this. I can't use the default implementation as-is, though, as my interactable element is a |
Should be pretty straightforward, let me know if you run into any issues. |
@clauderic after trying to actually adapt this to work for my use case, it does seem like a pass-through wrapping component would be best... That the element inside the A pass-through wrapper (something used like Supporting a // same as the default whitelist in 0.0.8
// eslint-disable-next-line react/sort-comp
static whitelist = new Set(['input', 'textarea', 'select', 'option'])
handleCancelStart (e) {
// default behavior
if (ClassName.whitelist.has(e.target.tagName.toLowerCase())) {
return true
}
for (let target = e.target; target !== this.contentDiv; target = target.parentElement) {
if (target.tagName.toLowerCase() === 'button') {
return true
}
}
} This requires saving an additional PS: A lot of the props given to a |
@Kovensky I'm not discarding the idea of a HOC to achieve this. As I mentioned, As for naming conventions, since this is not an event handler per-se, I don't see anything wrong with it. A function name should convey meaning, and I found
Agreed, a HOC would certainly hide this complexity, which is why it's not out of the cards. That said, looking at the code, it doesn't look all that complicated; that's fairly similar to how the SortableHandle is implemented, so ultimately it's just a matter of hiding complexity.
I disagree. All of those props are specific to each instance. Say you want to render two |
In my case the solution appeared to be much simpler than all that. |
…clauderic#36, clauderic#41) props. Prevent right click from causing sort start (clauderic#46)
The use case would be a non-draggable button nested inside a draggable list item.
Right now, the workarounds are to either attach native click events to the button (requiring a ref and extra lifecycle handlers), or to add a delay and hope the users click the button faster than the delay (but that reduces drag responsiveness). Otherwise, the dragging event takes precedence and blocks clicks to any clickable thing inside the SortableElement.
SortableHandle could also be a workaround, but that comes at the expense of being able to drag the SortableElement by dragging its background.
The text was updated successfully, but these errors were encountered: