import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import React, { Component } from 'react';
import classNames from 'classnames';
import shallowEqual from 'shallowequal';
import Checkbox from './Checkbox';
import ConfigContext from '../config-provider/ConfigContext';
import { CheckboxContextProvider } from './CheckboxContext';

var CheckboxGroup = /*#__PURE__*/function (_Component) {
  _inherits(CheckboxGroup, _Component);

  var _super = _createSuper(CheckboxGroup);

  function CheckboxGroup(props, context) {
    var _this;

    _classCallCheck(this, CheckboxGroup);

    _this = _super.call(this, props, context);

    _this.toggleOption = function (option) {
      var _assertThisInitialize = _assertThisInitialized(_this),
          state = _assertThisInitialize.state;

      var optionIndex = state.value.indexOf(option.value);

      var value = _toConsumableArray(state.value);

      if (optionIndex === -1) {
        value.push(option.value);
      } else {
        value.splice(optionIndex, 1);
      }

      if (!('value' in _this.props)) {
        _this.setState({
          value: value
        });
      }

      var onChange = _this.props.onChange;

      if (onChange) {
        onChange(value);
      }
    };

    _this.state = {
      value: props.value || props.defaultValue || []
    };
    return _this;
  }

  _createClass(CheckboxGroup, [{
    key: "getContextValue",
    value: function getContextValue() {
      var disabled = this.props.disabled;
      var value = this.state.value;
      return {
        checkboxGroup: {
          toggleOption: this.toggleOption,
          value: value,
          disabled: disabled
        }
      };
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if ('value' in nextProps) {
        this.setState({
          value: nextProps.value || []
        });
      }
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState, nextContext) {
      var context = this.context;
      return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState) || context.getPrefixCls !== nextContext.getPrefixCls;
    }
  }, {
    key: "getOptions",
    value: function getOptions() {
      var options = this.props.options; // https://github.com/Microsoft/TypeScript/issues/7960

      return options.map(function (option) {
        if (typeof option === 'string') {
          return {
            label: option,
            value: option
          };
        }

        return option;
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this,
          _classNames;

      var props = this.props,
          state = this.state;
      var customizePrefixCls = props.prefixCls,
          className = props.className,
          style = props.style,
          options = props.options,
          checkboxPrefixCls = props.checkboxPrefixCls,
          label = props.label;
      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = getPrefixCls('checkbox-group', customizePrefixCls);
      var children = props.children;

      if (options && options.length > 0) {
        children = this.getOptions().map(function (option) {
          return /*#__PURE__*/React.createElement(Checkbox, {
            key: String(option.value),
            disabled: 'disabled' in option ? option.disabled : props.disabled,
            value: option.value,
            checked: state.value.indexOf(option.value) !== -1,
            onChange: function onChange() {
              return _this2.toggleOption(option);
            },
            className: "".concat(prefixCls, "-item"),
            prefixCls: checkboxPrefixCls
          }, option.label);
        });
      }

      var classString = classNames(prefixCls, className);
      var wrapperClassString = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-wrapper"), true), _defineProperty(_classNames, "".concat(prefixCls, "-has-label"), label), _classNames));
      var labelClassString = classNames("".concat(prefixCls, "-label"), {
        'label-disabled': props.disabled
      });
      return /*#__PURE__*/React.createElement(CheckboxContextProvider, _extends({}, this.getContextValue(), {
        getPrefixCls: getPrefixCls
      }), label ? /*#__PURE__*/React.createElement("div", {
        className: wrapperClassString,
        style: style
      }, /*#__PURE__*/React.createElement("span", {
        className: labelClassString
      }, label), /*#__PURE__*/React.createElement("div", {
        className: classString
      }, children)) : /*#__PURE__*/React.createElement("div", {
        className: classString,
        style: style
      }, children));
    }
  }], [{
    key: "contextType",
    get: function get() {
      return ConfigContext;
    }
  }]);

  return CheckboxGroup;
}(Component);

export { CheckboxGroup as default };
CheckboxGroup.displayName = 'CheckboxGroup';
CheckboxGroup.defaultProps = {
  options: []
};
//# sourceMappingURL=Group.js.map
