1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports["default"] = void 0;
|
7 |
|
8 | var React = _interopRequireWildcard(require("react"));
|
9 |
|
10 | var _classnames = _interopRequireDefault(require("classnames"));
|
11 |
|
12 | var _PickerMixin = _interopRequireDefault(require("./PickerMixin"));
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
15 |
|
16 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
|
17 |
|
18 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
19 |
|
20 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
21 |
|
22 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
23 |
|
24 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
25 |
|
26 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
27 |
|
28 | function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
29 |
|
30 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
31 |
|
32 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
33 |
|
34 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
35 |
|
36 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
37 |
|
38 | ;
|
39 |
|
40 | var Picker =
|
41 |
|
42 | function (_React$Component) {
|
43 | _inherits(Picker, _React$Component);
|
44 |
|
45 | function Picker(props) {
|
46 | var _this;
|
47 |
|
48 | _classCallCheck(this, Picker);
|
49 |
|
50 | _this = _possibleConstructorReturn(this, _getPrototypeOf(Picker).call(this, props));
|
51 |
|
52 | _this.scrollHanders = function () {
|
53 | var scrollY = -1;
|
54 | var lastY = 0;
|
55 | var startY = 0;
|
56 | var scrollDisabled = false;
|
57 | var isMoving = false;
|
58 |
|
59 | var setTransform = function setTransform(nodeStyle, value) {
|
60 | nodeStyle.transform = value;
|
61 | nodeStyle.webkitTransform = value;
|
62 | };
|
63 |
|
64 | var setTransition = function setTransition(nodeStyle, value) {
|
65 | nodeStyle.transition = value;
|
66 | nodeStyle.webkitTransition = value;
|
67 | };
|
68 |
|
69 | var scrollTo = function scrollTo(_x, y) {
|
70 | var time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : .3;
|
71 |
|
72 | if (scrollY !== y) {
|
73 | scrollY = y;
|
74 |
|
75 | if (time && !_this.props.noAnimate) {
|
76 | setTransition(_this.contentRef.style, "cubic-bezier(0,0,0.2,1.15) ".concat(time, "s"));
|
77 | }
|
78 |
|
79 | setTransform(_this.contentRef.style, "translate3d(0,".concat(-y, "px,0)"));
|
80 | setTimeout(function () {
|
81 | _this.scrollingComplete();
|
82 |
|
83 | if (_this.contentRef) {
|
84 | setTransition(_this.contentRef.style, '');
|
85 | }
|
86 | }, +time * 1000);
|
87 | }
|
88 | };
|
89 |
|
90 | var Velocity = function () {
|
91 | var minInterval = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 30;
|
92 | var maxInterval = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100;
|
93 | var _time = 0;
|
94 | var _y = 0;
|
95 | var _velocity = 0;
|
96 | var recorder = {
|
97 | record: function record(y) {
|
98 | var now = +new Date();
|
99 | _velocity = (y - _y) / (now - _time);
|
100 |
|
101 | if (now - _time >= minInterval) {
|
102 | _velocity = now - _time <= maxInterval ? _velocity : 0;
|
103 | _y = y;
|
104 | _time = now;
|
105 | }
|
106 | },
|
107 | getVelocity: function getVelocity(y) {
|
108 | if (y !== _y) {
|
109 | recorder.record(y);
|
110 | }
|
111 |
|
112 | return _velocity;
|
113 | }
|
114 | };
|
115 | return recorder;
|
116 | }();
|
117 |
|
118 | var onFinish = function onFinish() {
|
119 | isMoving = false;
|
120 | var targetY = scrollY;
|
121 | var height = (_this.props.children.length - 1) * _this.itemHeight;
|
122 | var time = .3;
|
123 | var velocity = Velocity.getVelocity(targetY) * 4;
|
124 |
|
125 | if (velocity) {
|
126 | targetY = velocity * 40 + targetY;
|
127 | time = Math.abs(velocity) * .1;
|
128 | }
|
129 |
|
130 | if (targetY % _this.itemHeight !== 0) {
|
131 | targetY = Math.round(targetY / _this.itemHeight) * _this.itemHeight;
|
132 | }
|
133 |
|
134 | if (targetY < 0) {
|
135 | targetY = 0;
|
136 | } else if (targetY > height) {
|
137 | targetY = height;
|
138 | }
|
139 |
|
140 | scrollTo(0, targetY, time < .3 ? .3 : time);
|
141 |
|
142 | _this.onScrollChange();
|
143 | };
|
144 |
|
145 | var onStart = function onStart(y) {
|
146 | if (scrollDisabled) {
|
147 | return;
|
148 | }
|
149 |
|
150 | isMoving = true;
|
151 | startY = y;
|
152 | lastY = scrollY;
|
153 | };
|
154 |
|
155 | var onMove = function onMove(y) {
|
156 | if (scrollDisabled || !isMoving) {
|
157 | return;
|
158 | }
|
159 |
|
160 | scrollY = lastY - y + startY;
|
161 | Velocity.record(scrollY);
|
162 |
|
163 | _this.onScrollChange();
|
164 |
|
165 | setTransform(_this.contentRef.style, "translate3d(0,".concat(-scrollY, "px,0)"));
|
166 | };
|
167 |
|
168 | return {
|
169 | touchstart: function touchstart(evt) {
|
170 | return onStart(evt.touches[0].pageY);
|
171 | },
|
172 | mousedown: function mousedown(evt) {
|
173 | return onStart(evt.pageY);
|
174 | },
|
175 | touchmove: function touchmove(evt) {
|
176 | evt.preventDefault();
|
177 | onMove(evt.touches[0].pageY);
|
178 | },
|
179 | mousemove: function mousemove(evt) {
|
180 | evt.preventDefault();
|
181 | onMove(evt.pageY);
|
182 | },
|
183 | touchend: function touchend() {
|
184 | return onFinish();
|
185 | },
|
186 | touchcancel: function touchcancel() {
|
187 | return onFinish();
|
188 | },
|
189 | mouseup: function mouseup() {
|
190 | return onFinish();
|
191 | },
|
192 | getValue: function getValue() {
|
193 | return scrollY;
|
194 | },
|
195 | scrollTo: scrollTo,
|
196 | setDisabled: function setDisabled() {
|
197 | var disabled = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
198 | scrollDisabled = disabled;
|
199 | }
|
200 | };
|
201 | }();
|
202 |
|
203 | _this.scrollTo = function (top) {
|
204 | _this.scrollHanders.scrollTo(0, top);
|
205 | };
|
206 |
|
207 | _this.scrollToWithoutAnimation = function (top) {
|
208 | _this.scrollHanders.scrollTo(0, top, 0);
|
209 | };
|
210 |
|
211 | _this.fireValueChange = function (selectedValue) {
|
212 | if (selectedValue !== _this.state.selectedValue) {
|
213 | if (!('selectedValue' in _this.props)) {
|
214 | _this.setState({
|
215 | selectedValue: selectedValue
|
216 | });
|
217 | }
|
218 |
|
219 | if (_this.props.onValueChange) {
|
220 | _this.props.onValueChange(selectedValue);
|
221 | }
|
222 | }
|
223 | };
|
224 |
|
225 | _this.onScrollChange = function () {
|
226 | var top = _this.scrollHanders.getValue();
|
227 |
|
228 | if (top >= 0) {
|
229 | var children = React.Children.toArray(_this.props.children);
|
230 |
|
231 | var index = _this.props.computeChildIndex(top, _this.itemHeight, children.length);
|
232 |
|
233 | if (_this.scrollValue !== index) {
|
234 | _this.scrollValue = index;
|
235 | var child = children[index];
|
236 |
|
237 | if (child && _this.props.onScrollChange) {
|
238 | _this.props.onScrollChange(child.props.value);
|
239 | } else if (!child && console.warn) {
|
240 | console.warn('child not found', children, index);
|
241 | }
|
242 | }
|
243 | }
|
244 | };
|
245 |
|
246 | _this.scrollingComplete = function () {
|
247 | var top = _this.scrollHanders.getValue();
|
248 |
|
249 | if (top >= 0) {
|
250 | _this.props.doScrollingComplete(top, _this.itemHeight, _this.fireValueChange);
|
251 | }
|
252 | };
|
253 |
|
254 | var selectedValueState;
|
255 | var _this$props = _this.props,
|
256 | selectedValue = _this$props.selectedValue,
|
257 | defaultSelectedValue = _this$props.defaultSelectedValue;
|
258 |
|
259 | if (selectedValue !== undefined) {
|
260 | selectedValueState = selectedValue;
|
261 | } else if (defaultSelectedValue !== undefined) {
|
262 | selectedValueState = defaultSelectedValue;
|
263 | } else {
|
264 | var children = React.Children.toArray(_this.props.children);
|
265 | selectedValueState = children && children[0] && children[0].props.value;
|
266 | }
|
267 |
|
268 | _this.state = {
|
269 | selectedValue: selectedValueState
|
270 | };
|
271 | return _this;
|
272 | }
|
273 |
|
274 | _createClass(Picker, [{
|
275 | key: "componentDidMount",
|
276 | value: function componentDidMount() {
|
277 | var _this2 = this;
|
278 |
|
279 | var contentRef = this.contentRef,
|
280 | indicatorRef = this.indicatorRef,
|
281 | maskRef = this.maskRef,
|
282 | rootRef = this.rootRef;
|
283 | var rootHeight = rootRef.getBoundingClientRect().height;
|
284 |
|
285 | var itemHeight = this.itemHeight = indicatorRef.getBoundingClientRect().height;
|
286 | var num = Math.floor(rootHeight / itemHeight);
|
287 |
|
288 | if (num % 2 === 0) {
|
289 | num--;
|
290 | }
|
291 |
|
292 | num--;
|
293 | num /= 2;
|
294 | contentRef.style.padding = "".concat(itemHeight * num, "px 0");
|
295 | indicatorRef.style.top = "".concat(itemHeight * num, "px");
|
296 | maskRef.style.backgroundSize = "100% ".concat(itemHeight * num, "px");
|
297 | this.scrollHanders.setDisabled(this.props.disabled);
|
298 | this.props.select(this.state.selectedValue, this.itemHeight, this.scrollTo);
|
299 | var passiveSupported = this.passiveSupported();
|
300 | var willPreventDefault = passiveSupported ? {
|
301 | passive: false
|
302 | } : false;
|
303 | var willNotPreventDefault = passiveSupported ? {
|
304 | passive: true
|
305 | } : false;
|
306 | Object.keys(this.scrollHanders).forEach(function (key) {
|
307 | if (key.indexOf('touch') === 0 || key.indexOf('mouse') === 0) {
|
308 | var pd = key.indexOf('move') >= 0 ? willPreventDefault : willNotPreventDefault;
|
309 | rootRef.addEventListener(key, _this2.scrollHanders[key], pd);
|
310 | }
|
311 | });
|
312 | }
|
313 | }, {
|
314 | key: "componentWillUnmount",
|
315 | value: function componentWillUnmount() {
|
316 | var _this3 = this;
|
317 |
|
318 | Object.keys(this.scrollHanders).forEach(function (key) {
|
319 | if (key.indexOf('touch') === 0 || key.indexOf('mouse') === 0) {
|
320 | _this3.rootRef.removeEventListener(key, _this3.scrollHanders[key]);
|
321 | }
|
322 | });
|
323 | }
|
324 | }, {
|
325 | key: "passiveSupported",
|
326 | value: function passiveSupported() {
|
327 | var passiveSupported = false;
|
328 |
|
329 | try {
|
330 | var options = Object.defineProperty({}, 'passive', {
|
331 | get: function get() {
|
332 | passiveSupported = true;
|
333 | }
|
334 | });
|
335 | window.addEventListener('test', null, options);
|
336 | } catch (err) {}
|
337 |
|
338 | return passiveSupported;
|
339 | }
|
340 | }, {
|
341 | key: "componentWillReceiveProps",
|
342 | value: function componentWillReceiveProps(nextProps) {
|
343 | var _this4 = this;
|
344 |
|
345 | if ('selectedValue' in nextProps) {
|
346 | if (this.state.selectedValue !== nextProps.selectedValue) {
|
347 | this.setState({
|
348 | selectedValue: nextProps.selectedValue
|
349 | }, function () {
|
350 | _this4.props.select(nextProps.selectedValue, _this4.itemHeight, nextProps.noAnimate ? _this4.scrollToWithoutAnimation : _this4.scrollTo);
|
351 | });
|
352 | }
|
353 | }
|
354 |
|
355 | this.scrollHanders.setDisabled(nextProps.disabled);
|
356 | }
|
357 | }, {
|
358 | key: "shouldComponentUpdate",
|
359 | value: function shouldComponentUpdate(nextProps, nextState) {
|
360 | return this.state.selectedValue !== nextState.selectedValue || this.props.children !== nextProps.children;
|
361 | }
|
362 | }, {
|
363 | key: "componentDidUpdate",
|
364 | value: function componentDidUpdate() {
|
365 | this.props.select(this.state.selectedValue, this.itemHeight, this.scrollToWithoutAnimation);
|
366 | }
|
367 | }, {
|
368 | key: "getValue",
|
369 | value: function getValue() {
|
370 | if ('selectedValue' in this.props) {
|
371 | return this.props.selectedValue;
|
372 | }
|
373 |
|
374 | var children = React.Children.toArray(this.props.children);
|
375 | return children && children[0] && children[0].props.value;
|
376 | }
|
377 | }, {
|
378 | key: "render",
|
379 | value: function render() {
|
380 | var _pickerCls,
|
381 | _this5 = this;
|
382 |
|
383 | var props = this.props;
|
384 | var prefixCls = props.prefixCls,
|
385 | itemStyle = props.itemStyle,
|
386 | indicatorStyle = props.indicatorStyle,
|
387 | _props$indicatorClass = props.indicatorClassName,
|
388 | indicatorClassName = _props$indicatorClass === void 0 ? '' : _props$indicatorClass,
|
389 | children = props.children;
|
390 | var selectedValue = this.state.selectedValue;
|
391 | var itemClassName = "".concat(prefixCls, "-item");
|
392 | var selectedItemClassName = "".concat(itemClassName, " ").concat(prefixCls, "-item-selected");
|
393 |
|
394 | var map = function map(item) {
|
395 | var _item$props = item.props,
|
396 | _item$props$className = _item$props.className,
|
397 | className = _item$props$className === void 0 ? '' : _item$props$className,
|
398 | style = _item$props.style,
|
399 | value = _item$props.value;
|
400 | return React.createElement("div", {
|
401 | style: _extends({}, itemStyle, style),
|
402 | className: "".concat(selectedValue === value ? selectedItemClassName : itemClassName, " ").concat(className),
|
403 | key: value
|
404 | }, item.children || item.props.children);
|
405 | };
|
406 |
|
407 |
|
408 | var items = React.Children ? React.Children.map(children, map) : [].concat(children).map(map);
|
409 | var pickerCls = (_pickerCls = {}, _defineProperty(_pickerCls, props.className, !!props.className), _defineProperty(_pickerCls, prefixCls, true), _pickerCls);
|
410 | return React.createElement("div", {
|
411 | className: (0, _classnames["default"])(pickerCls),
|
412 | ref: function ref(el) {
|
413 | return _this5.rootRef = el;
|
414 | },
|
415 | style: this.props.style
|
416 | }, React.createElement("div", {
|
417 | className: "".concat(prefixCls, "-mask"),
|
418 | ref: function ref(el) {
|
419 | return _this5.maskRef = el;
|
420 | }
|
421 | }), React.createElement("div", {
|
422 | className: "".concat(prefixCls, "-indicator ").concat(indicatorClassName),
|
423 | ref: function ref(el) {
|
424 | return _this5.indicatorRef = el;
|
425 | },
|
426 | style: indicatorStyle
|
427 | }), React.createElement("div", {
|
428 | className: "".concat(prefixCls, "-content"),
|
429 | ref: function ref(el) {
|
430 | return _this5.contentRef = el;
|
431 | }
|
432 | }, items));
|
433 | }
|
434 | }]);
|
435 |
|
436 | return Picker;
|
437 | }(React.Component);
|
438 |
|
439 | Picker.defaultProps = {
|
440 | prefixCls: 'rmc-picker'
|
441 | };
|
442 |
|
443 | var _default = (0, _PickerMixin["default"])(Picker);
|
444 |
|
445 | exports["default"] = _default; |
\ | No newline at end of file |