diff --git a/packages/block-editor/src/components/link-control/search-input.js b/packages/block-editor/src/components/link-control/search-input.js index 0d72bc0444fad3..4bda15f1f1eb7b 100644 --- a/packages/block-editor/src/components/link-control/search-input.js +++ b/packages/block-editor/src/components/link-control/search-input.js @@ -1,7 +1,7 @@ - /** * WordPress dependencies */ +import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Button } from '@wordpress/components'; import { LEFT, @@ -45,21 +45,24 @@ const LinkControlSearchInput = ( { onReset, showInitialSuggestions, } ) => { + const [ selectedSuggestion, setSelectedSuggestion ] = useState(); + const selectItemHandler = ( selection, suggestion ) => { onChange( selection ); - - if ( suggestion ) { - onSelect( suggestion ); - } + setSelectedSuggestion( suggestion ); }; - const stopFormEventsPropagation = ( event ) => { + function selectSuggestionOrCurrentInputValue( event ) { + // Avoid default forms behavior, since it's being handled custom here. event.preventDefault(); - event.stopPropagation(); - }; + + // Interpret the selected value as either the selected suggestion, if + // exists, or otherwise the current input value as entered. + onSelect( selectedSuggestion || { url: value } ); + } return ( -
+ { return (