Skip to content

Commit 2db363a

Browse files
Update Search block label in ListView when it's an Instant Search
- Updated `getBlockLabel` to include `clientId` for improved label context. - Modified `useBlockDisplayTitle` to utilize the updated `getBlockLabel` function. - Introduced `__experimentalLabel` in the search settings to handle block labels based on query loop context and instant search status.
1 parent e0dd93b commit 2db363a

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

packages/block-editor/src/components/block-title/use-block-display-title.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ export default function useBlockDisplayTitle( {
5151
}
5252

5353
const attributes = getBlockAttributes( clientId );
54-
const label = getBlockLabel( blockType, attributes, context );
54+
const label = getBlockLabel(
55+
blockType,
56+
attributes,
57+
context,
58+
clientId
59+
);
5560
// If the label is defined we prioritize it over a possible block variation title match.
5661
if ( label !== blockType.title ) {
5762
return label;

packages/block-library/src/search/index.js

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/**
22
* WordPress dependencies
33
*/
4-
import { __ } from '@wordpress/i18n';
4+
import { __, sprintf } from '@wordpress/i18n';
55
import { search as icon } from '@wordpress/icons';
6+
import { select } from '@wordpress/data';
7+
import { store as blockEditorStore } from '@wordpress/block-editor';
68

79
/**
810
* Internal dependencies
@@ -18,6 +20,41 @@ export { metadata, name };
1820

1921
export const settings = {
2022
icon,
23+
__experimentalLabel( attributes, { clientId } ) {
24+
const { label } = attributes;
25+
const customName = attributes?.metadata?.name;
26+
27+
// Check if the block is inside a Query Loop block.
28+
const queryLoopBlockIds = select(
29+
blockEditorStore
30+
).getBlockParentsByBlockName( clientId, 'core/query' );
31+
32+
// If the block is not inside a Query Loop block, return the block label.
33+
if ( ! queryLoopBlockIds.length ) {
34+
return customName || label;
35+
}
36+
37+
const queryLoopBlock = select( blockEditorStore ).getBlock(
38+
queryLoopBlockIds[ 0 ]
39+
);
40+
41+
// Check if the Query Loop block has enhanced pagination enabled and
42+
// if the `__experimentalEnableSearchQueryBlock` flag is enabled.
43+
const hasInstantSearch = !! (
44+
queryLoopBlock?.attributes?.enhancedPagination &&
45+
window?.__experimentalEnableSearchQueryBlock
46+
);
47+
48+
if ( ! hasInstantSearch ) {
49+
return customName || label;
50+
}
51+
52+
return sprintf(
53+
/* translators: %s: The block label */
54+
__( '%s (Instant search enabled)' ),
55+
customName || label
56+
);
57+
},
2158
example: {
2259
attributes: { buttonText: __( 'Search' ), label: __( 'Search' ) },
2360
viewportWidth: 400,

packages/blocks/src/api/utils.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,19 @@ export function normalizeBlockType( blockTypeOrName ) {
153153
* @param {Object} blockType The block type.
154154
* @param {Object} attributes The values of the block's attributes.
155155
* @param {Object} context The intended use for the label.
156+
* @param {string} clientId The block's client ID.
156157
*
157158
* @return {string} The block label.
158159
*/
159-
export function getBlockLabel( blockType, attributes, context = 'visual' ) {
160+
export function getBlockLabel(
161+
blockType,
162+
attributes,
163+
context = 'visual',
164+
clientId
165+
) {
160166
const { __experimentalLabel: getLabel, title } = blockType;
161167

162-
const label = getLabel && getLabel( attributes, { context } );
168+
const label = getLabel && getLabel( attributes, { context, clientId } );
163169

164170
if ( ! label ) {
165171
return title;

0 commit comments

Comments
 (0)