Skip to content
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

Fix: a11y, enabled clear indicator to be accessible via keyboard #5850

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-walls-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-select': patch
---

Optional props to enable clear indicator to be keyboard accessible
16 changes: 12 additions & 4 deletions packages/react-select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export interface Props<
instanceId?: number | string;
/** Is the select value clearable */
isClearable?: boolean;
/** enabled clear indicator to accessible via keyboard and screen-reader */
enableAccessibleClearIndicator?: boolean;
/** Is the select disabled */
isDisabled: boolean;
/** Is the select in a state of loading (async) */
Expand Down Expand Up @@ -734,7 +736,6 @@ export default class Select<
`${instancePrefix}-option`
)
: [];

const focusedValue = clearFocusValueOnUpdate
? getNextFocusedValue(state, selectValue)
: null;
Expand All @@ -743,7 +744,6 @@ export default class Select<
focusableOptionsWithIds,
focusedOption
);

newMenuOptionsState = {
selectValue,
focusedOption,
Expand All @@ -764,7 +764,8 @@ export default class Select<

let newAriaSelection = ariaSelection;

let hasKeptFocus = isFocused && prevWasFocused;
let hasKeptFocus =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that since we are setting focus back to select input after clearing the value, initial-input-focus is not called again.

isFocused && (prevWasFocused || ariaSelection?.action === 'clear');

if (isFocused && !hasKeptFocus) {
// If `value` or `defaultValue` props are not empty then announce them
Expand Down Expand Up @@ -1860,7 +1861,8 @@ export default class Select<
renderClearIndicator() {
const { ClearIndicator } = this.getComponents();
const { commonProps } = this;
const { isDisabled, isLoading } = this.props;
const { isDisabled, isLoading, enableAccessibleClearIndicator } =
this.props;
const { isFocused } = this.state;

if (
Expand All @@ -1884,6 +1886,12 @@ export default class Select<
{...commonProps}
innerProps={innerProps}
isFocused={isFocused}
enableAccessibleClearIndicator={enableAccessibleClearIndicator}
handleClearingValue={() => {
this.openAfterFocus = false;
this.focusInput();
this.clearValue();
}}
/>
);
}
Expand Down
160 changes: 119 additions & 41 deletions packages/react-select/src/__tests__/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2488,7 +2488,6 @@ test('accessibility > A11yTexts can be provided through ariaLiveMessages prop',
keyCode: 13,
key: 'Enter',
});

expect(container.querySelector(liveRegionEventId)!.textContent).toMatch(
'CUSTOM: option 0 is selected.'
);
Expand Down Expand Up @@ -2926,48 +2925,81 @@ cases(
}
);

test('clear select by clicking on clear button > should not call onMenuOpen', () => {
let onChangeSpy = jest.fn();
let props = { ...BASIC_PROPS, onChange: onChangeSpy };
let { container } = render(
<Select {...props} isMulti value={[OPTIONS[0]]} />
);
cases(
'clear select by clicking on clear button > should not call onMenuOpen',
({ props = BASIC_PROPS }) => {
let onChangeSpy = jest.fn();
let { container } = render(
<Select {...props} onChange={onChangeSpy} isMulti value={[OPTIONS[0]]} />
);

expect(container.querySelectorAll('.react-select__multi-value').length).toBe(
1
);
fireEvent.mouseDown(
container.querySelector('.react-select__clear-indicator')!,
{ button: 0 }
);
expect(onChangeSpy).toBeCalledWith([], {
action: 'clear',
name: BASIC_PROPS.name,
removedValues: [{ label: '0', value: 'zero' }],
});
});
expect(
container.querySelectorAll('.react-select__multi-value').length
).toBe(1);
fireEvent.mouseDown(
container.querySelector('.react-select__clear-indicator')!,
{ button: 0 }
);
expect(onChangeSpy).toBeCalledWith([], {
action: 'clear',
name: BASIC_PROPS.name,
removedValues: [{ label: '0', value: 'zero' }],
});
},
{
'mouse only clear indicator': {
props: {
...BASIC_PROPS,
isClearable: true,
},
},
'clear indicator button': {
...BASIC_PROPS,
isClearable: true,
enableAccessibleClearIndicator: true,
},
}
);

