Skip to content

Commit

Permalink
Fix form token field suggestion list reopening after blurring the inp…
Browse files Browse the repository at this point in the history
…ut (#57002)

* Update boolean logic to ensure suggestions list closes when expandOnFocus prop is false

* Improve readability

* Add Changelog entry

* Fix CHANGELOG, move entry to the "Unreleased" section

---------

Co-authored-by: Marcelo Serpa <[email protected]>
  • Loading branch information
talldan and fullofcaffeine committed Dec 15, 2023
1 parent b90e4c5 commit 7c45fb6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fix

- `FormTokenField`: Fix a regression where the suggestion list would re-open after clicking away from the input ([#57002](https://github.com/WordPress/gutenberg/pull/57002)).

## 25.14.0 (2023-12-13)

### Enhancements
Expand Down
18 changes: 11 additions & 7 deletions packages/components/src/form-token-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,17 @@ export function FormTokenField( props: FormTokenFieldProps ) {
setInputOffsetFromEnd( 0 );
setIsActive( false );

// If `__experimentalExpandOnFocus` is true, don't close the suggestions list when
// the user clicks on it (`tokensAndInput` will be the element that caused the blur).
const shouldKeepSuggestionsExpanded =
! __experimentalExpandOnFocus ||
( __experimentalExpandOnFocus &&
event.relatedTarget === tokensAndInput.current );
setIsExpanded( shouldKeepSuggestionsExpanded );
if ( __experimentalExpandOnFocus ) {
// If `__experimentalExpandOnFocus` is true, don't close the suggestions list when
// the user clicks on it (`tokensAndInput` will be the element that caused the blur).
const hasFocusWithin =
event.relatedTarget === tokensAndInput.current;
setIsExpanded( hasFocusWithin );
} else {
// Else collapse the suggestion list. This will result in the suggestion list closing
// after a suggestion has been submitted since that causes a blur.
setIsExpanded( false );
}

setSelectedSuggestionIndex( -1 );
setSelectedSuggestionScroll( false );
Expand Down

1 comment on commit 7c45fb6

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 7c45fb6.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7226273311
📝 Reported issues:

Please sign in to comment.