Handle.js
4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import React from 'react';
import classNames from 'classnames';
import addEventListener from "rc-util/es/Dom/addEventListener";
var Handle = /*#__PURE__*/function (_React$Component) {
_inherits(Handle, _React$Component);
var _super = _createSuper(Handle);
function Handle() {
var _this;
_classCallCheck(this, Handle);
_this = _super.apply(this, arguments);
_this.state = {
clickFocused: false
};
_this.setHandleRef = function (node) {
_this.handle = node;
};
_this.handleMouseUp = function () {
if (document.activeElement === _this.handle) {
_this.setClickFocus(true);
}
};
_this.handleMouseDown = function (e) {
// avoid selecting text during drag
// https://github.com/ant-design/ant-design/issues/25010
e.preventDefault(); // fix https://github.com/ant-design/ant-design/issues/15324
_this.focus();
};
_this.handleBlur = function () {
_this.setClickFocus(false);
};
_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 _ref, _ref2;
var _this$props = this.props,
prefixCls = _this$props.prefixCls,
vertical = _this$props.vertical,
reverse = _this$props.reverse,
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,
ariaLabel = _this$props.ariaLabel,
ariaLabelledBy = _this$props.ariaLabelledBy,
ariaValueTextFormatter = _this$props.ariaValueTextFormatter,
restProps = _objectWithoutProperties(_this$props, ["prefixCls", "vertical", "reverse", "offset", "style", "disabled", "min", "max", "value", "tabIndex", "ariaLabel", "ariaLabelledBy", "ariaValueTextFormatter"]);
var className = classNames(this.props.className, _defineProperty({}, "".concat(prefixCls, "-handle-click-focused"), this.state.clickFocused));
var positionStyle = vertical ? (_ref = {}, _defineProperty(_ref, reverse ? 'top' : 'bottom', "".concat(offset, "%")), _defineProperty(_ref, reverse ? 'bottom' : 'top', 'auto'), _defineProperty(_ref, "transform", reverse ? null : "translateY(+50%)"), _ref) : (_ref2 = {}, _defineProperty(_ref2, reverse ? 'right' : 'left', "".concat(offset, "%")), _defineProperty(_ref2, reverse ? 'left' : 'right', 'auto'), _defineProperty(_ref2, "transform", "translateX(".concat(reverse ? '+' : '-', "50%)")), _ref2);
var elStyle = _objectSpread(_objectSpread({}, style), positionStyle);
var mergedTabIndex = tabIndex || 0;
if (disabled || tabIndex === null) {
mergedTabIndex = null;
}
var ariaValueText;
if (ariaValueTextFormatter) {
ariaValueText = ariaValueTextFormatter(value);
}
return React.createElement("div", Object.assign({
ref: this.setHandleRef,
tabIndex: mergedTabIndex
}, restProps, {
className: className,
style: elStyle,
onBlur: this.handleBlur,
onKeyDown: this.handleKeyDown,
onMouseDown: this.handleMouseDown,
// aria attribute
role: "slider",
"aria-valuemin": min,
"aria-valuemax": max,
"aria-valuenow": value,
"aria-disabled": !!disabled,
"aria-label": ariaLabel,
"aria-labelledby": ariaLabelledBy,
"aria-valuetext": ariaValueText
}));
}
}]);
return Handle;
}(React.Component);
export { Handle as default };