"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames4 = _interopRequireDefault(require("classnames"));

var _mixinUiState = _interopRequireDefault(require("../mixin-ui-state"));

var _configProvider = _interopRequireDefault(require("../config-provider"));

var _util = require("../util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var makeChain = _util.func.makeChain,
    noop = _util.func.noop;
/**
 * Radio
 * @order 1
 */

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

  function Radio(props, context) {
    var _this;

    _classCallCheck(this, Radio);

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

    _defineProperty(_assertThisInitialized(_this), "onChange", function (e) {
      var checked = e.target.checked;
      var value = _this.props.value;

      if (_this.context.__group__) {
        _this.context.onChange(value, e);
      } else if (_this.state.checked !== checked) {
        if (!('checked' in _this.props)) {
          _this.setState({
            checked: checked
          });
        }

        _this.props.onChange(checked, e);
      }
    });

    var _checked;

    if (context.__group__) {
      _checked = context.selectedValue === props.value;
    } else if ('checked' in props) {
      _checked = props.checked;
    } else {
      _checked = props.defaultChecked;
    }

    _this.state = {
      checked: _checked
    };
    _this.disabled = props.disabled || context.__group__ && 'disabled' in context && context.disabled;
    return _this;
  }

  _createClass(Radio, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps, nextContext) {
      if (nextContext.__group__) {
        var selectedValue = nextContext.selectedValue;

        if ('selectedValue' in nextContext) {
          this.setState({
            checked: selectedValue === nextProps.value
          });
        }
      } else if ('checked' in nextProps) {
        this.setState({
          checked: nextProps.checked
        });
      }

      this.disabled = nextProps.disabled || nextContext.__group__ && 'disabled' in nextContext && nextContext.disabled; // when disabled, reset UIState

      if (this.disabled) {
        // only class has an impact, no effect on visual
        this.resetUIState();
      }
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState, nextContext) {
      var shallowEqual = _util.obj.shallowEqual;
      return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState) || !shallowEqual(this.nextContext, nextContext);
    }
  }, {
    key: "render",
    value: function render() {
      var _classnames, _classnames2, _classnames3;

      /* eslint-disable no-unused-vars */
      var _this$props = this.props,
          id = _this$props.id,
          className = _this$props.className,
          children = _this$props.children,
          style = _this$props.style,
          label = _this$props.label,
          onMouseEnter = _this$props.onMouseEnter,
          onMouseLeave = _this$props.onMouseLeave,
          tabIndex = _this$props.tabIndex,
          rtl = _this$props.rtl,
          name = _this$props.name,
          otherProps = _objectWithoutProperties(_this$props, ["id", "className", "children", "style", "label", "onMouseEnter", "onMouseLeave", "tabIndex", "rtl", "name"]);

      var checked = !!this.state.checked;
      var disabled = this.disabled;
      var isButton = this.context.isButton;
      var prefix = this.context.prefix || this.props.prefix;

      var others = _util.obj.pickOthers(Radio.propTypes, otherProps);

      var othersData = _util.obj.pickAttrsWith(others, 'data-');

      var input = _react["default"].createElement("input", _extends({}, _util.obj.pickOthers(othersData, others), {
        name: name,
        id: id,
        role: "radio",
        tabIndex: tabIndex,
        disabled: disabled,
        checked: checked,
        type: "radio",
        onChange: this.onChange,
        "aria-checked": checked,
        className: "".concat(prefix, "radio-input")
      })); // disabled do not hove focus state


      if (!disabled) {
        input = this.getStateElement(input);
      }

      var cls = (0, _classnames4["default"])((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "radio"), true), _defineProperty(_classnames, "checked", checked), _defineProperty(_classnames, "disabled", disabled), _classnames));
      var clsInner = (0, _classnames4["default"])((_classnames2 = {}, _defineProperty(_classnames2, "".concat(prefix, "radio-inner"), true), _defineProperty(_classnames2, "press", checked), _defineProperty(_classnames2, "unpress", !checked), _classnames2));
      var clsWrapper = (0, _classnames4["default"])((_classnames3 = {}, _defineProperty(_classnames3, "".concat(prefix, "radio-wrapper"), true), _defineProperty(_classnames3, className, !!className), _defineProperty(_classnames3, "checked", checked), _defineProperty(_classnames3, "disabled", disabled), _classnames3));
      var childrenCls = "".concat(prefix, "radio-label");
      var radioComp = !isButton ? _react["default"].createElement("span", {
        className: cls
      }, _react["default"].createElement("span", {
        className: clsInner
      }), input) : _react["default"].createElement("span", {
        className: "".concat(prefix, "radio-single-input")
      }, input);
      return _react["default"].createElement("label", _extends({}, othersData, {
        dir: rtl ? 'rtl' : 'ltr',
        style: style,
        "aria-checked": checked,
        "aria-disabled": disabled,
        className: clsWrapper,
        onMouseEnter: disabled ? onMouseEnter : makeChain(this._onUIMouseEnter, onMouseEnter),
        onMouseLeave: disabled ? onMouseLeave : makeChain(this._onUIMouseLeave, onMouseLeave)
      }), radioComp, [children, label].map(function (d, i) {
        return d !== undefined ? _react["default"].createElement("span", {
          key: i,
          className: childrenCls
        }, d) : null;
      }));
    }
  }]);

  return Radio;
}(_mixinUiState["default"]);

_defineProperty(Radio, "displayName", 'Radio');

_defineProperty(Radio, "propTypes", {
  prefix: _propTypes["default"].string,
  rtl: _propTypes["default"].bool,

  /**
   * 自定义类名
   */
  className: _propTypes["default"].string,

  /**
   * 组件input的id
   */
  id: _propTypes["default"].string,

  /**
   * 自定义内敛样式
   */
  style: _propTypes["default"].object,

  /**
   * 设置radio是否选中
   */
  checked: _propTypes["default"].bool,

  /**
   * 设置radio是否默认选中
   */
  defaultChecked: _propTypes["default"].bool,

  /**
   * 通过属性配置label
   */
  label: _propTypes["default"].node,

  /**
   * 状态变化时触发的事件
   * @param {Boolean} checked 是否选中
   * @param {Event} e Dom 事件对象
   */
  onChange: _propTypes["default"].func,

  /**
   * 鼠标进入enter事件
   * @param {Event} e Dom 事件对象
   */
  onMouseEnter: _propTypes["default"].func,

  /**
   * 鼠标离开事件
   * @param {Event} e Dom 事件对象
   */
  onMouseLeave: _propTypes["default"].func,

  /**
   * radio是否被禁用
   */
  disabled: _propTypes["default"].bool,

  /**
   * radio 的value
   */
  value: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number, _propTypes["default"].bool]),

  /**
   * name
   */
  name: _propTypes["default"].string
});

_defineProperty(Radio, "defaultProps", {
  onChange: noop,
  onMouseLeave: noop,
  onMouseEnter: noop,
  tabIndex: 0,
  prefix: 'next-'
});

_defineProperty(Radio, "contextTypes", {
  onChange: _propTypes["default"].func,
  __group__: _propTypes["default"].bool,
  isButton: _propTypes["default"].bool,
  selectedValue: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number, _propTypes["default"].bool]),
  disabled: _propTypes["default"].bool
});

var _default = _configProvider["default"].config(Radio);

exports["default"] = _default;