-
Notifications
You must be signed in to change notification settings - Fork 934
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: add isItemDisabled prop #1510
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
silviuaavram
added a commit
that referenced
this pull request
Jul 13, 2023
* feat: add isItemDisabled prop * readme updates * migration guide update
silviuaavram
added a commit
that referenced
this pull request
Jul 15, 2023
BREAKING CHANGE: Release Downshift v8. ## PRs included: #1440 #1445 #1463 #1510 #1482 ## Changes These changes will also be covered in the [V8 migration guide](https://github.com/downshift-js/downshift/blob/master/src/hooks/MIGRATION_V8.md). ### isItemDisabled Both `useCombobox` and `useSelect` now support the `isItemDisabled` function. This new API is used to mark menu items as disabled, and as such remove the from the navigation and prevent them from being selected. The old API required passing the `disabled` prop to the `getItemProps` function. This old API has been removed and you will receive a console warning if you are trying to use the `disabled` prop in getItemProps. Example of API migration: Old: ```jsx const items = [{value: 'item1'}, {value: 'item2'}] const {getInputProps, ...rest} = useCombobox({items}) return ( // ... rest <li {...getItemProps({item, disabled: item.value === 'item2'})}> ) ``` New: ```jsx const items = [{value: 'item1'}, {value: 'item2'}] const {getInputProps, ...rest} = useCombobox({items, isItemDisabled(item, _index) { return item.value === 'item2' }}) return ( // ... rest <li {...getItemProps({item})}> ) ``` The API for Downshift remains unchange. ### useCombobox input click [ARIA 1.2](combobox-aria-example) recommends to toggle the menu open state at input click. Previously, in v7, the menu was opened on receiving focus, from both mouse and keyboard. Starting with v8, input focus will not trigger any state change anymore. Only the input click event will be handled and will trigger a menu toggle. Consequently: - getInputProps **will not** return any _Focus_ event handler. - getInputProps **will** return a _Click_ event handler. - `useCombobox.stateChangeTypes.InputFocus` has been removed. - `useCombobox.stateChangeTypes.InputClick` has been added instead. We recommend having the default toggle on input click behaviour as it's part of the official ARIA combobox 1.2 spec, but if you wish to override it and not toggle the menu on click, use the stateReducer: ```js function stateReducer(state, actionAndChanges) { const {changes, type} = actionAndChanges switch (type) { case useCombobox.stateChangeTypes.InputClick: return { ...changes, isOpen: state.isOpen, // do not toggle the menu when input is clicked. } default: return changes } } ``` If you want to return to the v7 behaviour and open the menu on focus, keep the reducer above so you remove the toggle behaviour, and call the _openMenu_ imperative function, returned by useCombobox, in a _onFocus_ handler passed to _getInputProps_: ```js <input {...getInputProps({ onFocus() { openMenu() }, })} /> ``` Consider to use the default 1.2 ARIA behaviour provided by default in order to align your widget to the accessibility official spec. This behaviour consistency improves the user experience, since all comboboxes should behave the same and there won't be need for any additional guess work done by your users. ### Getter props return value types Previously, the return value from the getter props returned by both Downshift and the hooks was `any`. In v8 we improved the types in order to reflect what is actually returned: the default values return by the getter prop function and whatever else the user passes as arguments. The type changes are done in [this PR](#1482), make sure you adapt your TS code, if applicable. Also, in the `Downshift` component, the return values for some getter prop values have changed from `null` to `undefined`, since that is what HTML elements expect (value or undefined). These values are also reflected in the TS types. - getRootProps: 'aria-owns': isOpen ? this.menuId : ~~null~~undefined, - getInputProps: - 'aria-controls': isOpen ? this.menuId : ~~null~~undefined - 'aria-activedescendant': isOpen && typeof highlightedIndex === 'number' && highlightedIndex >= 0 ? this.getItemId(highlightedIndex) : ~~null~~undefined - getMenuProps: props && props['aria-label'] ? ~~null~~undefined : this.labelId, ### environment propTypes The `environment` prop is useful when you are using downshift in a context different than regular DOM. Its TS type has been updated to contain `Node` and the propTypes have also been updated to reflect the properties which are required by downshift from `environment`. [combobox-aria-example]: https://www.w3.org/WAI/ARIA/apg/example-index/combobox/combobox-autocomplete-list.html
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It's the same as #1468 but the commits are squashed and it's rebased on v8 branch.