I am using react antd. this code is worked perfectly , but I want to replace <CheckboxGroup/>
. Now , I am just pass plainOptions array inside <CheckboxGroup/>
without looping, but right now I want to do loop single <Checkbox>
by using plainOptions array.
For example : plainOptions.map(item=><Checkbox value={item} onChange
{this.onChange}/>) ) // My code would be like this
const plainOptions = ['Apple', 'Pear', 'Orange'];
const defaultCheckedList = ['Apple', 'Orange'];
class App extends React.Component {
state = {
checkedList: defaultCheckedList,
indeterminate: true,
checkAll: false,
};
onChange = checkedList => {
this.setState({
checkedList,
indeterminate: !!checkedList.length && checkedList.length < plainOptions.length,
checkAll: checkedList.length === plainOptions.length,
});
};
onCheckAllChange = e => {
this.setState({
checkedList: e.target.checked ? plainOptions : [],
indeterminate: false,
checkAll: e.target.checked,
});
};
<div className="site-checkbox-all-wrapper">
<Checkbox
indeterminate={this.state.indeterminate}
onChange={this.onCheckAllChange}
checked={this.state.checkAll}
>
Check all
</Checkbox>
</div>
<br />
<CheckboxGroup
options={plainOptions}
value={this.state.checkedList}
onChange={this.onChange}
/>
I want to use <Checkbox/>
instead of CheckboxGroup. My code would be like this
For example : plainOptions.map(item=><Checkbox value={item} onChange
{this.onChange}/>)
How it can be done ?
Here is my codesanbox: https://codesandbox.io/s/4k6qi?file=/index.js
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…