Repository navigation
Components: Add a WAI-ARIA compliant Combobox. #19657
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # ComboboxControl | ||
|
|
||
| `ComboboxControl` is an enhanced version of a [`CustomSelectControl`](/packages/components/src/custom-select-control/readme.md), with the addition of being able to search for options using a search input. | ||
|
|
||
| ## Table of contents | ||
|
|
||
| 1. [Design guidelines](#design-guidelines) | ||
| 2. [Development guidelines](#development-guidelines) | ||
| 3. [Related components](#related-components) | ||
|
|
||
| ## Design guidelines | ||
|
|
||
| These are the same as [the ones for `CustomSelectControl`s](/packages/components/src/select-control/readme.md#design-guidelines), but this component is better suited for when there are too many items to scroll through or load at once so you need to filter them based on user input. | ||
|
|
||
| ## Development guidelines | ||
|
|
||
| ### Usage | ||
|
|
||
| ```jsx | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { ComboboxControl } from "@wordpress/components"; | ||
| import { useState } from "@wordpress/compose"; | ||
|
|
||
| const options = [ | ||
| { | ||
| key: "small", | ||
| name: "Small", | ||
| style: { fontSize: "50%" } | ||
| }, | ||
| { | ||
| key: "normal", | ||
| name: "Normal", | ||
| style: { fontSize: "100%" } | ||
| }, | ||
| { | ||
| key: "large", | ||
| name: "Large", | ||
| style: { fontSize: "200%" } | ||
| }, | ||
| { | ||
| key: "huge", | ||
| name: "Huge", | ||
| style: { fontSize: "300%" } | ||
| } | ||
| ]; | ||
|
|
||
| function MyComboboxControl() { | ||
| const [, setFontSize] = useState(); | ||
| const [filteredOptions, setFilteredOptions] = useState(options); | ||
| return ( | ||
| <ComboboxControl | ||
| label="Font Size" | ||
| options={filteredOptions} | ||
| onInputValueChange={({ inputValue }) => | ||
| setFilteredOptions( | ||
| options.filter(option => | ||
| option.name.toLowerCase().startsWith(inputValue.toLowerCase()) | ||
| ) | ||
| ) | ||
| } | ||
| onChange={({ selectedItem }) => setFontSize(selectedItem)} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function MyControlledComboboxControl() { | ||
| const [fontSize, setFontSize] = useState(options[0]); | ||
| const [filteredOptions, setFilteredOptions] = useState(options); | ||
| return ( | ||
| <ComboboxControl | ||
| label="Font Size" | ||
| options={filteredOptions} | ||
| onInputValueChange={({ inputValue }) => | ||
| setFilteredOptions( | ||
| options.filter(option => | ||
| option.name.toLowerCase().startsWith(inputValue.toLowerCase()) | ||
| ) | ||
| ) | ||
| } | ||
| onChange={({ selectedItem }) => setFontSize(selectedItem)} | ||
| value={options.find(option => option.key === fontSize.key)} | ||
| /> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ### Props | ||
|
|
||
| #### className | ||
|
|
||
| A custom class name to append to the outer `<div>`. | ||
|
|
||
| - Type: `String` | ||
| - Required: No | ||
|
|
||
| #### hideLabelFromVision | ||
|
|
||
| Used to visually hide the label. It will always be visible to screen readers. | ||
|
|
||
| - Type: `Boolean` | ||
| - Required: No | ||
|
|
||
| #### label | ||
|
|
||
| The label for the control. | ||
|
|
||
| - Type: `String` | ||
| - Required: Yes | ||
|
|
||
| #### options | ||
|
|
||
| The options that can be chosen from. | ||
|
|
||
| - Type: `Array<{ key: String, name: String, style: ?{}, ...rest }>` | ||
| - Required: Yes | ||
|
|
||
| #### onInputValueChange | ||
|
|
||
| Function called with the control's search input value changes. The `inputValue` property contains the next input value. | ||
|
|
||
| - Type: `Function` | ||
| - Required: No | ||
|
|
||
| #### onChange | ||
|
|
||
| Function called with the control's internal state changes. The `selectedItem` property contains the next selected item. | ||
|
|
||
| - Type: `Function` | ||
| - Required: No | ||
|
|
||
| #### value | ||
|
|
||
| Can be used to externally control the value of the control, like in the `MyControlledComboboxControl` example above. | ||
|
|
||
| - Type: `Object` | ||
| - Required: No | ||
|
|
||
| ## Related components | ||
|
|
||
| - Like this component, but without a search input, the `CustomSelectControl` component. | ||
|
|
||
| - To select one option from a set, when you want to show all the available options at once, use the `Radio` component. | ||
| - To select one or more items from a set, use the `CheckboxControl` component. | ||
| - To toggle a single setting on or off, use the `ToggleControl` component. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { useCombobox } from 'downshift'; | ||
| import classnames from 'classnames'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { Button, Dashicon } from '../'; | ||
|
|
||
| const itemToString = ( item ) => item && item.name; | ||
| export default function ComboboxControl( { | ||
| className, | ||
| hideLabelFromVision, | ||
| label, | ||
| options: items, | ||
| onInputValueChange: onInputValueChange, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no change here right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope |
||
| onChange: onSelectedItemChange, | ||
| value: _selectedItem, | ||
| } ) { | ||
| const { | ||
| getLabelProps, | ||
| getToggleButtonProps, | ||
| getComboboxProps, | ||
| getInputProps, | ||
| getMenuProps, | ||
| getItemProps, | ||
| isOpen, | ||
| highlightedIndex, | ||
| selectedItem, | ||
| } = useCombobox( { | ||
| initialSelectedItem: items[ 0 ], | ||
| items, | ||
| itemToString, | ||
| onInputValueChange, | ||
| onSelectedItemChange, | ||
| selectedItem: _selectedItem, | ||
| } ); | ||
| const menuProps = getMenuProps( { | ||
| className: 'components-combobox-control__menu', | ||
| } ); | ||
| // We need this here, because the null active descendant is not | ||
| // fully ARIA compliant. | ||
| if ( | ||
| menuProps[ 'aria-activedescendant' ] && | ||
| menuProps[ 'aria-activedescendant' ].slice( | ||
| 0, | ||
| 'downshift-null'.length | ||
| ) === 'downshift-null' | ||
| ) { | ||
| delete menuProps[ 'aria-activedescendant' ]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like it should be a downshift bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @epiqueras can you point out to me the use case when this happens? I want to fix it in v5. Thanks!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When mounting in uncontrolled mode. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am confused, sorry :)). Do we have an example somewhere?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @epiqueras downshift-js/downshift#945 is this fixing your use case?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should, yeah.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove this code if it's fixed in Downshift? |
||
| } | ||
| return ( | ||
| <div | ||
| className={ classnames( 'components-combobox-control', className ) } | ||
| > | ||
| { /* eslint-disable-next-line jsx-a11y/label-has-associated-control, jsx-a11y/label-has-for */ } | ||
| <label | ||
| { ...getLabelProps( { | ||
| className: classnames( | ||
| 'components-combobox-control__label', | ||
| { | ||
| 'screen-reader-text': hideLabelFromVision, | ||
| } | ||
| ), | ||
| } ) } | ||
| > | ||
| { label } | ||
| </label> | ||
| <div | ||
| { ...getComboboxProps( { | ||
| className: 'components-combobox-control__button', | ||
| } ) } | ||
| > | ||
| <input | ||
| { ...getInputProps( { | ||
| className: 'components-combobox-control__button-input', | ||
| } ) } | ||
| /> | ||
| <Button | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, if we're following Example 1, I don't think we need a button here at all.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's valuable to have a button on the right to open the list without typing, isn't it? It's what Downshift recommends too. cc @silviuavram
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going by the WAI-ARIA patterns linked to above, where the only example with a button is the inline autocomplete one. That's not the one we're implementing here, so thought it would be good to stay consistent with their recommendations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there are versions with the toggle and versions without. The toggle is helpful to see the options before typing anything, but if that is not part of the requirement then it can be ommited. :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we keep it? It looks quite nice.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep it for now, shouldn't do any harm 🙂 |
||
| { ...getToggleButtonProps( { | ||
| // This is needed because some speech recognition software don't support `aria-labelledby`. | ||
| 'aria-label': label, | ||
| 'aria-labelledby': undefined, | ||
| className: 'components-combobox-control__button-button', | ||
| } ) } | ||
| > | ||
| <Dashicon | ||
| icon="arrow-down-alt2" | ||
| className="components-combobox-control__button-icon" | ||
| /> | ||
| </Button> | ||
| </div> | ||
| <ul { ...menuProps }> | ||
| { isOpen && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we check that the input has a non-empty value before rendering the items? I'm still seeing the bug where, after typing in a letter and then deleting it so the input becomes empty, the whole list shows. Or perhaps this should be fixed in Downshift instead? Cc @silviuaavram There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
To customise you can probably use Thanks!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yup, that's the behaviour in the official ARIA combobox examples. Check the third example, that has the button just like ours. The whole menu is only visible on click of the button, or on Arrow Down keypress. But if you type in the box and then clear, the dropdown disappears. |
||
| items.map( ( item, index ) => ( | ||
| // eslint-disable-next-line react/jsx-key | ||
| <li | ||
| { ...getItemProps( { | ||
| item, | ||
| index, | ||
| key: item.key, | ||
| className: classnames( | ||
| 'components-combobox-control__item', | ||
| { | ||
| 'is-highlighted': | ||
| index === highlightedIndex, | ||
| } | ||
| ), | ||
| style: item.style, | ||
| } ) } | ||
| > | ||
| { item === selectedItem && ( | ||
| <Dashicon | ||
| icon="saved" | ||
| className="components-combobox-control__item-icon" | ||
| /> | ||
| ) } | ||
| { item.name } | ||
| </li> | ||
| ) ) } | ||
| </ul> | ||
| </div> | ||
| ); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.