choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
146 lines (125 loc) • 4.42 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
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 _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["prefixCls", "vertical", "offset", "style", "disabled", "min", "max", "value", "tabIndex"];
import React, { Component } from 'react';
import classNames from 'classnames';
import addEventListener from '../../_util/addEventListener';
var Handle = /*#__PURE__*/function (_Component) {
_inherits(Handle, _Component);
var _super = _createSuper(Handle);
function Handle() {
var _this;
_classCallCheck(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));
_defineProperty(_assertThisInitialized(_this), "state", {
clickFocused: false
});
_defineProperty(_assertThisInitialized(_this), "handleMouseUp", function () {
if (document.activeElement === _this.handle) {
_this.setClickFocus(true);
}
});
_defineProperty(_assertThisInitialized(_this), "handleBlur", function () {
_this.setClickFocus(false);
});
_defineProperty(_assertThisInitialized(_this), "handleKeyDown", function () {
_this.setClickFocus(false);
});
return _this;
}
_createClass(Handle, [{
key: "componentDidMount",
value: function componentDidMount() {
// mouseup won't trigger if mouse moved out of handle,
// so we listen on document here.
this.onMouseUpListener = addEventListener(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 = _objectWithoutProperties(_this$props, _excluded);
var className = classNames(this.props.className, _defineProperty({}, "".concat(prefixCls, "-handle-click-focused"), this.state.clickFocused));
var postionStyle = vertical ? {
bottom: "".concat(offset, "%")
} : {
left: "".concat(offset, "%")
};
var elStyle = _objectSpread(_objectSpread({}, style), postionStyle);
var ariaProps = {};
if (value !== undefined) {
ariaProps = _objectSpread(_objectSpread({}, ariaProps), {}, {
'aria-valuemin': min,
'aria-valuemax': max,
'aria-valuenow': value,
'aria-disabled': !!disabled
});
}
return /*#__PURE__*/React.createElement("div", _extends({
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;
}(Component);
export { Handle as default };
//# sourceMappingURL=Handle.js.map