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 FormTokenField suggestions broken scrollbar when __experimentalExpandOnFocus is defined #56426

Merged
merged 6 commits into from
Nov 27, 2023
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
2 changes: 2 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
### Bug Fix

- `ToolsPanelItem`: Use useLayoutEffect to prevent rendering glitch for last panel item styling. ([#56536](https://github.com/WordPress/gutenberg/pull/56536)).
- `FormTokenField`: Fix broken suggestions scrollbar when the `__experimentalExpandOnFocus` prop is defined ([#56426](https://github.com/WordPress/gutenberg/pull/56426)).
- `FormTokenField`: `onFocus` prop is now typed as a React `FocusEvent` ([#56426](https://github.com/WordPress/gutenberg/pull/56426)).

### Experimental

Expand Down
14 changes: 11 additions & 3 deletions packages/components/src/form-token-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import type { KeyboardEvent, MouseEvent, TouchEvent } from 'react';
import type { KeyboardEvent, MouseEvent, TouchEvent, FocusEvent } from 'react';

/**
* WordPress dependencies
Expand Down Expand Up @@ -162,7 +162,7 @@ export function FormTokenField( props: FormTokenFieldProps ) {
}
}

function onBlur() {
function onBlur( event: FocusEvent ) {
if (
inputHasValidValue() &&
__experimentalValidateInput( incompleteTokenValue )
Expand All @@ -176,7 +176,15 @@ export function FormTokenField( props: FormTokenFieldProps ) {
setIncompleteTokenValue( '' );
setInputOffsetFromEnd( 0 );
setIsActive( false );
setIsExpanded( 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 );

setSelectedSuggestionIndex( -1 );
setSelectedSuggestionScroll( false );
}
Expand Down
4 changes: 1 addition & 3 deletions packages/components/src/form-token-field/token-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ export function UnForwardedTokenInput(
onFocus?.( e );
};

const onBlurHandler: React.FocusEventHandler< HTMLInputElement > = (
e
) => {
const onBlurHandler: FocusEventHandler< HTMLInputElement > = ( e ) => {
setHasFocus( false );
onBlur?.( e );
};
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/form-token-field/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ComponentPropsWithRef,
MouseEventHandler,
ReactNode,
FocusEvent,
} from 'react';

type Messages = {
Expand Down
Loading