vendredi 26 juillet 2019

Group checkbox in Antd by using array object

Here is the original example of group checkbox of antd that I need and its fine:

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,
    });
  };

  render() {
    return (
      <div>
        <div style=>
          <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}
        />
      </div>
    );
  }

}

My question is how can I replace the plainOptions and defaultCheckedList by object array instead of simple array and using attribute name for this check boxes? For example this object:

const plainOptions = [
  {name:'alex', id:1},
  {name:'milo', id:2},
  {name:'saimon', id:3}
];
const defaultCheckedList = [
  {name:'alex', id:1},
  {name:'milo', id:2}
];

I want to use attribute name as the key in this example.




Aucun commentaire:

Enregistrer un commentaire