import {Component} from 'react'; import s from './CheckboxList.css'; import CheckboxListItem from './CheckboxListItem'; const ALL_ITEM = Symbol('ALL_ITEM'); export default class CheckboxList extends Component { static ALL_ITEM = ALL_ITEM; constructor(props) { super(props); this.state = { checkedItems: props.checkedItems || props.items, }; } componentWillReceiveProps(newProps) { if (newProps.items !== this.props.items) { if (this.isAllChecked()) { // Preserving `all checked` state this.setState({checkedItems: newProps.items}); this.informAboutChange(newProps.items); } else if (this.state.checkedItems.length) { // Checking only items that are in the new `items` array const checkedItems = newProps.items.filter(item => this.state.checkedItems.find(checkedItem => checkedItem.label === item.label) ); this.setState({checkedItems}); this.informAboutChange(checkedItems); } } else if (newProps.checkedItems !== this.props.checkedItems) { this.setState({checkedItems: newProps.checkedItems}); } } render() { const {label, items, renderLabel} = this.props; return (