"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _noop = _interopRequireDefault(require("lodash/noop"));

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

var _excluded = ["className", "prefixCls", "disabled", "loadingIcon", "checkedValue", "unCheckedValue", "checkedChildren", "tabIndex", "unCheckedChildren"];

var Switch = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(Switch, _Component);

  var _super = (0, _createSuper2["default"])(Switch);

  function Switch(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, Switch);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "toggle", function () {
      var onClick = _this.props.onClick;
      var checked = !_this.state.checked;

      _this.setChecked(checked);

      onClick(checked);
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleKeyDown", function (e) {
      if (e.keyCode === 37) {
        // Left
        _this.setChecked(false);
      } else if (e.keyCode === 39) {
        // Right
        _this.setChecked(true);
      } else if (e.keyCode === 32 || e.keyCode === 13) {
        // Space, Enter
        _this.toggle();
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleMouseUp", function (e) {
      if (_this.node) {
        _this.node.blur();
      }

      if (_this.props.onMouseUp) {
        _this.props.onMouseUp(e);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "saveNode", function (node) {
      _this.node = node;
    });
    var _checked = false;

    if ('checked' in props) {
      _checked = !!props.checked;
    } else if ('value' in props) {
      _checked = props.checkedValue === props.value;
    } else {
      _checked = !!props.defaultChecked;
    }

    _this.state = {
      checked: _checked
    };
    return _this;
  }

  (0, _createClass2["default"])(Switch, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props = this.props,
          autoFocus = _this$props.autoFocus,
          disabled = _this$props.disabled;

      if (autoFocus && !disabled) {
        this.focus();
      }
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if ('checked' in nextProps) {
        this.setState({
          checked: !!nextProps.checked
        });
      } else if ('value' in nextProps) {
        this.setState({
          checked: nextProps.checkedValue === nextProps.value
        });
      }
    }
  }, {
    key: "setChecked",
    value: function setChecked(checked) {
      if (this.props.disabled) {
        return;
      }

      if (!('checked' in this.props)) {
        this.setState({
          checked: checked
        });
      }

      var _this$props2 = this.props,
          _this$props2$checkedV = _this$props2.checkedValue,
          checkedValue = _this$props2$checkedV === void 0 ? true : _this$props2$checkedV,
          _this$props2$unChecke = _this$props2.unCheckedValue,
          unCheckedValue = _this$props2$unChecke === void 0 ? false : _this$props2$unChecke;
      var value = checked ? checkedValue : unCheckedValue;
      this.props.onChange(value);
    }
  }, {
    key: "focus",
    value: function focus() {
      this.node.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.node.blur();
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props3 = this.props,
          className = _this$props3.className,
          prefixCls = _this$props3.prefixCls,
          disabled = _this$props3.disabled,
          loadingIcon = _this$props3.loadingIcon,
          checkedValue = _this$props3.checkedValue,
          unCheckedValue = _this$props3.unCheckedValue,
          checkedChildren = _this$props3.checkedChildren,
          tabIndex = _this$props3.tabIndex,
          unCheckedChildren = _this$props3.unCheckedChildren,
          restProps = (0, _objectWithoutProperties2["default"])(_this$props3, _excluded);
      var checked = this.state.checked;
      var switchTabIndex = disabled ? -1 : tabIndex || 0;
      var switchClassName = (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, className, !!className), (0, _defineProperty2["default"])(_classNames, prefixCls, true), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-checked"), checked), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames));
      return /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({}, restProps, {
        className: switchClassName,
        tabIndex: switchTabIndex,
        ref: this.saveNode,
        onKeyDown: this.handleKeyDown,
        onClick: this.toggle,
        onMouseUp: this.handleMouseUp
      }), loadingIcon, /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-inner")
      }, checked ? checkedChildren : unCheckedChildren));
    }
  }]);
  return Switch;
}(_react.Component);

Switch.defaultProps = {
  prefixCls: 'rc-switch',
  checkedChildren: null,
  unCheckedChildren: null,
  className: '',
  defaultChecked: false,
  checkedValue: true,
  unCheckedValue: false,
  onChange: _noop["default"],
  onClick: _noop["default"]
};
var _default = Switch;
exports["default"] = _default;
//# sourceMappingURL=Switch.js.map
