表单包裹层
<Form.Item label="爱好" name="checkboxall">
<Space>
<CheckboxGroup
options={AllcheckData}
value={checkedList}
onChange={onChangeCheckBox}
/>
<Checkbox
indeterminate={indeterminate}
onChange={onCheckAllChange}
checked={CheckAll}
>
全选
</Checkbox>
</Space>
</Form.Item>
Checkbox
- 全选必须和 CheckBox 一起用才行否则状态不改变
function useCheckbox() {
const [checkedList, setcheckedList] = useState(['跑步'])
const [AllcheckData, setAllcheckData] = useState(['跑步', '游泳', '射击'])
const [CheckAll, setCheckAll] = useState(false)
const [indeterminate, setindeterminate] = useState(true)
function onChangeCheckBox(checkedList) {
setCheckAll(() => {
return checkedList.length === AllcheckData.length
})
setcheckedList(checkedList)
setindeterminate(() => {
return !!checkedList.length && checkedList.length < AllcheckData.length
})
}
function onCheckAllChange(e) {
console.log(e)
setcheckedList(() => {
if (e.target.checked) {
return AllcheckData
} else {
return []
}
})
setindeterminate(false)
setCheckAll(() => {
return e.target.checked
})
}
return (
<Form.Item label="爱好" name="checkboxall">
<Space>
<CheckboxGroup
options={AllcheckData}
value={checkedList}
onChange={onChangeCheckBox}
/>
<Checkbox
indeterminate={indeterminate}
onChange={onCheckAllChange}
checked={CheckAll}
>
全选
</Checkbox>
</Space>
</Form.Item>
)
}