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

Fixes issue of keyboard users unable to tab to the dismiss button and clear the input #30677

Open
wants to merge 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,32 @@ describe('SearchBox', () => {
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ value: '' }));
});

it('can tab into the dismiss button', () => {
const onChange = jest.fn();
renderedComponent = render(<SearchBox defaultValue="hello" onChange={onChange} />);

// First click into the text box
userEvent.click(renderedComponent.getByRole('searchbox'));
userEvent.tab();

const focusedElement = document.activeElement;
expect(focusedElement).toBeDefined();
expect(focusedElement?.tagName.toLowerCase()).toEqual('span');
expect(focusedElement?.getAttribute('aria-label')).toEqual('clear');
});

it('clears the searchbox when pressing {enter} on the dismiss button', () => {
const onChange = jest.fn();
renderedComponent = render(<SearchBox defaultValue="hello" onChange={onChange} />);

const input = renderedComponent.getByRole('searchbox');
const dismissButton = renderedComponent.getByLabelText('clear');
const previousValue = (input as HTMLInputElement).value;
userEvent.type(dismissButton, '{enter}');

expect(onChange).toHaveBeenLastCalledWith(expect.objectContaining({ key: 'Enter' }), { value: '' });
expect(previousValue).toEqual('hello');
expect((input as HTMLInputElement).value).toEqual('');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ export type SearchBoxState = ComponentState<SearchBoxSlots> &
};

/** Overloaded onChange event type, used to merge functionality of regular text entry and the dismiss button */
export type SearchBoxChangeEvent = React.ChangeEvent<HTMLInputElement> | React.MouseEvent<HTMLSpanElement>;
export type SearchBoxChangeEvent =
| React.ChangeEvent<HTMLInputElement>
| React.MouseEvent<HTMLSpanElement>
| React.KeyboardEvent<HTMLSpanElement>;
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports[`SearchBox renders a default state 1`] = `
aria-label="clear"
class="fui-SearchBox__dismiss"
role="button"
tabindex="-1"
tabindex="0"
>
<svg
aria-hidden="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export const useSearchBox_unstable = (props: SearchBoxProps, ref: React.Ref<HTML
props.onChange?.(event, { value: newValue });
});

const handleDismissKeyUp = useEventCallback((event: React.KeyboardEvent<HTMLSpanElement>) => {
if (event.key === 'Enter' && !event.altKey && !event.shiftKey && !event.ctrlKey) {
const newValue = '';
setValue(newValue);
props.onChange?.(event, { value: newValue });
searchBoxRef.current?.focus();
}
});

const inputState = useInput_unstable(
{
type: 'search',
Expand Down Expand Up @@ -104,7 +113,7 @@ export const useSearchBox_unstable = (props: SearchBoxProps, ref: React.Ref<HTML
children: <DismissRegular />,
role: 'button',
'aria-label': 'clear',
tabIndex: -1,
tabIndex: 0,
},
renderByDefault: true,
elementType: 'span',
Expand All @@ -116,6 +125,7 @@ export const useSearchBox_unstable = (props: SearchBoxProps, ref: React.Ref<HTML

if (state.dismiss) {
state.dismiss.onClick = handleDismissClick;
state.dismiss.onKeyUp = handleDismissKeyUp;
}

return state;
Expand Down
Loading