choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
193 lines (164 loc) • 5.58 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 _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import React, { Component } from 'react';
import PropTypes from 'prop-types';
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, ["prefixCls", "vertical", "offset", "style", "disabled", "min", "max", "value", "tabIndex"]);
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({}, style, {}, postionStyle);
var ariaProps = {};
if (value !== undefined) {
ariaProps = _objectSpread({}, ariaProps, {
'aria-valuemin': min,
'aria-valuemax': max,
'aria-valuenow': value,
'aria-disabled': !!disabled
});
}
return 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);
_defineProperty(Handle, "propTypes", {
prefixCls: PropTypes.string,
className: PropTypes.string,
vertical: PropTypes.bool,
offset: PropTypes.number,
style: PropTypes.object,
disabled: PropTypes.bool,
min: PropTypes.number,
max: PropTypes.number,
value: PropTypes.number,
tabIndex: PropTypes.number
});
export { Handle as default };
//# sourceMappingURL=Handle.js.map