SearchControl 是 WordPress 组件库中的一个 React 组件,用于提供搜索输入界面。它允许用户输入搜索内容,并支持多种自定义属性以增强可访问性和功能。
import { useState } from 'react';
import { __ } from '@wordpress/i18n';
import { SearchControl } from '@wordpress/components';
function MySearchControl( { className, setState } ) {
const [ searchInput, setSearchInput ] = useState( '' );
return (
<SearchControl
label={ __( 'Search posts' ) }
value={ searchInput }
onChange={ setSearchInput }
/>
);
}SearchControl components let users display a search control.
Check out the Storybook page for a visual exploration of this component.
Render a user interface to input the name of an additional css class.
import { useState } from 'react';
import { __ } from '@wordpress/i18n';
import { SearchControl } from '@wordpress/components';
function MySearchControl( { className, setState } ) {
const [ searchInput, setSearchInput ] = useState( '' );
return (
<SearchControl
label={ __( 'Search posts' ) }
value={ searchInput }
onChange={ setSearchInput }
/>
);
}
The set of props accepted by the component will be specified below.
Props not included in this set will be applied to the input element.
The accessible label for the input.
A label should always be provided as an accessibility best practice, even when a placeholder is defined
and hideLabelFromVision is true.
String__( 'Search' )If this property is added, a specific placeholder will be used for the input.
String__( 'Search' )The current value of the input.
StringThe class that will be added to the classes of the wrapper div.
StringA function that receives the value of the input.
functionNote: this prop is deprecated.
When an onClose callback is provided, the search control will render a close button that will trigger the given callback.
Use this if you want the button to trigger your own logic to close the search field entirely, rather than just clearing the input value.
functionIf this property is added, a help text will be generated using help property as the content.
String|ElementIf true, the label will not be visible, but will be read by screen readers. Defaults to true.
Booleantruesize: 'default' | 'compact'The size of the component.
'default'