From 78945df7c1c946a5633471a54c00f072a9d6ac8c Mon Sep 17 00:00:00 2001 From: Cindy Nguyen Date: Wed, 16 Aug 2023 15:36:22 -0400 Subject: [PATCH] fix: allows focus to return to input after hitting esc --- src/Form/FormAutosuggest.jsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Form/FormAutosuggest.jsx b/src/Form/FormAutosuggest.jsx index 86d17a346b..0b5d68cbbc 100644 --- a/src/Form/FormAutosuggest.jsx +++ b/src/Form/FormAutosuggest.jsx @@ -1,5 +1,5 @@ import React, { - useEffect, useState, + useEffect, useState, useRef, } from 'react'; import PropTypes from 'prop-types'; import { useIntl } from 'react-intl'; @@ -27,6 +27,7 @@ function FormAutosuggest({ ...props }) { const intl = useIntl(); + const formControlRef = useRef(); const parentRef = useArrowKeyNavigation({ selectors: arrowKeyNavigationSelector, ignoredKeys: ignoredArrowKeysNames, @@ -131,6 +132,10 @@ function FormAutosuggest({ if (e.key === 'Escape' && isActive) { e.preventDefault(); + if (formControlRef) { + formControlRef.current.focus(); + } + setState(prevState => ({ ...prevState, dropDownItems: [], @@ -220,6 +225,7 @@ function FormAutosuggest({
0).toString()} aria-owns="pgn__form-autosuggest__dropdown-box" value={state.displayValue}