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: add isActive to FormAutosuggest state #2517

Merged
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
17 changes: 10 additions & 7 deletions src/Form/FormAutosuggest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function FormAutosuggest({
ignoredKeys: ignoredArrowKeysNames,
});
const [isMenuClosed, setIsMenuClosed] = useState(true);
const [isActive, setIsActive] = useState(false);
const [state, setState] = useState({
displayValue: value || '',
errorMessage: '',
Expand Down Expand Up @@ -87,6 +88,7 @@ function FormAutosuggest({
};

if (isMenuClosed) {
setIsActive(true);
newState.dropDownItems = getItems(state.displayValue);
newState.errorMessage = '';
}
Expand All @@ -111,8 +113,10 @@ function FormAutosuggest({
/>
);

const handleClickOutside = (e) => {
if (parentRef.current && !parentRef.current.contains(e.target) && state.dropDownItems.length > 0) {
const handleDocumentClick = (e) => {
if (parentRef.current && !parentRef.current.contains(e.target) && isActive) {
setIsActive(false);

setState(prevState => ({
...prevState,
dropDownItems: [],
Expand All @@ -124,13 +128,12 @@ function FormAutosuggest({
};

const keyDownHandler = e => {
if (e.key === 'Escape') {
if (e.key === 'Escape' && isActive) {
e.preventDefault();

setState(prevState => ({
...prevState,
dropDownItems: [],
errorMessage: !state.displayValue ? errorMessageText : '',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[clarification] If removing the errorMessage state here and below, is it correct that this component will no longer be using the errorMessageText prop anymore?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not changing the errorMessage state in the keyDownHandler since the user will still be focused on the component even when they type esc (because this only closes the drop down) #2516. Therefore, we wouldn't want there to be any reported errors until they have left the component.

We still are keeping errorMessageText prop since we use it in handleDocumentClick function above.

}));

setIsMenuClosed(true);
Expand All @@ -139,10 +142,10 @@ function FormAutosuggest({

useEffect(() => {
document.addEventListener('keydown', keyDownHandler);
document.addEventListener('click', handleClickOutside, true);
document.addEventListener('click', handleDocumentClick, true);

return () => {
document.removeEventListener('click', handleClickOutside, true);
document.removeEventListener('click', handleDocumentClick, true);
document.removeEventListener('keydown', keyDownHandler);
};
});
Expand Down Expand Up @@ -173,6 +176,7 @@ function FormAutosuggest({
};

const handleClick = (e) => {
setIsActive(true);
const dropDownItems = getItems(e.target.value);

if (dropDownItems.length > 1) {
Expand Down Expand Up @@ -204,7 +208,6 @@ function FormAutosuggest({
setState(prevState => ({
...prevState,
dropDownItems: [],
errorMessageText,
}));

setIsMenuClosed(true);
Expand Down