@nutui/nutui-react-taro 1.3.2-beta.0 → 1.3.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,923 +0,0 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
3
- import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
4
- import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
5
- var _excluded = ["isVisible", "title", "listData", "defaultValueData", "onConfirm", "onClose", "onCloseUpdate", "onChange", "className", "style", "threeDimensional", "swipeDuration"],
6
- _excluded2 = ["minDate", "maxDate", "type", "isShowChinese", "minuteStep", "modelValue", "visible", "title", "formatter", "onCloseDatePicker", "onConfirmDatePicker", "filter", "threeDimensional", "className", "style"];
7
-
8
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
9
-
10
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
11
-
12
- import React__default, { useState, useRef, useEffect, useImperativeHandle } from 'react';
13
- import { u as useRefs, P as Popup } from './useRefs-032cf186.js';
14
- import { u as useTouch } from './useTouch-a2ea68c6.js';
15
- import { u as useConfig } from './configprovider-3e87d67a.js';
16
- import { c as cn } from './bem-893ad28d.js';
17
-
18
- var InternalPickerSlot = function InternalPickerSlot(props, ref) {
19
- var _props$keyIndex = props.keyIndex,
20
- keyIndex = _props$keyIndex === void 0 ? 0 : _props$keyIndex,
21
- defaultValue = props.defaultValue,
22
- _props$listData = props.listData,
23
- listData = _props$listData === void 0 ? [] : _props$listData,
24
- _props$threeDimension = props.threeDimensional,
25
- threeDimensional = _props$threeDimension === void 0 ? true : _props$threeDimension,
26
- _props$swipeDuration = props.swipeDuration,
27
- swipeDuration = _props$swipeDuration === void 0 ? 1000 : _props$swipeDuration,
28
- chooseItem = props.chooseItem;
29
- var touch = useTouch();
30
- var DEFAULT_DURATION = 200; // 触发惯性滑动条件:
31
- // 在手指离开屏幕时,如果和上一次 move 时的间隔小于 `MOMENTUM_TIME` 且 move
32
- // 距离大于 `MOMENTUM_DISTANCE` 时,执行惯性滑动
33
-
34
- var INERTIA_TIME = 300;
35
- var INERTIA_DISTANCE = 15;
36
-
37
- var _useState = useState(1),
38
- _useState2 = _slicedToArray(_useState, 2),
39
- currIndex = _useState2[0],
40
- setCurrIndex = _useState2[1];
41
-
42
- var lineSpacing = 36;
43
-
44
- var _useState3 = useState(0),
45
- _useState4 = _slicedToArray(_useState3, 2),
46
- touchTime = _useState4[0],
47
- setTouchTime = _useState4[1];
48
-
49
- var _useState5 = useState('0deg'),
50
- _useState6 = _slicedToArray(_useState5, 2),
51
- touchDeg = _useState6[0],
52
- setTouchDeg = _useState6[1];
53
-
54
- var rotation = 20;
55
- var moving = useRef(false);
56
- var timer;
57
- useRef(null);
58
- var rollerRef = useRef(null);
59
- var pickerSlotRef = useRef(null);
60
-
61
- var _useState7 = useState(0),
62
- _useState8 = _slicedToArray(_useState7, 2),
63
- startTime = _useState8[0],
64
- setStartTime = _useState8[1];
65
-
66
- var _useState9 = useState(0),
67
- _useState10 = _slicedToArray(_useState9, 2),
68
- startY = _useState10[0],
69
- setStartY = _useState10[1];
70
-
71
- var transformY = useRef(0);
72
-
73
- var _useState11 = useState(0),
74
- _useState12 = _slicedToArray(_useState11, 2),
75
- scrollDistance = _useState12[0],
76
- setScrollDistance = _useState12[1];
77
-
78
- var isHidden = function isHidden(index) {
79
- if (index >= currIndex + 8 || index <= currIndex - 8) {
80
- return true;
81
- }
82
-
83
- return false;
84
- };
85
-
86
- var setTransform = function setTransform() {
87
- var translateY = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
88
- var type = arguments.length > 1 ? arguments[1] : undefined;
89
- var time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_DURATION;
90
- var deg = arguments.length > 3 ? arguments[3] : undefined;
91
- var nTime = time;
92
-
93
- if (type !== 'end') {
94
- nTime = 0;
95
- }
96
-
97
- setTouchTime(nTime);
98
- setTouchDeg(deg);
99
- setScrollDistance(translateY);
100
- };
101
-
102
- var setMove = function setMove(move, type, time) {
103
- var updateMove = move + transformY.current;
104
-
105
- if (type === 'end') {
106
- // 限定滚动距离
107
- if (updateMove > 0) {
108
- updateMove = 0;
109
- }
110
-
111
- if (updateMove < -(listData.length - 1) * lineSpacing) {
112
- updateMove = -(listData.length - 1) * lineSpacing;
113
- } // 设置滚动距离为lineSpacing的倍数值
114
-
115
-
116
- var endMove = Math.round(updateMove / lineSpacing) * lineSpacing;
117
- var deg = "".concat((Math.abs(Math.round(endMove / lineSpacing)) + 1) * rotation, "deg");
118
- setTransform(endMove, type, time, deg);
119
- setCurrIndex(Math.abs(Math.round(endMove / lineSpacing)) + 1);
120
- } else {
121
- var _deg = 0;
122
- var currentDeg = (-updateMove / lineSpacing + 1) * rotation; // picker 滚动的最大角度
123
-
124
- var maxDeg = (listData.length + 1) * rotation;
125
- var minDeg = 0;
126
- _deg = Math.min(Math.max(currentDeg, minDeg), maxDeg);
127
-
128
- if (minDeg < _deg && _deg < maxDeg) {
129
- setTransform(updateMove, '', undefined, "".concat(_deg, "deg"));
130
- setCurrIndex(Math.abs(Math.round(updateMove / lineSpacing)) + 1);
131
- }
132
- }
133
- };
134
-
135
- var setChooseValue = function setChooseValue(move) {
136
- chooseItem && chooseItem(listData === null || listData === void 0 ? void 0 : listData[Math.round(-move / lineSpacing)], keyIndex);
137
- }; // 开始滚动
138
-
139
-
140
- var touchStart = function touchStart(event) {
141
- touch.start(event);
142
- setStartY(touch.deltaY);
143
- setStartTime(Date.now());
144
- transformY.current = scrollDistance;
145
- };
146
-
147
- var touchMove = function touchMove(event) {
148
- touch.move(event);
149
-
150
- if (touch.isVertical) {
151
- moving.current = true;
152
- preventDefault(event, true);
153
- }
154
-
155
- var move = touch.deltaY - startY;
156
- setMove(move);
157
- };
158
-
159
- var touchEnd = function touchEnd(event) {
160
- if (!moving.current) return;
161
- var move = touch.deltaY - startY;
162
- var moveTime = Date.now() - startTime; // 区分是否为惯性滚动
163
-
164
- if (moveTime <= INERTIA_TIME && Math.abs(move) > INERTIA_DISTANCE) {
165
- // 惯性滚动
166
- var distance = momentum(move, moveTime);
167
- setMove(distance, 'end', +swipeDuration);
168
- } else {
169
- setMove(move, 'end');
170
- }
171
-
172
- setTimeout(function () {
173
- // moving.current = false
174
- touch.reset();
175
- }, 0);
176
- }; // 惯性滚动 距离
177
-
178
-
179
- var momentum = function momentum(distance, duration) {
180
- var nDistance = distance; // 惯性滚动的速度
181
-
182
- var speed = Math.abs(nDistance / duration); // 惯性滚动的距离
183
-
184
- nDistance = speed / 0.003 * (nDistance < 0 ? -1 : 1);
185
- return nDistance;
186
- };
187
-
188
- var modifyStatus = function modifyStatus(type, val) {
189
- var value = val || defaultValue;
190
- var index = -1;
191
-
192
- if (value) {
193
- listData.some(function (item, idx) {
194
- if (item.value === value) {
195
- index = idx;
196
- return true;
197
- }
198
-
199
- return false;
200
- });
201
- } else {
202
- listData.forEach(function (item, i) {
203
- if (item.value === defaultValue) {
204
- index = i;
205
- }
206
- });
207
- }
208
-
209
- setCurrIndex(index === -1 ? 1 : index + 1);
210
- var move = index === -1 ? 0 : index * lineSpacing;
211
- type && setChooseValue(-move);
212
- setMove(-move);
213
- }; // 惯性滚动结束
214
-
215
-
216
- var stopMomentum = function stopMomentum() {
217
- moving.current = false;
218
- setTouchTime(0);
219
- setChooseValue(scrollDistance);
220
- }; // 阻止默认事件
221
-
222
-
223
- var preventDefault = function preventDefault(event, isStopPropagation) {
224
- /* istanbul ignore else */
225
- if (typeof event.cancelable !== 'boolean' || event.cancelable) {
226
- event.preventDefault();
227
- }
228
-
229
- if (isStopPropagation) {
230
- event.stopPropagation();
231
- }
232
- };
233
-
234
- var touchRollerStyle = function touchRollerStyle() {
235
- return {
236
- transition: "transform ".concat(touchTime, "ms cubic-bezier(0.17, 0.89, 0.45, 1)"),
237
- transform: "rotate3d(1, 0, 0, ".concat(touchDeg, ")")
238
- };
239
- };
240
-
241
- var touchTileStyle = function touchTileStyle() {
242
- return {
243
- transition: "transform ".concat(touchTime, "ms cubic-bezier(0.17, 0.89, 0.45, 1)"),
244
- transform: "translate3d(0, ".concat(scrollDistance, "px, 0)")
245
- };
246
- };
247
-
248
- useEffect(function () {
249
- setScrollDistance(0);
250
- transformY.current = 0;
251
- modifyStatus(false);
252
- return function () {
253
- clearTimeout(timer);
254
- };
255
- }, [listData]);
256
- useImperativeHandle(ref, function () {
257
- return {
258
- stopMomentum: stopMomentum,
259
- moving: moving.current
260
- };
261
- });
262
- return React__default.createElement("div", {
263
- className: "nut-picker-list",
264
- ref: pickerSlotRef,
265
- onTouchStart: touchStart,
266
- onTouchMove: touchMove,
267
- onTouchEnd: touchEnd
268
- }, React__default.createElement("div", {
269
- className: "nut-picker-roller",
270
- ref: rollerRef,
271
- style: threeDimensional ? touchRollerStyle() : touchTileStyle(),
272
- onTransitionEnd: stopMomentum
273
- }, threeDimensional && listData.map(function (item, index) {
274
- return React__default.createElement("div", {
275
- className: "nut-picker-roller-item ".concat(isHidden(index + 1) && 'nut-picker-roller-item-hidden'),
276
- style: {
277
- transform: "rotate3d(1, 0, 0, ".concat(-rotation * (index + 1), "deg) translate3d(0px, 0px, 104px)")
278
- },
279
- key: item.value ? item.value : index
280
- }, React__default.createElement(React__default.Fragment, null, item.text ? item.text : item));
281
- }), !threeDimensional && listData.map(function (item, index) {
282
- return React__default.createElement("div", {
283
- className: "nut-picker-roller-item-tile",
284
- key: item.value ? item.value : index
285
- }, React__default.createElement(React__default.Fragment, null, item.text ? item.text : item));
286
- })), React__default.createElement("div", {
287
- className: "nut-picker-mask"
288
- }), React__default.createElement("div", {
289
- className: "nut-picker-indicator"
290
- }));
291
- };
292
-
293
- var PickerSlot = React__default.forwardRef(InternalPickerSlot);
294
-
295
- var InternalPicker = function InternalPicker(props, ref) {
296
- var _useConfig = useConfig(),
297
- locale = _useConfig.locale;
298
-
299
- var isVisible = props.isVisible,
300
- title = props.title,
301
- _props$listData2 = props.listData,
302
- listData = _props$listData2 === void 0 ? [] : _props$listData2,
303
- defaultValueData = props.defaultValueData,
304
- onConfirm = props.onConfirm,
305
- onClose = props.onClose,
306
- onCloseUpdate = props.onCloseUpdate,
307
- onChange = props.onChange,
308
- className = props.className,
309
- style = props.style,
310
- threeDimensional = props.threeDimensional,
311
- swipeDuration = props.swipeDuration,
312
- rest = _objectWithoutProperties(props, _excluded);
313
-
314
- var _useState13 = useState([]),
315
- _useState14 = _slicedToArray(_useState13, 2),
316
- chooseValueData = _useState14[0],
317
- setchooseValueData = _useState14[1]; // 选择的数据的 value 值, 每一条数据的 value 值
318
-
319
-
320
- var _useState15 = useState(0),
321
- _useState16 = _slicedToArray(_useState15, 2),
322
- columnIndex = _useState16[0],
323
- setcolumnIndex = _useState16[1]; // 选中列
324
-
325
-
326
- var pickerRef = useRef(null);
327
-
328
- var _useRefs = useRefs(),
329
- _useRefs2 = _slicedToArray(_useRefs, 2),
330
- refs = _useRefs2[0],
331
- setRefs = _useRefs2[1];
332
-
333
- var _useState17 = useState([]),
334
- _useState18 = _slicedToArray(_useState17, 2),
335
- columnsList = _useState18[0],
336
- setColumnsList = _useState18[1]; // 格式化后每一列的数据
337
-
338
-
339
- var b = cn('picker');
340
- var isConfirmEvent = useRef(false); // 默认值修改
341
-
342
- useEffect(function () {
343
- if (defaultValueData && defaultValueData.length !== 0 && defaultValueData.toString() !== chooseValueData.toString()) {
344
- var data = _toConsumableArray(defaultValueData);
345
-
346
- setchooseValueData(data);
347
- setColumnsList(normalListData());
348
- }
349
- }, [defaultValueData]); // 选中值进行修改
350
-
351
- useEffect(function () {
352
- console.log('修改');
353
- onChange && onChange(columnIndex, chooseValueData, selectedOptions());
354
-
355
- if (isConfirmEvent.current) {
356
- isConfirmEvent.current = false;
357
- onConfirm && onConfirm(chooseValueData, selectedOptions());
358
- }
359
- }, [chooseValueData]); // 列表格式修改
360
-
361
- useEffect(function () {
362
- init();
363
- }, [listData]);
364
-
365
- var closeActionSheet = function closeActionSheet() {
366
- onClose && onClose(chooseValueData, selectedOptions());
367
- onCloseUpdate && onCloseUpdate(chooseValueData, selectedOptions(), pickerRef);
368
- }; // 点击确定
369
-
370
-
371
- var confirm = function confirm() {
372
- var movings = false;
373
- refs.forEach(function (_ref) {
374
- if (_ref.moving) movings = true;
375
-
376
- _ref.stopMomentum();
377
- });
378
-
379
- if (movings) {
380
- isConfirmEvent.current = true;
381
- } else {
382
- onConfirm && onConfirm(chooseValueData, selectedOptions());
383
- }
384
-
385
- onClose && onClose(chooseValueData, selectedOptions());
386
- setTimeout(function () {
387
- isConfirmEvent.current = false;
388
- }, 0);
389
- };
390
-
391
- var selectedOptions = function selectedOptions() {
392
- var optins = [];
393
- columnsList.map(function (column, index) {
394
- var currOptions = [];
395
- currOptions = column.filter(function (item) {
396
- return item.value === chooseValueData[index];
397
- });
398
-
399
- if (currOptions[0]) {
400
- optins.push(currOptions[0]);
401
- } else {
402
- column[0] && optins.push(column[0]);
403
- }
404
-
405
- return column;
406
- });
407
- return optins;
408
- }; // 选择每一列的数据
409
-
410
-
411
- var _chooseItem = function chooseItem(option, columnIndex) {
412
- if (option && Object.keys(option).length) {
413
- // 是否移动后是否与之前有差异
414
- if (chooseValueData[columnIndex] !== option.value) {
415
- if (columnsType() === 'cascade') {
416
- chooseValueData[columnIndex] = option.value ? option.value : '';
417
- setchooseValueData(_toConsumableArray(chooseValueData));
418
- var index = columnIndex;
419
- var cursor = option;
420
-
421
- while (cursor && cursor.children && cursor.children[0]) {
422
- chooseValueData[index + 1] = cursor.children[0].value;
423
- setchooseValueData(_toConsumableArray(chooseValueData));
424
- index++;
425
- var cc = cursor.children[0];
426
- cursor = cc;
427
- } // 当前改变列的下一列 children 值为空
428
-
429
-
430
- if (cursor && cursor.children) {
431
- chooseValueData[index + 1] = '';
432
- setchooseValueData(_toConsumableArray(chooseValueData));
433
- }
434
-
435
- setColumnsList(normalListData());
436
- } else {
437
- setchooseValueData(function (data) {
438
- var cdata = _toConsumableArray(data);
439
-
440
- cdata[columnIndex] = Object.prototype.hasOwnProperty.call(option, 'value') ? option.value : '';
441
- return cdata;
442
- });
443
- }
444
-
445
- setcolumnIndex(columnIndex);
446
- }
447
- }
448
- }; // 传入的数据格式化
449
-
450
-
451
- var normalListData = function normalListData() {
452
- var type = columnsType();
453
-
454
- switch (type) {
455
- case 'multiple':
456
- return listData;
457
-
458
- case 'cascade':
459
- // 级联数据处理
460
- return formatCascade(listData, chooseValueData);
461
-
462
- default:
463
- return [listData];
464
- }
465
- }; // 每一列的类型
466
-
467
-
468
- var columnsType = function columnsType() {
469
- var firstColumn = listData[0];
470
-
471
- if (firstColumn) {
472
- if (Array.isArray(firstColumn)) {
473
- return 'multiple';
474
- }
475
-
476
- if ('children' in firstColumn) {
477
- return 'cascade';
478
- }
479
- }
480
-
481
- return 'single';
482
- }; // 级联数据格式化
483
-
484
-
485
- var formatCascade = function formatCascade(columns, defaultValues) {
486
- var formatted = [];
487
- var cursor = {
488
- text: '',
489
- value: '',
490
- children: columns
491
- };
492
- var columnIndex = 0;
493
-
494
- var _loop = function _loop() {
495
- var options = cursor.children;
496
- var value = defaultValues[columnIndex];
497
- var index = options.findIndex(function (columnItem) {
498
- return columnItem.value === value;
499
- });
500
- if (index === -1) index = 0;
501
- cursor = cursor.children[index];
502
- columnIndex++;
503
- formatted.push(options);
504
- };
505
-
506
- while (cursor && cursor.children) {
507
- _loop();
508
- }
509
-
510
- return formatted;
511
- };
512
-
513
- var init = function init() {
514
- var data = [];
515
- var normalData = normalListData();
516
- setColumnsList(normalData);
517
- normalData.length > 0 && normalData.map(function (item) {
518
- item[0] && data.push(item[0].value);
519
- return item;
520
- });
521
-
522
- if (!defaultValueData && chooseValueData.length === 0) {
523
- setchooseValueData([].concat(data));
524
- }
525
- };
526
-
527
- var renderToolbar = function renderToolbar() {
528
- return React__default.createElement("div", {
529
- className: b('control')
530
- }, React__default.createElement("span", {
531
- className: b('cancel-btn'),
532
- onClick: function onClick() {
533
- return closeActionSheet();
534
- }
535
- }, locale.cancel), React__default.createElement("div", {
536
- className: b('title')
537
- }, title || ''), React__default.createElement("span", {
538
- className: b('confirm-btn'),
539
- onClick: confirm
540
- }, locale.confirm));
541
- };
542
-
543
- return React__default.createElement(Popup, {
544
- visible: isVisible,
545
- position: "bottom",
546
- onClose: function onClose() {
547
- closeActionSheet();
548
- }
549
- }, React__default.createElement("div", _objectSpread({
550
- className: "".concat(b(), " ").concat(className || ''),
551
- style: style
552
- }, rest), renderToolbar(), React__default.createElement("div", {
553
- className: b('panel'),
554
- ref: pickerRef
555
- }, columnsList === null || columnsList === void 0 ? void 0 : columnsList.map(function (item, index) {
556
- return React__default.createElement(PickerSlot, {
557
- ref: setRefs(index),
558
- defaultValue: chooseValueData === null || chooseValueData === void 0 ? void 0 : chooseValueData[index],
559
- listData: item,
560
- threeDimensional: threeDimensional,
561
- chooseItem: function chooseItem(value, index) {
562
- return _chooseItem(value, index);
563
- },
564
- swipeDuration: swipeDuration,
565
- key: index,
566
- keyIndex: index
567
- });
568
- }))));
569
- };
570
-
571
- var Picker = React__default.forwardRef(InternalPicker);
572
- var currentYear = new Date().getFullYear();
573
- var defaultProps = {
574
- modelValue: null,
575
- visible: false,
576
- title: '',
577
- type: 'date',
578
- isShowChinese: false,
579
- threeDimensional: true,
580
- minuteStep: 1,
581
- minDate: new Date(currentYear - 10, 0, 1),
582
- maxDate: new Date(currentYear + 10, 11, 31)
583
- };
584
-
585
- var DatePicker = function DatePicker(props) {
586
- var _defaultProps$props = _objectSpread(_objectSpread({}, defaultProps), props),
587
- minDate = _defaultProps$props.minDate,
588
- maxDate = _defaultProps$props.maxDate,
589
- type = _defaultProps$props.type,
590
- isShowChinese = _defaultProps$props.isShowChinese,
591
- minuteStep = _defaultProps$props.minuteStep,
592
- modelValue = _defaultProps$props.modelValue,
593
- visible = _defaultProps$props.visible,
594
- title = _defaultProps$props.title,
595
- formatter = _defaultProps$props.formatter,
596
- onCloseDatePicker = _defaultProps$props.onCloseDatePicker,
597
- onConfirmDatePicker = _defaultProps$props.onConfirmDatePicker,
598
- filter = _defaultProps$props.filter,
599
- threeDimensional = _defaultProps$props.threeDimensional,
600
- className = _defaultProps$props.className,
601
- style = _defaultProps$props.style,
602
- rest = _objectWithoutProperties(_defaultProps$props, _excluded2);
603
-
604
- var _useState19 = useState(false),
605
- _useState20 = _slicedToArray(_useState19, 2),
606
- show = _useState20[0],
607
- setShow = _useState20[1];
608
-
609
- var _useState21 = useState(modelValue),
610
- _useState22 = _slicedToArray(_useState21, 2),
611
- currentDate = _useState22[0],
612
- setCurrentDate = _useState22[1];
613
-
614
- var _useState23 = useState([]),
615
- _useState24 = _slicedToArray(_useState23, 2),
616
- defaultValue = _useState24[0],
617
- setDefaultValue = _useState24[1];
618
-
619
- var _useState25 = useState([]),
620
- _useState26 = _slicedToArray(_useState25, 2),
621
- listData = _useState26[0],
622
- setListData = _useState26[1];
623
-
624
- var pickerRef = useRef(null);
625
-
626
- var isDate = function isDate(val) {
627
- return Object.prototype.toString.call(val) === '[object Date]' && !Number.isNaN(val.getTime());
628
- };
629
-
630
- var zhCNType = {
631
- day: '日',
632
- year: '年',
633
- month: '月',
634
- hour: '时',
635
- minute: '分',
636
- seconds: '秒'
637
- };
638
-
639
- var formatValue = function formatValue(value) {
640
- var cvalue = value;
641
-
642
- if (!cvalue || cvalue && !isDate(cvalue)) {
643
- cvalue = minDate;
644
- }
645
-
646
- var timestmp = Math.max(cvalue.getTime(), minDate.getTime());
647
- timestmp = Math.min(timestmp, maxDate.getTime());
648
- return new Date(timestmp);
649
- };
650
-
651
- function getMonthEndDay(year, month) {
652
- return new Date(year, month, 0).getDate();
653
- }
654
-
655
- var getBoundary = function getBoundary(type, value) {
656
- var _ref2;
657
-
658
- var boundary = type === 'min' ? minDate : maxDate;
659
- var year = boundary.getFullYear();
660
- var month = 1;
661
- var date = 1;
662
- var hour = 0;
663
- var minute = 0;
664
-
665
- if (type === 'max') {
666
- month = 12;
667
- date = getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
668
- hour = 23;
669
- minute = 59;
670
- }
671
-
672
- var seconds = minute;
673
-
674
- if (value.getFullYear() === year) {
675
- month = boundary.getMonth() + 1;
676
-
677
- if (value.getMonth() + 1 === month) {
678
- date = boundary.getDate();
679
-
680
- if (value.getDate() === date) {
681
- hour = boundary.getHours();
682
-
683
- if (value.getHours() === hour) {
684
- minute = boundary.getMinutes();
685
- }
686
- }
687
- }
688
- }
689
-
690
- return _ref2 = {}, _defineProperty(_ref2, "".concat(type, "Year"), year), _defineProperty(_ref2, "".concat(type, "Month"), month), _defineProperty(_ref2, "".concat(type, "Date"), date), _defineProperty(_ref2, "".concat(type, "Hour"), hour), _defineProperty(_ref2, "".concat(type, "Minute"), minute), _defineProperty(_ref2, "".concat(type, "Seconds"), seconds), _ref2;
691
- };
692
-
693
- var ranges = function ranges(date) {
694
- var curDate = date || currentDate;
695
- console.log(11, currentDate);
696
- if (!curDate) return [];
697
-
698
- var _getBoundary = getBoundary('max', curDate),
699
- maxYear = _getBoundary.maxYear,
700
- maxDate = _getBoundary.maxDate,
701
- maxMonth = _getBoundary.maxMonth,
702
- maxHour = _getBoundary.maxHour,
703
- maxMinute = _getBoundary.maxMinute,
704
- maxSeconds = _getBoundary.maxSeconds;
705
-
706
- var _getBoundary2 = getBoundary('min', curDate),
707
- minYear = _getBoundary2.minYear,
708
- minDate = _getBoundary2.minDate,
709
- minMonth = _getBoundary2.minMonth,
710
- minHour = _getBoundary2.minHour,
711
- minMinute = _getBoundary2.minMinute,
712
- minSeconds = _getBoundary2.minSeconds;
713
-
714
- var result = [{
715
- type: 'year',
716
- range: [minYear, maxYear]
717
- }, {
718
- type: 'month',
719
- range: [minMonth, maxMonth]
720
- }, {
721
- type: 'day',
722
- range: [minDate, maxDate]
723
- }, {
724
- type: 'hour',
725
- range: [minHour, maxHour]
726
- }, {
727
- type: 'minute',
728
- range: [minMinute, maxMinute]
729
- }, {
730
- type: 'seconds',
731
- range: [minSeconds, maxSeconds]
732
- }];
733
-
734
- switch (type.toLocaleLowerCase()) {
735
- case 'date':
736
- result = result.slice(0, 3);
737
- break;
738
-
739
- case 'datetime':
740
- result = result.slice(0, 5);
741
- break;
742
-
743
- case 'time':
744
- result = result.slice(3, 6);
745
- break;
746
-
747
- case 'year-month':
748
- result = result.slice(0, 2);
749
- break;
750
-
751
- case 'month-day':
752
- result = result.slice(1, 3);
753
- break;
754
-
755
- case 'datehour':
756
- result = result.slice(0, 4);
757
- break;
758
- }
759
-
760
- return result;
761
- };
762
-
763
- var updateChooseValueCustmer = function updateChooseValueCustmer(index, selectedValue, cacheValueData) {
764
- if (['date', 'datetime', 'datehour', 'month-day', 'year-month'].includes(type.toLocaleLowerCase())) {
765
- var _date;
766
-
767
- var formatDate = [];
768
- selectedValue.forEach(function (item) {
769
- formatDate.push(item);
770
- });
771
-
772
- if (type.toLocaleLowerCase() === 'month-day' && formatDate.length < 3) {
773
- formatDate.unshift(new Date(modelValue || minDate || maxDate).getFullYear());
774
- }
775
-
776
- if (type.toLocaleLowerCase() === 'year-month' && formatDate.length < 3) {
777
- formatDate.push(new Date(modelValue || minDate || maxDate).getDate());
778
- }
779
-
780
- var year = Number(formatDate[0]);
781
- var month = Number(formatDate[1]) - 1;
782
- var day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1])));
783
- var date = null;
784
-
785
- if (type.toLocaleLowerCase() === 'date' || type.toLocaleLowerCase() === 'month-day' || type.toLocaleLowerCase() === 'year-month') {
786
- date = new Date(year, month, day);
787
- } else if (type.toLocaleLowerCase() === 'datetime') {
788
- date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4]));
789
- } else if (type.toLocaleLowerCase() === 'datehour') {
790
- date = new Date(year, month, day, Number(formatDate[3]));
791
- }
792
-
793
- var isEqual = (currentDate === null || currentDate === void 0 ? void 0 : currentDate.getTime()) === ((_date = date) === null || _date === void 0 ? void 0 : _date.getTime());
794
- date && isDate(date) && !isEqual && setCurrentDate(formatValue(date));
795
- }
796
-
797
- props.onChangeDatePicker && props.onChangeDatePicker(index, selectedValue, cacheValueData);
798
- };
799
-
800
- var padZero = function padZero(num) {
801
- var targetLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
802
- var str = "".concat(num);
803
-
804
- while (str.length < targetLength) {
805
- str = "0".concat(str);
806
- }
807
-
808
- return str;
809
- };
810
-
811
- var formatterOption = function formatterOption(type, value) {
812
- var fOption = null;
813
-
814
- if (formatter) {
815
- fOption = formatter(type, {
816
- text: padZero(value, 2),
817
- value: padZero(value, 2)
818
- });
819
- } else {
820
- var padMin = padZero(value, 2);
821
- var fatter = isShowChinese ? zhCNType[type] : '';
822
- fOption = {
823
- text: padMin + fatter,
824
- value: padMin
825
- };
826
- }
827
-
828
- return fOption;
829
- };
830
-
831
- var generateValue = function generateValue(min, max, val, type, columnIndex) {
832
- var cmin = min;
833
- var arr = [];
834
- var index = 0;
835
-
836
- while (cmin <= max) {
837
- arr.push(formatterOption(type, cmin));
838
-
839
- if (type === 'minute') {
840
- cmin += minuteStep;
841
- } else {
842
- cmin++;
843
- }
844
-
845
- if (cmin <= val) {
846
- index++;
847
- }
848
- }
849
-
850
- defaultValue[columnIndex] = arr[index].value;
851
- setDefaultValue(_toConsumableArray(defaultValue));
852
-
853
- if (props.filter && props.filter(type, arr)) {
854
- return props.filter(type, arr);
855
- }
856
-
857
- return arr;
858
- };
859
-
860
- var getDateIndex = function getDateIndex(type) {
861
- if (!currentDate) return 0;
862
- var d = 0;
863
-
864
- if (type === 'year') {
865
- d = currentDate.getFullYear();
866
- } else if (type === 'month') {
867
- d = currentDate.getMonth() + 1;
868
- } else if (type === 'day') {
869
- d = currentDate.getDate();
870
- } else if (type === 'hour') {
871
- d = currentDate.getHours();
872
- } else if (type === 'minute') {
873
- d = currentDate.getMinutes();
874
- } else if (type === 'seconds') {
875
- d = currentDate.getSeconds();
876
- }
877
-
878
- return d;
879
- };
880
-
881
- var columns = function columns(date) {
882
- var val = ranges(date).map(function (res, columnIndex) {
883
- return generateValue(res.range[0], res.range[1], getDateIndex(res.type), res.type, columnIndex);
884
- });
885
- return val || [];
886
- };
887
-
888
- useEffect(function () {
889
- setCurrentDate(formatValue(modelValue)); // initDefault()
890
- }, []);
891
- useEffect(function () {
892
- setCurrentDate(formatValue(modelValue));
893
- }, [modelValue]);
894
- useEffect(function () {
895
- setShow(visible);
896
- }, [visible]);
897
- useEffect(function () {
898
- if (currentDate) {
899
- setListData(columns());
900
- }
901
- }, [currentDate]);
902
- return React__default.createElement("div", _objectSpread({
903
- className: "nut-datepicker ".concat(className || ''),
904
- style: style
905
- }, rest), listData.length > 0 && React__default.createElement(Picker, {
906
- isVisible: show,
907
- listData: listData,
908
- onClose: onCloseDatePicker,
909
- defaultValueData: defaultValue,
910
- onConfirm: function onConfirm(values, options) {
911
- return onConfirmDatePicker && onConfirmDatePicker(values, options);
912
- },
913
- onChange: function onChange(index, value, list) {
914
- return updateChooseValueCustmer(index, value, list);
915
- },
916
- threeDimensional: threeDimensional,
917
- ref: pickerRef
918
- }));
919
- };
920
-
921
- DatePicker.defaultProps = defaultProps;
922
- DatePicker.displayName = 'NutDatePicker';
923
- export { DatePicker as D };