Skip to content

Commit 798b947

Browse files
Infinite-NullInfinite-Nullt-hamanohanneslsm
authored
Search Block: Move search setting to inspector (#70144)
* Move `showLabel` to inspector control * Move `buttonUseIcon` to inspector control * Move `buttonPosition` to inspector control * Remove block controls * Add settings to `resetAll` * Refactor button position label and simplify state update logic * Update label for show label toggle and reposition button icon control * Change position of width settings * Remove unused SVG icon components from search icons Co-authored-by: Infinite-Null <[email protected]> Co-authored-by: t-hamano <[email protected]> Co-authored-by: hanneslsm <[email protected]>
1 parent 5cde2a6 commit 798b947

File tree

2 files changed

+84
-188
lines changed

2 files changed

+84
-188
lines changed

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

Lines changed: 84 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import clsx from 'clsx';
88
*/
99
import {
1010
useBlockProps,
11-
BlockControls,
1211
InspectorControls,
1312
RichText,
1413
__experimentalUseBorderProps as useBorderProps,
@@ -21,9 +20,8 @@ import {
2120
import { useDispatch, useSelect } from '@wordpress/data';
2221
import { useEffect, useRef } from '@wordpress/element';
2322
import {
24-
ToolbarDropdownMenu,
25-
ToolbarGroup,
26-
ToolbarButton,
23+
SelectControl,
24+
ToggleControl,
2725
ResizableBox,
2826
__experimentalUseCustomUnits as useCustomUnits,
2927
__experimentalUnitControl as UnitControl,
@@ -41,14 +39,6 @@ import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
4139
/**
4240
* Internal dependencies
4341
*/
44-
import {
45-
buttonOnly,
46-
buttonOutside,
47-
buttonInside,
48-
noButton,
49-
buttonWithIcon,
50-
toggleLabel,
51-
} from './icons';
5242
import {
5343
PC_WIDTH_DEFAULT,
5444
PX_WIDTH_DEFAULT,
@@ -201,68 +191,23 @@ export default function SearchEdit( {
201191

202192
const buttonPositionControls = [
203193
{
204-
role: 'menuitemradio',
205-
title: __( 'Button outside' ),
206-
isActive: buttonPosition === 'button-outside',
207-
icon: buttonOutside,
208-
onClick: () => {
209-
setAttributes( {
210-
buttonPosition: 'button-outside',
211-
isSearchFieldHidden: false,
212-
} );
213-
},
194+
label: __( 'Button outside' ),
195+
value: 'button-outside',
214196
},
215197
{
216-
role: 'menuitemradio',
217-
title: __( 'Button inside' ),
218-
isActive: buttonPosition === 'button-inside',
219-
icon: buttonInside,
220-
onClick: () => {
221-
setAttributes( {
222-
buttonPosition: 'button-inside',
223-
isSearchFieldHidden: false,
224-
} );
225-
},
198+
label: __( 'Button inside' ),
199+
value: 'button-inside',
226200
},
227201
{
228-
role: 'menuitemradio',
229-
title: __( 'No button' ),
230-
isActive: buttonPosition === 'no-button',
231-
icon: noButton,
232-
onClick: () => {
233-
setAttributes( {
234-
buttonPosition: 'no-button',
235-
isSearchFieldHidden: false,
236-
} );
237-
},
202+
label: __( 'No button' ),
203+
value: 'no-button',
238204
},
239205
{
240-
role: 'menuitemradio',
241-
title: __( 'Button only' ),
242-
isActive: buttonPosition === 'button-only',
243-
icon: buttonOnly,
244-
onClick: () => {
245-
setAttributes( {
246-
buttonPosition: 'button-only',
247-
isSearchFieldHidden: true,
248-
} );
249-
},
206+
label: __( 'Button only' ),
207+
value: 'button-only',
250208
},
251209
];
252210

253-
const getButtonPositionIcon = () => {
254-
switch ( buttonPosition ) {
255-
case 'button-inside':
256-
return buttonInside;
257-
case 'button-outside':
258-
return buttonOutside;
259-
case 'no-button':
260-
return noButton;
261-
case 'button-only':
262-
return buttonOnly;
263-
}
264-
};
265-
266211
const getResizableSides = () => {
267212
if ( hasOnlyButton ) {
268213
return {};
@@ -376,51 +321,91 @@ export default function SearchEdit( {
376321

377322
const controls = (
378323
<>
379-
<BlockControls>
380-
<ToolbarGroup>
381-
<ToolbarButton
382-
title={ __( 'Show search label' ) }
383-
icon={ toggleLabel }
384-
onClick={ () => {
385-
setAttributes( {
386-
showLabel: ! showLabel,
387-
} );
388-
} }
389-
className={ showLabel ? 'is-pressed' : undefined }
390-
/>
391-
<ToolbarDropdownMenu
392-
icon={ getButtonPositionIcon() }
393-
label={ __( 'Change button position' ) }
394-
controls={ buttonPositionControls }
395-
/>
396-
{ ! hasNoButton && (
397-
<ToolbarButton
398-
title={ __( 'Use button with icon' ) }
399-
icon={ buttonWithIcon }
400-
onClick={ () => {
401-
setAttributes( {
402-
buttonUseIcon: ! buttonUseIcon,
403-
} );
404-
} }
405-
className={
406-
buttonUseIcon ? 'is-pressed' : undefined
407-
}
408-
/>
409-
) }
410-
</ToolbarGroup>
411-
</BlockControls>
412-
413324
<InspectorControls>
414325
<ToolsPanel
415326
label={ __( 'Settings' ) }
416327
resetAll={ () => {
417328
setAttributes( {
418329
width: undefined,
419330
widthUnit: undefined,
331+
showLabel: true,
332+
buttonUseIcon: false,
333+
buttonPosition: 'button-outside',
334+
isSearchFieldHidden: false,
420335
} );
421336
} }
422337
dropdownMenuProps={ dropdownMenuProps }
423338
>
339+
<ToolsPanelItem
340+
hasValue={ () => ! showLabel }
341+
label={ __( 'Show label' ) }
342+
onDeselect={ () => {
343+
setAttributes( {
344+
showLabel: true,
345+
} );
346+
} }
347+
isShownByDefault
348+
>
349+
<ToggleControl
350+
__nextHasNoMarginBottom
351+
checked={ showLabel }
352+
label={ __( 'Show label' ) }
353+
onChange={ ( value ) =>
354+
setAttributes( {
355+
showLabel: value,
356+
} )
357+
}
358+
/>
359+
</ToolsPanelItem>
360+
<ToolsPanelItem
361+
hasValue={ () => buttonPosition !== 'button-outside' }
362+
label={ __( 'Button position' ) }
363+
onDeselect={ () => {
364+
setAttributes( {
365+
buttonPosition: 'button-outside',
366+
isSearchFieldHidden: false,
367+
} );
368+
} }
369+
isShownByDefault
370+
>
371+
<SelectControl
372+
value={ buttonPosition }
373+
__next40pxDefaultSize
374+
__nextHasNoMarginBottom
375+
label={ __( 'Button position' ) }
376+
onChange={ ( value ) => {
377+
setAttributes( {
378+
buttonPosition: value,
379+
isSearchFieldHidden:
380+
value === 'button-only',
381+
} );
382+
} }
383+
options={ buttonPositionControls }
384+
/>
385+
</ToolsPanelItem>
386+
{ buttonPosition !== 'no-button' && (
387+
<ToolsPanelItem
388+
hasValue={ () => !! buttonUseIcon }
389+
label={ __( 'Use button with icon' ) }
390+
onDeselect={ () => {
391+
setAttributes( {
392+
buttonUseIcon: false,
393+
} );
394+
} }
395+
isShownByDefault
396+
>
397+
<ToggleControl
398+
__nextHasNoMarginBottom
399+
checked={ buttonUseIcon }
400+
label={ __( 'Use button with icon' ) }
401+
onChange={ ( value ) =>
402+
setAttributes( {
403+
buttonUseIcon: value,
404+
} )
405+
}
406+
/>
407+
</ToolsPanelItem>
408+
) }
424409
<ToolsPanelItem
425410
hasValue={ () => !! width }
426411
label={ __( 'Width' ) }

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

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)