From d1483a2016a26c617be6beceedf324e26ffc6e2e Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 10 Dec 2024 18:55:34 +0000 Subject: [PATCH] Set the search state in the interactivity state for inherited queries. --- packages/block-library/src/search/index.php | 6 +++++- packages/block-library/src/search/view.js | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 3f40eaecf5f02..23bead6158aeb 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 c9fa508335297..6d99c2b4ca8ca 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?.();