Slider.js
8.77 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var _react = _interopRequireDefault(require("react"));
var _warning = _interopRequireDefault(require("rc-util/lib/warning"));
var _Track = _interopRequireDefault(require("./common/Track"));
var _createSlider = _interopRequireDefault(require("./common/createSlider"));
var utils = _interopRequireWildcard(require("./utils"));
var Slider = /*#__PURE__*/function (_React$Component) {
(0, _inherits2.default)(Slider, _React$Component);
var _super = (0, _createSuper2.default)(Slider);
/* eslint-enable */
function Slider(props) {
var _this;
(0, _classCallCheck2.default)(this, Slider);
_this = _super.call(this, props);
_this.onEnd = function (force) {
var dragging = _this.state.dragging;
_this.removeDocumentEvents();
if (dragging || force) {
_this.props.onAfterChange(_this.getValue());
}
_this.setState({
dragging: false
});
};
var defaultValue = props.defaultValue !== undefined ? props.defaultValue : props.min;
var value = props.value !== undefined ? props.value : defaultValue;
_this.state = {
value: _this.trimAlignValue(value),
dragging: false
};
(0, _warning.default)(!('minimumTrackStyle' in props), 'minimumTrackStyle will be deprecated, please use trackStyle instead.');
(0, _warning.default)(!('maximumTrackStyle' in props), 'maximumTrackStyle will be deprecated, please use railStyle instead.');
return _this;
}
/**
* [Legacy] Used for inherit other component.
* It's a bad code style which should be refactor.
*/
/* eslint-disable @typescript-eslint/no-unused-vars, class-methods-use-this */
(0, _createClass2.default)(Slider, [{
key: "calcValueByPos",
value: function calcValueByPos(value) {
return 0;
}
}, {
key: "calcOffset",
value: function calcOffset(value) {
return 0;
}
}, {
key: "saveHandle",
value: function saveHandle(index, h) {}
}, {
key: "removeDocumentEvents",
value: function removeDocumentEvents() {}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var _this$props = this.props,
min = _this$props.min,
max = _this$props.max,
value = _this$props.value,
onChange = _this$props.onChange;
if (!('min' in this.props || 'max' in this.props)) {
return;
}
var theValue = value !== undefined ? value : prevState.value;
var nextValue = this.trimAlignValue(theValue, this.props);
if (nextValue === prevState.value) {
return;
} // eslint-disable-next-line
this.setState({
value: nextValue
});
if (!(min === prevProps.min && max === prevProps.max) && utils.isValueOutOfRange(theValue, this.props)) {
onChange(nextValue);
}
}
}, {
key: "onChange",
value: function onChange(state) {
var props = this.props;
var isNotControlled = !('value' in props);
var nextState = state.value > this.props.max ? (0, _objectSpread2.default)((0, _objectSpread2.default)({}, state), {}, {
value: this.props.max
}) : state;
if (isNotControlled) {
this.setState(nextState);
}
var changedValue = nextState.value;
props.onChange(changedValue);
}
}, {
key: "onStart",
value: function onStart(position) {
this.setState({
dragging: true
});
var props = this.props;
var prevValue = this.getValue();
props.onBeforeChange(prevValue);
var value = this.calcValueByPos(position);
this.startValue = value;
this.startPosition = position;
if (value === prevValue) return;
this.prevMovedHandleIndex = 0;
this.onChange({
value: value
});
}
}, {
key: "onMove",
value: function onMove(e, position) {
utils.pauseEvent(e);
var oldValue = this.state.value;
var value = this.calcValueByPos(position);
if (value === oldValue) return;
this.onChange({
value: value
});
}
}, {
key: "onKeyboard",
value: function onKeyboard(e) {
var _this$props2 = this.props,
reverse = _this$props2.reverse,
vertical = _this$props2.vertical;
var valueMutator = utils.getKeyboardValueMutator(e, vertical, reverse);
if (valueMutator) {
utils.pauseEvent(e);
var state = this.state;
var oldValue = state.value;
var mutatedValue = valueMutator(oldValue, this.props);
var value = this.trimAlignValue(mutatedValue);
if (value === oldValue) return;
this.onChange({
value: value
});
this.props.onAfterChange(value);
this.onEnd();
}
}
}, {
key: "getValue",
value: function getValue() {
return this.state.value;
}
}, {
key: "getLowerBound",
value: function getLowerBound() {
var minPoint = this.props.startPoint || this.props.min;
return this.state.value > minPoint ? minPoint : this.state.value;
}
}, {
key: "getUpperBound",
value: function getUpperBound() {
if (this.state.value < this.props.startPoint) {
return this.props.startPoint;
}
return this.state.value;
}
}, {
key: "trimAlignValue",
value: function trimAlignValue(v) {
var nextProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (v === null) {
return null;
}
var mergedProps = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, this.props), nextProps);
var val = utils.ensureValueInRange(v, mergedProps);
return utils.ensureValuePrecision(val, mergedProps);
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props3 = this.props,
prefixCls = _this$props3.prefixCls,
vertical = _this$props3.vertical,
included = _this$props3.included,
disabled = _this$props3.disabled,
minimumTrackStyle = _this$props3.minimumTrackStyle,
trackStyle = _this$props3.trackStyle,
handleStyle = _this$props3.handleStyle,
tabIndex = _this$props3.tabIndex,
ariaLabelForHandle = _this$props3.ariaLabelForHandle,
ariaLabelledByForHandle = _this$props3.ariaLabelledByForHandle,
ariaValueTextFormatterForHandle = _this$props3.ariaValueTextFormatterForHandle,
min = _this$props3.min,
max = _this$props3.max,
startPoint = _this$props3.startPoint,
reverse = _this$props3.reverse,
handleGenerator = _this$props3.handle;
var _this$state = this.state,
value = _this$state.value,
dragging = _this$state.dragging;
var offset = this.calcOffset(value);
var handle = handleGenerator({
className: "".concat(prefixCls, "-handle"),
prefixCls: prefixCls,
vertical: vertical,
offset: offset,
value: value,
dragging: dragging,
disabled: disabled,
min: min,
max: max,
reverse: reverse,
index: 0,
tabIndex: tabIndex,
ariaLabel: ariaLabelForHandle,
ariaLabelledBy: ariaLabelledByForHandle,
ariaValueTextFormatter: ariaValueTextFormatterForHandle,
style: handleStyle[0] || handleStyle,
ref: function ref(h) {
return _this2.saveHandle(0, h);
}
});
var trackOffset = startPoint !== undefined ? this.calcOffset(startPoint) : 0;
var mergedTrackStyle = trackStyle[0] || trackStyle;
var track = _react.default.createElement(_Track.default, {
className: "".concat(prefixCls, "-track"),
vertical: vertical,
included: included,
offset: trackOffset,
reverse: reverse,
length: offset - trackOffset,
style: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, minimumTrackStyle), mergedTrackStyle)
});
return {
tracks: track,
handles: handle
};
}
}]);
return Slider;
}(_react.default.Component);
var _default = (0, _createSlider.default)(Slider);
exports.default = _default;