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

Combobox Control: Sync props.value with internal inputValue #65256

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 11 additions & 3 deletions packages/components/src/combobox-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useMemo,
useRef,
useEffect,
useLayoutEffect,
} from '@wordpress/element';
import { useInstanceId } from '@wordpress/compose';
import { speak } from '@wordpress/a11y';
Expand Down Expand Up @@ -137,7 +138,6 @@ function ComboboxControl( props: ComboboxControlProps ) {
} );

const currentOption = options.find( ( option ) => option.value === value );
const currentLabel = currentOption?.label ?? '';
// Use a custom prefix when generating the `instanceId` to avoid having
// duplicate input IDs when rendering this component and `FormTokenField`
// in the same page (see https://github.com/WordPress/gutenberg/issues/42112).
Expand All @@ -148,6 +148,13 @@ function ComboboxControl( props: ComboboxControlProps ) {
const [ isExpanded, setIsExpanded ] = useState( false );
const [ inputHasFocus, setInputHasFocus ] = useState( false );
const [ inputValue, setInputValue ] = useState( '' );

const currentLabel = currentOption?.label;

useLayoutEffect( () => {
setInputValue( currentLabel ?? '' );
}, [ currentLabel ] );

const inputContainer = useRef< HTMLInputElement >( null );

const matchingSuggestions = useMemo( () => {
Expand Down Expand Up @@ -176,7 +183,6 @@ function ComboboxControl( props: ComboboxControlProps ) {
setValue( newSelectedSuggestion.value );
speak( messages.selected, 'assertive' );
setSelectedSuggestion( newSelectedSuggestion );
setInputValue( '' );
setIsExpanded( false );
};

Expand Down Expand Up @@ -234,6 +240,7 @@ function ComboboxControl( props: ComboboxControlProps ) {

const onBlur = () => {
setInputHasFocus( false );
setInputValue( currentLabel ?? '' );
};

const onFocus = () => {
Expand Down Expand Up @@ -265,6 +272,7 @@ function ComboboxControl( props: ComboboxControlProps ) {

const handleOnReset = () => {
setValue( null );
setSelectedSuggestion( null );
inputContainer.current?.focus();
};

Expand Down Expand Up @@ -340,7 +348,7 @@ function ComboboxControl( props: ComboboxControlProps ) {
className="components-combobox-control__input"
instanceId={ instanceId }
ref={ inputContainer }
value={ isExpanded ? inputValue : currentLabel }
value={ inputValue }
onFocus={ onFocus }
onBlur={ onBlur }
onClick={ onClick }
Expand Down
Loading