Source: CheckboxGroup/index.js

import React, { useEffect, useState, memo } from 'react';
import { func, shape, string, any, arrayOf } from 'prop-types';
import isEmpty from 'lodash/isEmpty';

import FormGroup from '@material-ui/core/FormGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Checkbox from '@material-ui/core/Checkbox';

import './index.css';

/**
 * CheckboxGroup component
 *
 * @param {Object} props
 * @param {Object[]} props.options
 * @param {function} props.onChange
 */
function CheckboxGroup({ options, onChange }) {
  /**
   * State - List of checked items
   * @constant
   */
  const [checkedList, changeCheckedList] = useState([]);

  /**
   * State - Are all items checked? Boolean value
   * @constant
   */
  const [checkAll, changeCheckAll] = useState(false);

  /**
   * State - Check if component is in an indeterminate state. Boolean value
   * @constant
   */
  const [indeterminate, changeIndeterminate] = useState(false);

  useEffect(() => {
    if (isEmpty(options)) {
      return;
    }

    let dataChecked = [];

    options.forEach(o => {
      if (o.checked) {
        dataChecked = [...dataChecked, o.value.toString()];
      }
    });

    changeCheckedList(dataChecked);
  }, []);

  /**
   * Handle check change
   *
   * @param {*} e
   */
  const handleChange = e => {
    const newCheckedList = checkedList.includes(e.target.value)
      ? checkedList.filter(i => i !== e.target.value) // Remove item
      : [...checkedList, e.target.value]; // Add item

    if (onChange) {
      onChange(newCheckedList);
    }

    changeCheckedList(newCheckedList);
    changeIndeterminate(
      !!newCheckedList.length && newCheckedList.length < options.length
    );
    changeCheckAll(newCheckedList.length === options.length);
  };

  /**
   * Handle check all elements selected
   *
   * @param {*} e
   */
  const handleCheckAll = e => {
    const isChecked = e.target.checked;
    const newCheckedList = isChecked
      ? options.map(t => t.value.toString())
      : [];

    if (onChange) {
      onChange(newCheckedList);
    }

    changeCheckedList(newCheckedList);
    changeIndeterminate(false);
    changeCheckAll(isChecked);
  };

  return (
    <FormGroup>
      <FormControlLabel
        control={
          <Checkbox
            indeterminate={indeterminate}
            checked={checkAll}
            onChange={handleCheckAll}
          />
        }
        label='Seleziona tutti'
      />
      {options &&
        options.map(option => (
          <FormControlLabel
            key={option.value}
            control={
              <Checkbox
                checked={checkedList.includes(option.value.toString())}
                onChange={handleChange}
                value={option.value}
              />
            }
            label={option.label}
          />
        ))}
    </FormGroup>
  );
}

CheckboxGroup.propTypes = {
  onChange: func,
  options: arrayOf(shape({ label: string, value: any }))
};

CheckboxGroup.defaultProps = {
  options: []
};

export default memo(CheckboxGroup);