Skip to content

Commit

Permalink
fix: allows focus to return to autosuggest input after hitting esc (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cintnguyen authored Oct 12, 2023
1 parent 5521493 commit 34f055a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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 { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -28,6 +28,7 @@ function FormAutosuggest({
...props
}) {
const intl = useIntl();
const formControlRef = useRef();
const parentRef = useArrowKeyNavigation({
selectors: arrowKeyNavigationSelector,
ignoredKeys: ignoredArrowKeysNames,
Expand Down Expand Up @@ -146,6 +147,10 @@ function FormAutosuggest({
if (e.key === 'Escape' && isActive) {
e.preventDefault();

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

setState(prevState => ({
...prevState,
dropDownItems: [],
Expand Down Expand Up @@ -241,6 +246,7 @@ function FormAutosuggest({
</div>
<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 @@ -257,4 +257,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);
});
});

0 comments on commit 34f055a

Please sign in to comment.