Skip to content
187 changes: 88 additions & 99 deletions packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import clsx from 'clsx';
*/
import {
useBlockProps,
BlockControls,
InspectorControls,
RichText,
__experimentalUseBorderProps as useBorderProps,
Expand All @@ -21,9 +20,8 @@ import {
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect, useRef } from '@wordpress/element';
import {
ToolbarDropdownMenu,
ToolbarGroup,
ToolbarButton,
SelectControl,
ToggleControl,
ResizableBox,
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
Expand All @@ -41,14 +39,6 @@ import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
/**
* Internal dependencies
*/
import {
buttonOnly,
buttonOutside,
buttonInside,
noButton,
buttonWithIcon,
toggleLabel,
} from './icons';
import {
PC_WIDTH_DEFAULT,
PX_WIDTH_DEFAULT,
Expand Down Expand Up @@ -201,68 +191,23 @@ export default function SearchEdit( {

const buttonPositionControls = [
{
role: 'menuitemradio',
title: __( 'Button outside' ),
isActive: buttonPosition === 'button-outside',
icon: buttonOutside,
onClick: () => {
setAttributes( {
buttonPosition: 'button-outside',
isSearchFieldHidden: false,
} );
},
label: __( 'Button outside' ),
value: 'button-outside',
},
{
role: 'menuitemradio',
title: __( 'Button inside' ),
isActive: buttonPosition === 'button-inside',
icon: buttonInside,
onClick: () => {
setAttributes( {
buttonPosition: 'button-inside',
isSearchFieldHidden: false,
} );
},
label: __( 'Button inside' ),
value: 'button-inside',
},
{
role: 'menuitemradio',
title: __( 'No button' ),
isActive: buttonPosition === 'no-button',
icon: noButton,
onClick: () => {
setAttributes( {
buttonPosition: 'no-button',
isSearchFieldHidden: false,
} );
},
label: __( 'No button' ),
value: 'no-button',
},
{
role: 'menuitemradio',
title: __( 'Button only' ),
isActive: buttonPosition === 'button-only',
icon: buttonOnly,
onClick: () => {
setAttributes( {
buttonPosition: 'button-only',
isSearchFieldHidden: true,
} );
},
label: __( 'Button only' ),
value: 'button-only',
},
];

const getButtonPositionIcon = () => {
switch ( buttonPosition ) {
case 'button-inside':
return buttonInside;
case 'button-outside':
return buttonOutside;
case 'no-button':
return noButton;
case 'button-only':
return buttonOnly;
}
};

const getResizableSides = () => {
if ( hasOnlyButton ) {
return {};
Expand Down Expand Up @@ -376,47 +321,17 @@ export default function SearchEdit( {

const controls = (
<>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
title={ __( 'Show search label' ) }
icon={ toggleLabel }
onClick={ () => {
setAttributes( {
showLabel: ! showLabel,
} );
} }
className={ showLabel ? 'is-pressed' : undefined }
/>
<ToolbarDropdownMenu
icon={ getButtonPositionIcon() }
label={ __( 'Change button position' ) }
controls={ buttonPositionControls }
/>
{ ! hasNoButton && (
<ToolbarButton
title={ __( 'Use button with icon' ) }
icon={ buttonWithIcon }
onClick={ () => {
setAttributes( {
buttonUseIcon: ! buttonUseIcon,
} );
} }
className={
buttonUseIcon ? 'is-pressed' : undefined
}
/>
) }
</ToolbarGroup>
</BlockControls>

<InspectorControls>
<ToolsPanel
label={ __( 'Settings' ) }
resetAll={ () => {
setAttributes( {
width: undefined,
widthUnit: undefined,
showLabel: true,
buttonUseIcon: false,
buttonPosition: 'button-outside',
isSearchFieldHidden: false,
} );
} }
dropdownMenuProps={ dropdownMenuProps }
Expand Down Expand Up @@ -505,6 +420,80 @@ export default function SearchEdit( {
</ToggleGroupControl>
</VStack>
</ToolsPanelItem>
<ToolsPanelItem
hasValue={ () => ! showLabel }
label={ __( 'Show search label' ) }
onDeselect={ () => {
setAttributes( {
showLabel: true,
} );
} }
isShownByDefault
>
<ToggleControl
__nextHasNoMarginBottom
checked={ showLabel }
label={ __( 'Show search label' ) }
onChange={ ( value ) =>
setAttributes( {
showLabel: value,
} )
}
/>
</ToolsPanelItem>
<ToolsPanelItem
hasValue={ () => !! buttonUseIcon }
label={ __( 'Use button with icon' ) }
onDeselect={ () => {
setAttributes( {
buttonUseIcon: false,
} );
} }
isShownByDefault
>
<ToggleControl
__nextHasNoMarginBottom
checked={ buttonUseIcon }
label={ __( 'Use button with icon' ) }
onChange={ ( value ) =>
setAttributes( {
buttonUseIcon: value,
} )
}
/>
</ToolsPanelItem>
<ToolsPanelItem
hasValue={ () => buttonPosition !== 'button-outside' }
label={ __( 'Change button position' ) }
onDeselect={ () => {
setAttributes( {
buttonPosition: 'button-outside',
isSearchFieldHidden: false,
} );
} }
isShownByDefault
>
<SelectControl
value={ buttonPosition }
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Change button position' ) }
onChange={ ( value ) => {
if ( value === 'button-only' ) {
setAttributes( {
buttonPosition: value,
isSearchFieldHidden: true,
} );
} else {
setAttributes( {
buttonPosition: value,
isSearchFieldHidden: false,
} );
}
} }
options={ buttonPositionControls }
/>
</ToolsPanelItem>
</ToolsPanel>
</InspectorControls>
</>
Expand Down
Loading