mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 08:59:15 +08:00
a48e17717c
* demo(Checkbox): adjust check-all demo * chore: semantic
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { Checkbox, Divider } from 'antd';
|
|
import type { CheckboxChangeEvent } from 'antd/es/checkbox';
|
|
import type { CheckboxValueType } from 'antd/es/checkbox/Group';
|
|
|
|
const CheckboxGroup = Checkbox.Group;
|
|
|
|
const plainOptions = ['Apple', 'Pear', 'Orange'];
|
|
const defaultCheckedList = ['Apple', 'Orange'];
|
|
|
|
const App: React.FC = () => {
|
|
const [checkedList, setCheckedList] = useState<CheckboxValueType[]>(defaultCheckedList);
|
|
|
|
const checkAll = plainOptions.length === checkedList.length;
|
|
const indeterminate = checkedList.length > 0 && checkedList.length < plainOptions.length;
|
|
|
|
const onChange = (list: CheckboxValueType[]) => {
|
|
setCheckedList(list);
|
|
};
|
|
|
|
const onCheckAllChange = (e: CheckboxChangeEvent) => {
|
|
setCheckedList(e.target.checked ? plainOptions : []);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<Checkbox indeterminate={indeterminate} onChange={onCheckAllChange} checked={checkAll}>
|
|
Check all
|
|
</Checkbox>
|
|
<Divider />
|
|
<CheckboxGroup options={plainOptions} value={checkedList} onChange={onChange} />
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default App;
|