diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 3f40eaecf5f028..23bead6158aeb1 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -107,7 +107,7 @@ function render_block_core_search( $attributes, $content, $block ) { wp_enqueue_script_module( '@wordpress/block-library/search/view' ); if ( $instant_search_enabled ) { - $input->set_attribute( 'data-wp-bind--value', 'context.search' ); + $input->set_attribute( 'data-wp-bind--value', 'state.searchGetter' ); $input->set_attribute( 'data-wp-on-async--input', 'actions.updateSearch' ); } } @@ -238,6 +238,10 @@ function render_block_core_search( $attributes, $content, $block ) { // If the query is defined in the URL, it overrides the block context value. $search = empty( $_GET[ $search_key ] ) ? $search : sanitize_text_field( $_GET[ $search_key ] ); + if ( $is_inherited ) { + wp_interactivity_state( 'core/search', array( 'search' => $search ) ); + } + $form_context = array_merge( $form_context, array( diff --git a/packages/block-library/src/search/view.js b/packages/block-library/src/search/view.js index c9fa508335297b..6d99c2b4ca8caa 100644 --- a/packages/block-library/src/search/view.js +++ b/packages/block-library/src/search/view.js @@ -48,6 +48,10 @@ const { state, actions } = store( } return ctx.isSearchInputVisible; }, + get searchGetter() { + const { isInherited, search } = getContext(); + return isInherited ? state.search : search; + }, }, actions: { openSearchInput( event ) { @@ -88,14 +92,18 @@ const { state, actions } = store( *updateSearch( e ) { const { value } = e.target; - const ctx = getContext(); - // Don't navigate if the search didn't really change. - if ( value === ctx.search ) { + if ( value === state.searchGetter ) { return; } - ctx.search = value; + const ctx = getContext(); + + if ( ctx.isInherited ) { + state.search = value; + } else { + ctx.search = value; + } // Debounce the search by 300ms to prevent multiple navigations. supersedePreviousSearch?.();