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 all 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
Loading