Checkbox can be used alone to switch between two states.
Set the default check through the defaultCheck attribute, and add event functions through onChange
2
const onChange = (e) => {
7
<Checkbox label="Test1" defaultChecked onChange={onChange} />
8
<Checkbox label="Indeterminate" indeterminate />
Disabled state for Checkbox.
2
const [disabled, setDisabled] = React.useState(false);
4
setDisabled(!disabled);
9
<Checkbox label="Test1" disabled={disabled} />
12
<Button onClick={toggle}>Switch</Button>
The indeterminate property can help you to achieve a 'check all' effect.
2
const [indeterminate, setIndeterminate] = React.useState(false);
4
setIndeterminate(!indeterminate);
9
<Checkbox label="Test1" indeterminate={indeterminate} />
12
<Button onClick={toggle}>Switch</Button>
2
const [checked, setChecked] = React.useState(false);
9
<Checkbox label="Test1" checked={checked} />
12
<Button onClick={toggle}>Switch</Button>
2
const [value, setValue] = React.useState(['China']);
4
if (value.length > 1) {
7
setValue(['USA', 'China']);
10
const onChange = (val) => {
15
<CheckboxGroup value={value} onChange={onChange}>
16
<Checkbox label="China" value="China" />
17
<Checkbox label="USA" value="USA" />
20
<Button onClick={toggle}>Switch</Button>