TextControl 是 WordPress 组件库中用于用户输入和编辑文本的 React 组件,适用于单行自由文本输入场景。文档提供了设计指南、开发指南和属性说明,帮助开发者正确使用该组件。
import { useState } from 'react';
import { TextControl } from '@wordpress/components';
const MyTextControl = () => {
const [ className, setClassName ] = useState( '' );
return (
<TextControl
__next40pxDefaultSize
label="Additional CSS Class"
value={ className }
onChange={ ( value ) => setClassName( value ) }
/>
);
};TextControl components let users enter and edit text.

TextControls are best used for free text entry. If you have a set of predefined options you want users to select from, it’s best to use a more constrained component, such as a SelectControl, RadioControl, CheckboxControl, or RangeControl.
Because TextControls are single-line fields, they are not suitable for collecting long responses. For those, use a text area instead.
TextControls should:

Label text is used to inform users as to what information is requested for a text field. Every text field should have a label. Label text should be above the input field, and always visible.
Containers improve the discoverability of text fields by creating contrast between the text field and surrounding content.

Do
A stroke around the container clearly indicates that users can input information.

Don’t
Don’t use unclear visual markers to indicate a text field.
Render a user interface to input the name of an additional css class.
import { useState } from 'react';
import { TextControl } from '@wordpress/components';
const MyTextControl = () => {
const [ className, setClassName ] = useState( '' );
return (
<TextControl
__next40pxDefaultSize
label="Additional CSS Class"
value={ className }
onChange={ ( value ) => setClassName( value ) }
/>
);
};
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.
If this property is added, a label will be generated using label property as the content.
StringIf true, the label will only be visible to screen readers.
BooleanIf this property is added, a help text will be generated using help property as the content.
StringType of the input element to render. Defaults to “text”.
StringThe current value of the input.
String | NumberThe class that will be added with “components-base-control” to the classes of the wrapper div.
If no className is passed only components-base-control is used.
StringA function that receives the value of the input.
functionStart opting into the larger default height that will become the default size in a future version.
Booleanfalse