test('clearing select using clear button to not call onMenuOpen or onMenuClose', () => {
let onMenuCloseSpy = jest.fn();
let onMenuOpenSpy = jest.fn();
let props = {
...BASIC_PROPS,
onMenuClose: onMenuCloseSpy,
onMenuOpen: onMenuOpenSpy,
};
let { container } = render(
<Select {...props} isMulti value={[OPTIONS[0]]} />
);
expect(container.querySelectorAll('.react-select__multi-value').length).toBe(
1
);
fireEvent.mouseDown(
container.querySelector('.react-select__clear-indicator')!,
{ button: 0 }
);
expect(onMenuOpenSpy).not.toHaveBeenCalled();
expect(onMenuCloseSpy).not.toHaveBeenCalled();
});
cases(
'clearing select using clear button to not call onMenuOpen or onMenuClose',
({ props = BASIC_PROPS }) => {
let onMenuCloseSpy = jest.fn();
let onMenuOpenSpy = jest.fn();

let { container } = render(
<Select
{...props}
onMenuClose={onMenuCloseSpy}
onMenuOpen={onMenuOpenSpy}
isMulti
value={[OPTIONS[0]]}
/>
);
expect(
container.querySelectorAll('.react-select__multi-value').length
).toBe(1);
fireEvent.mouseDown(
container.querySelector('.react-select__clear-indicator')!,
{ button: 0 }
);
expect(onMenuOpenSpy).not.toHaveBeenCalled();
expect(onMenuCloseSpy).not.toHaveBeenCalled();
},
{
'mouse only clear indicator': {
props: {
...BASIC_PROPS,
isClearable: true,
},
},
'clear indicator button': {
...BASIC_PROPS,
isClearable: true,
enableAccessibleClearIndicator: true,
},
}
);

test('multi select > calls onChange when option is selected and isSearchable is false', () => {
let onChangeSpy = jest.fn();
Expand Down Expand Up @@ -3376,3 +3408,49 @@ cases(
},
}
);

test('enableAccessibleClearIndicator is false > render non-interactive clear indicator', () => {
let props = {
...BASIC_PROPS,
value: OPTIONS[0],
};
let { container } = render(<Select {...props} isClearable />);
expect(
container.querySelector('div.react-select__clear-indicator')
).toBeVisible();
});

test('enableAccessibleClearIndicator is true > clear indicator is focusable and clear value', () => {
let props = {
...BASIC_PROPS,
value: OPTIONS[0],
};
let { container } = render(
<Select {...props} isClearable enableAccessibleClearIndicator />
);

const clearIndicator = container.querySelector(
'button.react-select__clear-indicator'
)!;
expect(clearIndicator).toBeVisible();

userEvent.click(container.querySelector('.react-select__input')!);
userEvent.tab();
expect(
container.querySelector('button.react-select__clear-indicator')!
).toHaveFocus();

fireEvent.keyDown(
container.querySelector('button.react-select__clear-indicator')!,
{
key: 'Enter',
keyCode: 13,
}
);
expect(
container.querySelector('button.react-select__clear-indicator')
).not.toBeInTheDocument();
expect(
container.querySelector('.react-select__single-value')!.textContent
).toEqual('0');
});
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,21 @@ exports[`defaults - snapshot 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}

.emotion-9:hover {
color: hsl(0, 0%, 60%);
}

.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}

.emotion-10 {
display: inline-block;
fill: currentColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,21 @@ exports[`defaults - snapshot 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}

.emotion-9:hover {
color: hsl(0, 0%, 60%);
}

.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}

.emotion-10 {
display: inline-block;
fill: currentColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,21 @@ exports[`defaults - snapshot 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}

.emotion-9:hover {
color: hsl(0, 0%, 60%);
}

.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}

.emotion-10 {
display: inline-block;
fill: currentColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,21 @@ exports[`snapshot - defaults 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}

.emotion-9:hover {
color: hsl(0, 0%, 60%);
}

.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}

.emotion-10 {
display: inline-block;
fill: currentColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,21 @@ exports[`defaults > snapshot 1`] = `
transition: color 150ms;
color: hsl(0, 0%, 80%);
padding: 8px;
border: none;
background: none;
box-sizing: border-box;
}

.emotion-9:hover {
color: hsl(0, 0%, 60%);
}

.emotion-9:focus {
border-color: #2684FF;
outline: unset;
box-shadow: 0 0 0 2px #2684FF inset;
}

.emotion-10 {
display: inline-block;
fill: currentColor;
Expand Down
1 change: 0 additions & 1 deletion packages/react-select/src/components/LiveRegion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ const LiveRegion = <
<span id="aria-guidance">{ariaGuidance}</span>
</Fragment>
);

return (
<Fragment>
{/* We use 'aria-describedby' linked to this component for the initial focus */}
Expand Down
Loading