import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
/**
 * RadioGroup
 */

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

  _createClass(RadioGroup, null, [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      var defaultValue = props.defaultValue;

      if (!state.isControlled && defaultValue !== state.prevDefaultValue) {
        return {
          value: defaultValue,
          prevDefaultValue: defaultValue
        };
      }

      return null;
    }
  }]);

  function RadioGroup(props) {
    var _this;

    _classCallCheck(this, RadioGroup);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(RadioGroup).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "onItemChange", function (itemValue) {
      return function (e) {
        var onChange = _this.props.onChange;

        if (!_this.isControlled) {
          _this.setState({
            value: itemValue
          });
        }

        if (onChange) {
          onChange(e, itemValue);
        }
      };
    });

    var _this$props = _this.props,
        value = _this$props.value,
        defaultValue = _this$props.defaultValue;
    _this.isControlled = value != null;
    _this.state = {
      isControlled: _this.isControlled,
      value: defaultValue,
      prevDefaultValue: defaultValue
    };
    return _this;
  }

  _createClass(RadioGroup, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      var value = this.props.value;

      if (!this.isControlled && value != null) {
        this.isControlled = true;
        this.setState({
          value: null
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          prefixCls = _this$props2.prefixCls,
          className = _this$props2.className,
          propsValue = _this$props2.value,
          defaultValue = _this$props2.defaultValue,
          disabled = _this$props2.disabled,
          name = _this$props2.name,
          onChange = _this$props2.onChange,
          children = _this$props2.children,
          error = _this$props2.error,
          inputProps = _this$props2.inputProps,
          size = _this$props2.size,
          matchingCondition = _this$props2.matchingCondition,
          status = _this$props2.status,
          errorIcon = _this$props2.errorIcon,
          loadingIcon = _this$props2.loadingIcon,
          successIcon = _this$props2.successIcon,
          showStatusIcon = _this$props2.showStatusIcon,
          other = _objectWithoutProperties(_this$props2, ["prefixCls", "className", "value", "defaultValue", "disabled", "name", "onChange", "children", "error", "inputProps", "size", "matchingCondition", "status", "errorIcon", "loadingIcon", "successIcon", "showStatusIcon"]);

      var stateValue = this.state.value;
      var value = this.isControlled ? propsValue : stateValue;
      var items = React.Children.map(children, function (child, index) {
        if (!React.isValidElement(child)) {
          return null;
        }

        var checked = matchingCondition ? matchingCondition(child.props.value, value) : child.props.value === value;
        return React.cloneElement(child, {
          key: child.props.value || index,
          checked: checked,
          size: size,
          name: name || child.props.name,
          disabled: disabled || child.props.disabled,
          onChange: _this2.onItemChange(child.props.value)
        });
      });
      var classes = classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-disabled"), disabled), className);
      return React.createElement("div", _extends({
        className: classes
      }, other), items);
    }
  }]);

  return RadioGroup;
}(Component);

_defineProperty(RadioGroup, "propTypes", {
  prefixCls: PropTypes.string,

  /** 子组件name */
  name: PropTypes.string,

  /** 是否禁用 */
  disabled: PropTypes.bool,

  /** 当前值（适用于受控组件） */
  value: PropTypes.oneOfType([PropTypes.bool, PropTypes.string, PropTypes.number]),

  /** 默认值 */
  defaultValue: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
  className: PropTypes.string,

  /** 选中状态改变时的回调 function(e, value) */
  onChange: PropTypes.func,

  /** 尺寸 */
  size: PropTypes.oneOf(['small', 'medium', 'large']),

  /** 定制判断值匹配的方法 */
  checkMatching: PropTypes.func
});

_defineProperty(RadioGroup, "defaultProps", {
  prefixCls: 'ne-radio-group',
  disabled: false
});

export { RadioGroup as default };