"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 _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

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 _classnames = _interopRequireDefault(require("classnames"));

var _addEventListener = _interopRequireDefault(require("../../_util/addEventListener"));

var _excluded = ["prefixCls", "vertical", "offset", "style", "disabled", "min", "max", "value", "tabIndex"];

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

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

  function Handle() {
    var _this;

    (0, _classCallCheck2["default"])(this, Handle);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "state", {
      clickFocused: false
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleMouseUp", function () {
      if (document.activeElement === _this.handle) {
        _this.setClickFocus(true);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleBlur", function () {
      _this.setClickFocus(false);
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleKeyDown", function () {
      _this.setClickFocus(false);
    });
    return _this;
  }

  (0, _createClass2["default"])(Handle, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      // mouseup won't trigger if mouse moved out of handle,
      // so we listen on document here.
      this.onMouseUpListener = (0, _addEventListener["default"])(document, 'mouseup', this.handleMouseUp);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.onMouseUpListener) {
        this.onMouseUpListener.remove();
      }
    }
  }, {
    key: "setClickFocus",
    value: function setClickFocus(focused) {
      this.setState({
        clickFocused: focused
      });
    }
  }, {
    key: "clickFocus",
    value: function clickFocus() {
      this.setClickFocus(true);
      this.focus();
    }
  }, {
    key: "focus",
    value: function focus() {
      this.handle.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.handle.blur();
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          prefixCls = _this$props.prefixCls,
          vertical = _this$props.vertical,
          offset = _this$props.offset,
          style = _this$props.style,
          disabled = _this$props.disabled,
          min = _this$props.min,
          max = _this$props.max,
          value = _this$props.value,
          tabIndex = _this$props.tabIndex,
          restProps = (0, _objectWithoutProperties2["default"])(_this$props, _excluded);
      var className = (0, _classnames["default"])(this.props.className, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-handle-click-focused"), this.state.clickFocused));
      var postionStyle = vertical ? {
        bottom: "".concat(offset, "%")
      } : {
        left: "".concat(offset, "%")
      };
      var elStyle = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, style), postionStyle);
      var ariaProps = {};

      if (value !== undefined) {
        ariaProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, ariaProps), {}, {
          'aria-valuemin': min,
          'aria-valuemax': max,
          'aria-valuenow': value,
          'aria-disabled': !!disabled
        });
      }

      return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
        ref: function ref(node) {
          return _this2.handle = node;
        },
        role: "slider",
        tabIndex: disabled ? null : tabIndex || 0
      }, ariaProps, restProps, {
        className: className,
        style: elStyle,
        onBlur: this.handleBlur,
        onKeyDown: this.handleKeyDown
      }));
    }
  }]);
  return Handle;
}(_react.Component);

exports["default"] = Handle;
//# sourceMappingURL=Handle.js.map
