import Checkbox from '..';
import Divider from '../../divider';
import React from 'react';
import ReactDOM from 'react-dom';

const CheckboxGroup = Checkbox.Group;

const plainOptions = [ 'Apple', 'Pear', 'Orange' ];
const defaultCheckedList = [ 'Apple', 'Orange' ];

const App = () => {
  const [ checkedList, setCheckedList ] = React.useState(defaultCheckedList);
  const [ indeterminate, setIndeterminate ] = React.useState(true);
  const [ checkAll, setCheckAll ] = React.useState(false);

  const onChange = list => {
    setCheckedList(list);
    setIndeterminate(!!list.length && list.length < plainOptions.length);
    setCheckAll(list.length === plainOptions.length);
  };

  const onCheckAllChange = (checked, e) => {
    setCheckedList(e.target.checked ? plainOptions : []);
    setIndeterminate(false);
    setCheckAll(e.target.checked);
  };

  return (
    <div>
      <Checkbox
        indeterminate={indeterminate}
        onChange={onCheckAllChange}
        checked={checkAll}
      >
        Check all
      </Checkbox>
      <Divider />
      <CheckboxGroup
        dataSource={plainOptions}
        value={checkedList}
        onChange={onChange}
      />
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById('checkbox-demo-8'));
