-
Notifications
You must be signed in to change notification settings - Fork 929
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
onChange called with null when esc
pressed
#719
Comments
Just as a heads up, I am planning to create React hooks for dropdowns. The solution you are looking for is my first hook, here but it's not an npm package yet. Still work in progress, just as a heads up. As for this usage, Downshift only works by design with search dropdowns (comboboxes), not simple dropdowns such as your. What you can do is to use the In You can also wait for the hooks library to get published and use that directly, since I want to have it 100% ARIA compliant as far as a dropdown is concerned. Let me know how I can assist you further. |
I believe this behaviour was added recently. Previously on ESC just options were closed and item text was set to selected Personally I find this behaviour really user unfriendly. As a user, on ESC I would not expected a selected item to be unset. This is not even done in native select! Remember, Downshift can be used also to build not searchable dropdowns, also with multiple options allowed to be selected. Imagine a client to select 10 items and his surprised face that those 10 items are gone! This person would probably leave the site. I think that please reconsider and at least give a prop to downshift to revert to a previous behaviour, like |
Yeah it does appear it was added recently in #689 to match the spec for comboboxes. The problem actually wasn't the fact that the component was a simple dropdown. I have another component that utilizes Downshift and failed in the exact same way, since it also had an onChange prop that assumed it would only be called with a valid item. Now obviously, this isn't compliant with the combobox spec linked to in the above PR, but it was a safe assumption for how Downshift worked before this change. So yeah, I think a major release would have been nice for this change. Anyways, it's easy enough to override the behavior when needed. It's just a lot easier now to run into a situation where this crashes than it was before. And anyone who was making the same assumption I was (that onChange always passed an item) will have their dropdowns break. For instance, the basic autocomplete example linked to in the README fails when pressing ESC after first selecting an item because of this: https://codesandbox.io/s/github/kentcdodds/downshift-examples/tree/master/?module=%2Fsrc%2Fordered-examples%2F02-complete-autocomplete.js&moduleview=1
because selection now is So I would guess I'm not alone in making the assumption that onChange is only called with an item. Assuming the behavior is remaining this way, we probably should call this out in the docs. Currently, I think they would lead you to believe that it will always be an item. |
The problem is that Downshift can be used not only for combobox, but as select, multiselect too, as mentioned in readme. For select input obviously clearing selected item is wrong. For combobox, well, I know it is w3, but for me this is illogical. Escape usually is used for closing/hiding, not to update state. Standards are changed too, they are not axiom, they are based on someone elses opinion. But I will not argue with it, just problem with using Downshift for |
Thank you all for you input, let's try to work out a solution out of this. This is still a versioned library, no need to update to 3.2.10, nobody wants to break apps and all the effort is to actually make both the library and the apps that use it better. With that in mind:
I like @TLadd docs and examples change for now. I don't think not calling @klis87 I am not for blindly following docs. But those docs are also followed by screen readers, users with disabilities, accessibility trainers, accessibility app developers etc. We should be in line with that. What do you think about the docs/example edit? |
Pressing the ESC key clears the input and calls onChange with null. Some examples did not handle this case and crash as a result. downshift-js/downshift#719 Update debounce-fn to fix gmail and axios examples
* Fix ESC key in examples Pressing the ESC key clears the input and calls onChange with null. Some examples did not handle this case and crash as a result. downshift-js/downshift#719 Update debounce-fn to fix gmail and axios examples * Set debounce-fn to ^3.0.1
@silviuavram I hope community will not complain about it as long as there will be an easy way to revert it to previous behaviour which would also explained in docs. What would you recommend to make not painful for people like me who prefer the previous behaviour? |
Agree, that this behaviour from W3C is weird, raised an issue about that - w3c/aria-practices#1066 |
@klis87 if you want to have Downshift as a |
@silviuavram thx for the link, I will try it after my holidays. Personally I cannot wait for official Downshift hooks support though :) |
Please reopen this, they adjusted standards so that escape doesnt clear value if options are opened common sense won after all ;) this behaviour was veery weird, I am glad they fixed that. Now, we should revert default behaviour in Downshift too |
Downshift will follow ARIA patterns and suggestions. We are not looking to follow 'common sense', but common patterns. Once the 1.2 version is done and released we will apply the needed changes to |
I am a bit confused, even on 1.1: On the ARIA examples page: https://www.w3.org/TR/wai-aria-practices/examples/combobox/aria1.1pattern/listbox-combo.html On the repo, though, it says clearing the value is optional:
Link: https://github.com/w3c/aria-practices/blob/apg-1.1/aria-practices.html#L812 Which one is true? Because is 1.1 clearing states that clearing is optional, shouldn't Downshift support not clearing on |
The new ARIA 1.2 example is only closing the menu on Escape, and it will remove the text only if the menu is closed. Also We will include this in the |
**What**: Update downshift to v6. **Why**: Introduce breaking changes that fix the issues below. Fixes #1088. Fixes #1015. Fixes #1010. Fixes #719. **How**: **The list of breaking changes:** BREAKING CHANGE: Update TS typings for `selectedItem` to accept `null` in both `useSelect` and `useCombobox`. To migrate to the new change, update your types or code if necessary. `selectedItem`, `defaultSelectedItem` and `initialSelectedItem` now have `Item | null` instead of `Item` type. PR with the changes: #1090 BREAKING CHANGE: Update TS typings for `itemToString` to accept `null` for the `item` parameter, in `useSelect` and `useCombobox` + in `Downshift` where this was missing. `useMultipleSelection` type for `itemToString` stays the same as it can't receive `null` as `item`. To migrate to the new change, update your types or code if necessary. `itemToString: (item: Item) => string` -> `itemToString: (item: Item | null) => string}`. PR with the changes: #1075 #1105 BREAKING CHANGE: Pass `type` to the onChange (onInputValueChange, onHighlightedIndexChange, onSelectedItemChange, onIsOpenChange) handler parameters, as specified in the documentation. Also updated the TS typings to reflect this + `onStateChange` - the `type` parameter was passed but it was not reflected in the TS types. To migrate to the new change, update your types or code if necessary, better to view the changes in the PR: #985. BREAKING BEHAVIOUR [useCombobox]: When an item is highlighted by keyboard and user closes the menu using mouse/touch, the item is not selected anymore. The only selection on Blur happens using either Tab / Shift+Tab. PR with the changes: #1109 BREAKING BEHAVIOUR [useCombobox & downshift]: When pressing Escape and the menu is open, only close the menu. When the menu is closed and there is an item selected and/or text in the input, clear the selectedItem and the inputValue. PR with the changes: #719
🎉 This issue has been resolved in version 6.0.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
@silviuaavram is it possible to prevent the text from being cleared if the menu is closed? |
Hi @haleybarlar! Yes, https://www.downshift-js.com/use-combobox#state-reducer In your case you probably need to look for InputKeyDownEscape state change and return the new state you want. Good luck! |
I ran into this as well, and fixed it with the state reducer: const stateReducer = (state, changes) => {
switch (changes.type) {
case Downshift.stateChangeTypes.keyDownEscape:
return {
...changes,
...state,
isOpen: false,
inputValue: state.selectedItem.label,
}
default:
return changes
}
} |
downshift
version: 3.2.10node
version: 10.16.0npm
(oryarn
) version: 1.16.0Relevant code or config
What you did:
I created a Downshift dropdown that behaves as a simple select with a button to open/close. I click on the
button
to open the menu and then pressesc
key.What happened:
The onChange prop is called with
null
, and the above code crashes because onChange is expecting to receive an object.Reproduction repository:
https://codesandbox.io/s/musing-austin-g1bt1
Problem description:
It was unexpected for me that
onChange
was triggered at all in this case. I would expectesc
to close the menu, but keep the value the same as it was.Suggested solution:
Do not trigger onChange in this case.
The text was updated successfully, but these errors were encountered: