Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

javascript - How to select multiple checkbox reactjs?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no need for multiple states, keep one single source (checkedList) would be fine.

onCheckItem = value => e => {
  const { checkedList } = this.state;
  this.setState({
    checkedList: checkedList.includes(value)
      ? checkedList.filter(x => x !== value)
      : [...checkedList, value]
  });
};
render() {
  const { checkedList } = this.state;
  return (
    <div>
      <div className="site-checkbox-all-wrapper">
        <Checkbox
          indeterminate={
            checkedList.length < plainOptions.length && checkedList.length > 0
          }
          onChange={this.onCheckAllChange}
          checked={checkedList.length === plainOptions.length}
        >
          Check all
        </Checkbox>
      </div>
      <br />
      {plainOptions.map((item, idx) => (
        <Checkbox
          key={item + idx}
          onChange={this.onCheckItem(item)}
          checked={checkedList.includes(item)}
        >
          {item}
        </Checkbox>
      ))}
    </div>
  );
}

Edit Check all - Ant Design Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...