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

Auto Suggest Fix: allows focus to return to input after hitting esc #2556

Merged
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
8 changes: 7 additions & 1 deletion src/Form/FormAutosuggest.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {
useEffect, useState,
useEffect, useState, useRef,
} from 'react';
import PropTypes from 'prop-types';
import { useIntl } from 'react-intl';
Expand Down Expand Up @@ -27,6 +27,7 @@ function FormAutosuggest({
...props
}) {
const intl = useIntl();
const formControlRef = useRef();
const parentRef = useArrowKeyNavigation({
selectors: arrowKeyNavigationSelector,
ignoredKeys: ignoredArrowKeysNames,
Expand Down Expand Up @@ -132,6 +133,10 @@ function FormAutosuggest({
if (e.key === 'Escape' && isActive) {
e.preventDefault();

if (formControlRef) {
formControlRef.current.focus();
}

setState(prevState => ({
...prevState,
dropDownItems: [],
Expand Down Expand Up @@ -221,6 +226,7 @@ function FormAutosuggest({
<div className="pgn__form-autosuggest__wrapper" ref={parentRef}>
<FormGroup isInvalid={!!state.errorMessage}>
<FormControl
ref={formControlRef}
aria-expanded={(state.dropDownItems.length > 0).toString()}
aria-owns="pgn__form-autosuggest__dropdown-box"
role="combobox"
Expand Down
11 changes: 11 additions & 0 deletions src/Form/tests/FormAutosuggest.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,15 @@ describe('controlled behavior', () => {
const updatedList = queryAllByTestId('autosuggest_optionitem');
expect(updatedList.length).toBe(0);
});

it('check focus on input after esc', () => {
const { getByTestId } = render(<FormAutosuggestTestComponent />);
const input = getByTestId('autosuggest_textbox_input');
const dropdownBtn = getByTestId('autosuggest_iconbutton');
userEvent.click(dropdownBtn);

userEvent.keyboard('{esc}');

expect(input.matches(':focus')).toBe(true);
});
});
Loading