CheckboxControl 是 WordPress 组件库中的一个 React 组件,用于实现复选框功能,允许用户从一组选项中选择一个或多个项目。文档涵盖了设计指南、开发指南和组件属性。
import { useState } from 'react';
import { CheckboxControl } from '@wordpress/components';
const MyCheckboxControl = () => {
const [ isChecked, setChecked ] = useState( true );
return (
<CheckboxControl
label="Is author"
help="Is the user a author or not?"
checked={ isChecked }
onChange={ setChecked }
/>
);
}; Checkboxes allow the user to select one or more items from a set.

Use checkboxes when you want users to:

Do
Use checkboxes when users can select multiple items from a list. They let users select more than one item.

Don’t
Don’t use toggles when a list consists of multiple options. Use checkboxes — they take up less space.

Checkboxes can be used to open a list containing sub-selections.
Checkboxes can have a parent-child relationship, with secondary options nested under primary options.

When the parent checkbox is checked, all the child checkboxes are checked. When a parent checkbox is unchecked, all the child checkboxes are unchecked.

If only a few child checkboxes are checked, the parent checkbox becomes a mixed checkbox.
Render an is author checkbox:
import { useState } from 'react';
import { CheckboxControl } from '@wordpress/components';
const MyCheckboxControl = () => {
const [ isChecked, setChecked ] = useState( true );
return (
<CheckboxControl
label="Is author"
help="Is the user a author or not?"
checked={ isChecked }
onChange={ setChecked }
/>
);
};
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.
label: stringA label for the input field, that appears at the side of the checkbox.
The prop will be rendered as content a label element.
If no prop is passed an empty label is rendered.
help: string|ElementIf this property is added, a help text will be generated using help property as the content.
checked: booleanIf checked is true the checkbox will be checked. If checked is false the checkbox will be unchecked.
If no value is passed the checkbox will be unchecked.
onChange: functionA function that receives the checked state (boolean) as input.
indeterminate: booleanIf indeterminate is true the state of the checkbox will be indeterminate.
RadioControl component.FormToggle component.