Cannot prevent default keyDown handling and weird debugging issue
#666 😈
downshiftversion: 3.2.2nodeversion: codesandboxnpm(oryarn) version: codesandbox
Relevant code or config
Snippet 1
function stateReducer(state, changes) {
// If dropdown is about to get open and there is not highlighted index, highlight the first one.
if (
changes.hasOwnProperty("isOpen") &&
changes.isOpen &&
state.highlightedIndex === null &&
changes.type === Downshift.stateChangeTypes.clickButton
) {
return {
...changes,
highlightedIndex: 0
};
}
// FIXME: This one I had to add because after handling the previous change
// another one got dispatched immediately after with `changes = { highlightedIndex: null }`
if (
changes.hasOwnProperty("highlightedIndex") &&
changes.highlightedIndex === null &&
changes.type === Downshift.stateChangeTypes.clickButton
) {
return {
...changes,
highlightedIndex: 0
};
}
return changes;
}
Snippet 2
<button
className="toggle"
ref={this.dropdownRef}
{...downshiftProps.getToggleButtonProps({
onKeyDown: event => {
// FIXME: This is not preventing the menu to open.
if (event.keyCode === 13 && !downshiftProps.isOpen) {
event.preventDownshiftDefault = true;
}
}
})}
>
...
</button>
What you did:
Created a text dropdown with Downshift with 2 custom behaviors:
- When dropdown gets opened and Enter key is the pressed, first option should be selected.
- When dropdown is closed and Enter key is pressed (with focus on the dropdown) the surrounding form should be submitted (as if the form received the enter Key press)
What happened:
Implemented first custom behaviour with Snippet 1. The first statement should cover the functionality, but for some reason another set of changes is being dispatched immediately after I "correct" the next state. So I needed to patch another state change to highlight the first option.
Implemented second custom behaviour with Snippet 2. The Enter key handling should not be handled by the component in general, so I'm setting the property on the event for downshift to prevent default behaviour. The behaviour is only prevent it when I put a breakpoint in event.preventDownshiftDefault = true;. When I'm not debugging, the menu just opens up. (I would still need to check that the form is submitted even without downshift handling the keyDown)
Reproduction repository:
https://codesandbox.io/s/x3prz3l224
Problem description:
- Can't seem to prevent downshift from handling keyDown events
- Can't seem to override state change in a straightforward way
Hello. Let me answer to your points.
- From what I can see in your state change code, it seems that you can achieve that functionality by using
defaultHighlightedIndexprop and pass 0 to it :). Not sure why the reducer is called again, maybe something in your code triggering it, but just use that prop I suggested and remove that customstateReducer. - Right now I am not really familiar with preventing the Downshift event from happening, but from reading the docs, the syntax is a bit different from your example:
const ui = (
<Downshift>
{({getInputProps}) => (
<input
{...getInputProps({
onKeyDown: event => {
if (event.key === 'Enter') {
// Prevent Downshift's default 'Enter' behavior.
event.nativeEvent.preventDownshiftDefault = true
// your handler code
}
},
})}
/>
)}
</Downshift>
)
Try with the nativeEvent and see what happens. Let me know if both your problems get solved, thanks!
@silviuavram for 1, you are right, the defaultHightlightedIndex prop works as expected (I guess I had a typo when I tried it out 😢). Still, would be nice to understand how and why the stateReducer is being called.
As for 2, its the same, downshift code checks for both (neither is working for me): https://github.com/downshift-js/downshift/blob/f2b7b6f3de493352b2d006b10ca897588592cf67/src/utils.js#L93..L97
stateReduceris called in ourinternalSetStateand is used in case you want to change something before the state is set.- you still need to preventDefault on the
onClicksince hitting Enter on a button not only triggers aonKeyDown, but also aonClick. We love Javascript :)
@silviuavram
-
I understand that, but I can’t see why I’m receiving a null highlighted index in the second call (and no other change, with the same changes.type that it was called before)
-
I was afraid I would need to prevent the default given at the end it is a button click. The downside is that I can’t get the normal behavior of “submitting a form with enter key” that I would get with a submit button if the toggle is getting the click and preventing default :(.
What I would love is to have natural propagation of the event, as if it was not being handled by downshift or the component for that matter. That is the behavior you would get with a normal select (that is inside a form with a submit button) when receiving the Enter keypress.
Note: I could do 2 if React allowed to dispatch synthethic events. Just prevent default and dispatch to the appropriate component
-
at the moment, we have 2 state changes for toggle. I don't think this is really required, maybe you can help out. in
toggleMenufirst there is a setState with a newisOpenthen there is a callback to set thehighlightedIndex. As it's a toggle and there is nodefaultHighlightedIndex, that ends as beingnull. So if you can help improving this, that could be very nice :). -
I still don't understand why you need form related stuff on a dropdown toggle button.
@silviuavram
-
Clear. I will take a look and see if I can work it out.
-
Maybe I did not make myself clear. Want I would like to achieve is the same behavior as having a native
Sure, I can pass an onSubmit callback and trigger it manually, but I want to avoid it, as the rest of the form components would not need it. And that is what I am trying to avoid
Opened https://github.com/downshift-js/downshift/pull/672 to solve the double call to set internal state
solved by https://github.com/downshift-js/downshift/pull/674