import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
var _excluded = ["prefixCls", "className", "children", "style"];
import React, { Component } from 'react';
import classNames from 'classnames';
import shallowEqual from 'shallowequal';
import RcCheckbox from '../rc-components/checkbox';
import RadioContext from './RadioContext';

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

  var _super = _createSuper(Radio);

  function Radio() {
    var _this;

    _classCallCheck(this, Radio);

    _this = _super.apply(this, arguments);

    _this.saveCheckbox = function (node) {
      _this.rcCheckbox = node;
    };

    _this.onChange = function (e) {
      var onChange = _this.props.onChange;
      var radioGroup = _this.context.radioGroup;

      if (onChange) {
        onChange(e);
      }

      if (radioGroup && radioGroup.onChange) {
        radioGroup.onChange(e);
      }
    };

    return _this;
  }

  _createClass(Radio, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState, nextContext) {
      var _this$context = this.context,
          radioGroup = _this$context.radioGroup,
          getPrefixCls = _this$context.getPrefixCls;
      return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState) || !shallowEqual(radioGroup, nextContext.radioGroup) || getPrefixCls !== nextContext.getPrefixCls;
    }
  }, {
    key: "focus",
    value: function focus() {
      this.rcCheckbox.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.rcCheckbox.blur();
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var props = this.props,
          context = this.context;

      var customizePrefixCls = props.prefixCls,
          className = props.className,
          children = props.children,
          style = props.style,
          restProps = _objectWithoutProperties(props, _excluded);

      var radioGroup = context.radioGroup,
          getPrefixCls = context.getPrefixCls;
      var prefixCls = getPrefixCls('radio', customizePrefixCls);

      var radioProps = _objectSpread({}, restProps);

      if (radioGroup) {
        radioProps.name = radioGroup.name;
        radioProps.onChange = this.onChange;
        radioProps.checked = props.value === radioGroup.value;
        radioProps.disabled = props.disabled || radioGroup.disabled;
      }

      var wrapperClassString = classNames(className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-wrapper"), true), _defineProperty(_classNames, "".concat(prefixCls, "-wrapper-checked"), radioProps.checked), _defineProperty(_classNames, "".concat(prefixCls, "-wrapper-disabled"), radioProps.disabled), _classNames));
      return /*#__PURE__*/React.createElement("label", {
        className: wrapperClassString,
        style: style,
        onMouseEnter: props.onMouseEnter,
        onMouseLeave: props.onMouseLeave
      }, /*#__PURE__*/React.createElement(RcCheckbox, _extends({}, radioProps, {
        prefixCls: prefixCls,
        ref: this.saveCheckbox
      })), children !== undefined ? /*#__PURE__*/React.createElement("span", null, children) : null);
    }
  }], [{
    key: "contextType",
    get: function get() {
      return RadioContext;
    }
  }]);

  return Radio;
}(Component);

export { Radio as default };
Radio.displayName = 'Radio';
Radio.defaultProps = {
  type: 'radio'
};
//# sourceMappingURL=radio.js.map
