Skip to content

Commit

Permalink
[ARIA] Add more Accessible Tags in Input and MenuList
Browse files Browse the repository at this point in the history
Added combo box role for Input item, listbox role to MenuList and option role to MenuItems. Added relationship between elements by ID.

Signed-off-by: Progyan Bhattacharya <bprogyan@gmail.com>
  • Loading branch information
0xTheProDev authored and Progyan-APAC committed Dec 9, 2020
1 parent 998c979 commit 890b5f2
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/react-select/src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -1420,9 +1420,12 @@ export default class Select extends Component<Props, State> {
const { inputIsHidden } = this.state;

const id = inputId || this.getElementId('input');
const listBoxId = this.getElementId('listbox');

// aria attributes makes the JSX "noisy", separated for clarity
const ariaAttributes = {
role: 'combobox',
'aria-owns': listBoxId,
'aria-autocomplete': 'list',
'aria-label': this.props['aria-label'],
'aria-labelledby': this.props['aria-labelledby'],
Expand Down Expand Up @@ -1727,6 +1730,8 @@ export default class Select extends Component<Props, State> {
menuShouldScrollIntoView,
};

const listBoxId = this.getElementId('listbox');

const menuElement = (
<MenuPlacer {...commonProps} {...menuPlacementProps}>
{({ ref, placerProps: { placement, maxHeight } }) => (
Expand All @@ -1749,6 +1754,7 @@ export default class Select extends Component<Props, State> {
<ScrollBlock isEnabled={menuShouldBlockScroll}>
<MenuList
{...commonProps}
id={listBoxId}
innerRef={this.getMenuListRef}
isLoading={isLoading}
maxHeight={maxHeight}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ exports[`defaults - snapshot 1`] = `
>
<input
aria-autocomplete="list"
aria-owns="react-select-2-listbox"
autocapitalize="none"
autocomplete="off"
autocorrect="off"
id="react-select-2-input"
role="combobox"
spellcheck="false"
style="box-sizing: content-box; width: 2px; border: 0px; opacity: 1; outline: 0; padding: 0px;"
tabindex="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ exports[`defaults - snapshot 1`] = `
>
<input
aria-autocomplete="list"
aria-owns="react-select-2-listbox"
autocapitalize="none"
autocomplete="off"
autocorrect="off"
id="react-select-2-input"
role="combobox"
spellcheck="false"
style="box-sizing: content-box; width: 2px; border: 0px; opacity: 1; outline: 0; padding: 0px;"
tabindex="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ exports[`defaults - snapshot 1`] = `
>
<input
aria-autocomplete="list"
aria-owns="react-select-2-listbox"
autocapitalize="none"
autocomplete="off"
autocorrect="off"
id="react-select-2-input"
role="combobox"
spellcheck="false"
style="box-sizing: content-box; width: 2px; border: 0px; opacity: 1; outline: 0; padding: 0px;"
tabindex="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ exports[`snapshot - defaults 1`] = `
>
<input
aria-autocomplete="list"
aria-owns="react-select-2-listbox"
autocapitalize="none"
autocomplete="off"
autocorrect="off"
id="react-select-2-input"
role="combobox"
spellcheck="false"
style="box-sizing: content-box; width: 2px; border: 0px; opacity: 1; outline: 0; padding: 0px;"
tabindex="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ exports[`defaults > snapshot 1`] = `
>
<input
aria-autocomplete="list"
aria-owns="react-select-2-listbox"
autocapitalize="none"
autocomplete="off"
autocorrect="off"
id="react-select-2-input"
role="combobox"
spellcheck="false"
style="box-sizing: content-box; width: 2px; border: 0px; opacity: 1; outline: 0; padding: 0px;"
tabindex="0"
Expand Down
7 changes: 6 additions & 1 deletion packages/react-select/src/components/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ export type MenuListProps = {
children: Node,
/** Inner ref to DOM Node */
innerRef: InnerRef,
/** ID of DOM Element */
id?: string,
};
export type MenuListComponentProps = CommonProps &
MenuListProps &
Expand All @@ -368,9 +370,11 @@ export const menuListCSS = ({
WebkitOverflowScrolling: 'touch',
});
export const MenuList = (props: MenuListComponentProps) => {
const { children, className, cx, getStyles, isMulti, innerRef } = props;
const { children, className, cx, getStyles, isMulti, innerRef, id } = props;
return (
<div
id={id}
role="listbox"
css={getStyles('menuList', props)}
className={cx(
{
Expand All @@ -380,6 +384,7 @@ export const MenuList = (props: MenuListComponentProps) => {
className
)}
ref={innerRef}
aria-expanded="true"
>
{children}
</div>
Expand Down
1 change: 1 addition & 0 deletions packages/react-select/src/components/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const Option = (props: OptionProps) => {
} = props;
return (
<div
role="option"
css={getStyles('option', props)}
className={cx(
{
Expand Down

0 comments on commit 890b5f2

Please sign in to comment.