hy-app 0.5.13 → 0.5.15

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.
Files changed (34) hide show
  1. package/components/hy-address-picker/hy-address-picker.vue +0 -1
  2. package/components/hy-button/hy-button.vue +7 -1
  3. package/components/hy-button/index.scss +25 -21
  4. package/components/hy-cell-item/hy-cell-item.vue +161 -161
  5. package/components/hy-cell-item/props.ts +59 -66
  6. package/components/hy-code-input/hy-code-input.vue +231 -231
  7. package/components/hy-code-input/props.ts +90 -88
  8. package/components/hy-config-provider/index.scss +4 -7
  9. package/components/hy-datetime-picker/hy-datetime-picker.vue +519 -521
  10. package/components/hy-folding-panel/hy-folding-panel-group.vue +163 -0
  11. package/components/hy-icon/index.scss +1 -2
  12. package/components/hy-index-bar/hy-index-bar.vue +185 -0
  13. package/components/hy-index-bar/index.scss +65 -0
  14. package/components/hy-index-bar/props.ts +94 -0
  15. package/components/hy-index-bar/typing.d.ts +36 -0
  16. package/components/hy-notify/hy-notify.vue +174 -174
  17. package/components/hy-number-step/hy-number-step.vue +367 -367
  18. package/components/hy-number-step/index.scss +14 -5
  19. package/components/hy-picker/hy-picker.vue +0 -1
  20. package/components/hy-picker/index.scss +1 -1
  21. package/components/hy-qrcode/qrcode.js.bak +1434 -0
  22. package/components/hy-table/hy-table.vue +30 -13
  23. package/components/hy-table/index.scss +6 -1
  24. package/components/hy-table/props.ts +1 -1
  25. package/components/hy-tabs/index.scss +1 -0
  26. package/components/hy-tag/index.scss +4 -13
  27. package/components/hy-text/hy-text.vue +1 -1
  28. package/components/hy-textarea/hy-textarea.vue +1 -1
  29. package/components/hy-textarea/index.scss +1 -4
  30. package/global.d.ts +92 -91
  31. package/libs/css/iconfont.css +117 -119
  32. package/libs/utils/utils.ts +502 -521
  33. package/package.json +2 -2
  34. package/web-types.json +1 -1
@@ -1,521 +1,519 @@
1
- <template>
2
- <view :class="['hy-datetime-picker', customClass]" :style="customStyle">
3
- <view v-if="hasInput" class="hy-datetime-picker__has-input" @click="onShowByClickInput">
4
- <slot name="trigger" :value="inputValue">
5
- <HyInput
6
- v-model="inputValue"
7
- :readonly="true"
8
- :disabled="input?.disabled"
9
- :disabledColor="input?.disabledColor"
10
- :shape="input?.shape"
11
- :border="input?.border"
12
- :prefixIcon="input?.prefixIcon"
13
- :suffixIcon="input?.suffixIcon"
14
- :color="input?.color"
15
- :fontSize="input?.fontSize"
16
- :inputAlign="input?.inputAlign"
17
- :placeholder="input?.placeholder || '请选择时间'"
18
- :placeholderStyle="input?.placeholderStyle"
19
- :placeholderClass="input?.placeholderClass"
20
- :customStyle="Object.assign({ 'pointer-events': 'none' }, input?.customStyle)"
21
- ></HyInput>
22
- <view class="input-cover"></view>
23
- </slot>
24
- </view>
25
- <HyPicker
26
- ref="picker"
27
- :show="show || (hasInput && showByClickInput)"
28
- :popupMode="popupMode"
29
- :closeOnClickOverlay="closeOnClickOverlay"
30
- :columns="columns"
31
- :title="title"
32
- :itemHeight="itemHeight"
33
- :loading="loading"
34
- :showToolbar="showToolbar"
35
- :visibleItemCount="visibleItemCount"
36
- :defaultIndex="innerDefaultIndex"
37
- :cancelText="cancelText"
38
- :cancelColor="cancelColor"
39
- :confirmColor="confirmColor"
40
- :toolbarRightSlot="toolbarRightSlot"
41
- @close="close"
42
- @cancel="cancel"
43
- @confirm="confirm"
44
- @change="change"
45
- >
46
- <template #toolbar-right>
47
- <slot name="toolbar-right">
48
- {{ confirmText }}
49
- </slot>
50
- </template>
51
- <template #toolbar-bottom>
52
- <slot name="toolbar-bottom"></slot>
53
- </template>
54
- </HyPicker>
55
- </view>
56
- </template>
57
-
58
- <script lang="ts">
59
- export default {
60
- name: 'hy-datetime-picker',
61
- options: {
62
- addGlobalClass: true,
63
- virtualHost: true,
64
- styleIsolation: 'shared'
65
- }
66
- }
67
- </script>
68
-
69
- <script setup lang="ts">
70
- import { computed, onMounted, ref, watch } from 'vue'
71
- import dayjs from 'dayjs/esm'
72
- import { error, padZero, DateModeEnum } from '../../libs'
73
- import type { IDatetimePickerEmits } from './typing'
74
- import datetimePickerProps from './props'
75
- // 组件
76
- import HyInput from '../hy-input/hy-input.vue'
77
- import HyPicker from '../hy-picker/hy-picker.vue'
78
-
79
- /**
80
- * 此选择器用于时间日期选择
81
- * @displayName hy-datetime-picker
82
- */
83
- defineOptions({})
84
-
85
- const props = defineProps(datetimePickerProps)
86
- const emit = defineEmits<IDatetimePickerEmits>()
87
-
88
- // 原来的日期选择器不方便,这里增加一个hasInput选项支持类似element的自带输入框的功能。
89
- const inputValue = ref<string | number>('') // 表单显示值
90
- const innerValue = ref<string | number>('') // 表单显示值
91
- const showByClickInput = ref<boolean>(false) // 是否在hasInput模式下显示日期选择弹唱
92
- const columns = ref<any[]>([])
93
- const innerDefaultIndex = ref<number[]>([])
94
- let innerFormatter = (type: any, value: any) => value
95
- const validModes = new Set([
96
- DateModeEnum.TIME,
97
- DateModeEnum.MONTH_DAY,
98
- DateModeEnum.HOUR_MINUTE,
99
- DateModeEnum.MINUTE_SECOND
100
- ])
101
-
102
- /**
103
- * @description 更新各列的值
104
- * */
105
- const updateColumns = () => {
106
- const formatterFn = props.formatter || innerFormatter
107
- // 获取各列的值,并且map后,对各列的具体值进行补0操作
108
- columns.value = getOriginColumns().map((column) =>
109
- column.values.map((value) => formatterFn(column.type, value))
110
- )
111
- }
112
-
113
- /**
114
- * @description 更新各列的值,进行补0、格式化等操作
115
- * */
116
- const updateColumnValue = (value: string | number) => {
117
- innerValue.value = value
118
- updateColumns()
119
- // 延迟执行,等待u-picker组件列数据更新完后再设置选中值索引
120
- setTimeout(() => {
121
- updateIndexes(value)
122
- }, 100)
123
- }
124
-
125
- const init = () => {
126
- // 获取当前值
127
- innerValue.value = correctValue(props.modelValue)
128
- // 更新列表
129
- updateColumnValue(innerValue.value)
130
-
131
- // 初始化hasInput展示
132
- getInputValue(innerValue.value)
133
- }
134
-
135
- watch(
136
- () => props.show,
137
- (newValue) => {
138
- if (newValue) {
139
- updateColumnValue(innerValue.value)
140
- }
141
- }
142
- )
143
-
144
- watch(
145
- () => props.modelValue,
146
- () => init()
147
- )
148
-
149
- const propsChange = computed(() => {
150
- return [
151
- props.mode,
152
- props.maxDate,
153
- props.minDate,
154
- props.minHour,
155
- props.maxHour,
156
- props.minMinute,
157
- props.maxMinute,
158
- props.filter
159
- ]
160
- })
161
-
162
- watch(
163
- () => propsChange.value,
164
- () => init()
165
- )
166
-
167
- onMounted(() => {
168
- init()
169
- })
170
-
171
- const getInputValue = (newValue: string | number) => {
172
- if (newValue == '' || !newValue) {
173
- inputValue.value = ''
174
- return
175
- }
176
- if (props.mode === 'time') {
177
- inputValue.value = newValue
178
- } else {
179
- if (props.format) {
180
- inputValue.value = dayjs(newValue).format(props.format)
181
- } else {
182
- let format = ''
183
- switch (props.mode) {
184
- case DateModeEnum.DATE:
185
- format = 'YYYY-MM-DD'
186
- break
187
- case DateModeEnum.YEAR_MONTH:
188
- format = 'YYYY-MM'
189
- break
190
- case DateModeEnum.DATETIME:
191
- format = 'YYYY-MM-DD HH:mm:ss'
192
- break
193
- case DateModeEnum.MONTH_DAY:
194
- format = 'MM-DD'
195
- break
196
- case DateModeEnum.HOUR_MINUTE:
197
- format = 'HH:mm'
198
- break
199
- case DateModeEnum.MINUTE_SECOND:
200
- format = 'mm:ss'
201
- break
202
- default:
203
- break
204
- }
205
- inputValue.value = dayjs(newValue).isValid() ? dayjs(newValue).format(format) : newValue
206
- }
207
- }
208
- }
209
- const times = (n: number, iteratee: Function) => {
210
- let index = -1
211
- const result = Array(n < 0 ? 0 : n)
212
- while (++index < n) {
213
- result[index] = iteratee(index)
214
- }
215
- return result
216
- }
217
-
218
- /**
219
- * @description 关闭选择器
220
- * */
221
- const close = () => {
222
- if (props.closeOnClickOverlay) {
223
- if (props.hasInput) {
224
- showByClickInput.value = false
225
- }
226
- emit('close')
227
- }
228
- }
229
-
230
- /**
231
- * @description 点击工具栏的取消按钮
232
- * */
233
- const cancel = () => {
234
- if (props.hasInput) {
235
- showByClickInput.value = false
236
- }
237
- emit('cancel')
238
- }
239
-
240
- /**
241
- * @description 点击工具栏的确定按钮
242
- * */
243
- const confirm = () => {
244
- emit('update:modelValue', innerValue.value)
245
- if (props.hasInput) {
246
- getInputValue(innerValue.value)
247
- showByClickInput.value = false
248
- }
249
- emit('confirm', {
250
- value: innerValue.value,
251
- mode: props.mode
252
- })
253
- }
254
-
255
- /**
256
- * @description 用正则截取输出值,当出现多组数字时,抛出错误
257
- * */
258
- const intercept = (e: any, type?: string) => {
259
- let judge = e.match(/\d+/g)
260
- //判断是否掺杂数字
261
- if (judge.length > 1) {
262
- error('请勿在过滤或格式化函数时添加数字')
263
- return 0
264
- } else if (type && judge[0].length == 4) {
265
- //判断是否是年份
266
- return judge[0]
267
- } else if (judge[0].length > 2) {
268
- error('请勿在过滤或格式化函数时添加数字')
269
- return 0
270
- } else {
271
- return judge[0]
272
- }
273
- }
274
-
275
- /**
276
- * @description 列发生变化时触发
277
- * */
278
- const change = (e: any) => {
279
- const { indexs, values } = e
280
- let selectValue: string | number = ''
281
- if (validModes.has(props.mode) && props.mode !== DateModeEnum.MONTH_DAY) {
282
- // 根据value各列索引,从各列数组中,取出当前时间的选中值
283
- selectValue = `${intercept(values[0][indexs[0]])}:${intercept(values[1][indexs[1]])}`
284
- } else if (props.mode === DateModeEnum.MONTH_DAY) {
285
- // 根据value各列索引,从各列数组中,取出当前时间的选中值
286
- selectValue = `${intercept(values[0][indexs[0]])}-${intercept(values[1][indexs[1]])}`
287
- } else {
288
- // 将选择的值转为数值,比如'03'转为数值的3,'2019'转为数值的2019
289
- const year = parseInt(intercept(values[0][indexs[0]], 'year'))
290
- const month = parseInt(intercept(values[1][indexs[1]]))
291
- let date = parseInt(values[2] ? intercept(values[2][indexs[2]]) : 1)
292
- let hour = 0,
293
- minute = 0,
294
- second = 0
295
- // 此月份的最大天数
296
- const maxDate = dayjs(`${year}-${month}`).daysInMonth()
297
- // 不允许超过maxDate值
298
- date = Math.min(maxDate, date)
299
- if (props.mode === DateModeEnum.DATETIME) {
300
- hour = parseInt(intercept(values[3][indexs[3]]))
301
- minute = parseInt(intercept(values[4][indexs[4]]))
302
- second = parseInt(intercept(values[5][indexs[5]]))
303
- }
304
- // 转为时间模式
305
- selectValue = Number(new Date(year, month - 1, date, hour, minute, second))
306
- }
307
- // 取出准确的合法值,防止超越边界的情况
308
- selectValue = correctValue(selectValue)
309
- innerValue.value = selectValue
310
- updateColumnValue(selectValue)
311
- // 发出change时间,value为当前选中的时间戳
312
- emit('change', {
313
- value: selectValue,
314
- mode: props.mode
315
- })
316
- }
317
-
318
- /**
319
- * @description 更新索引
320
- * */
321
- const updateIndexes = (value: number | string) => {
322
- let values: string[] = []
323
- let timeArr: string[] = []
324
- const formatterFn = props.formatter || innerFormatter
325
-
326
- switch (props.mode) {
327
- case DateModeEnum.TIME:
328
- timeArr = value.toString().split(':')
329
- // 使用formatter格式化方法进行管道处理
330
- values = [formatterFn('hour', timeArr[0]), formatterFn('minute', timeArr[1])]
331
- break
332
- case DateModeEnum.MONTH_DAY:
333
- timeArr = value.toString().split('-')
334
- // 使用formatter格式化方法进行管道处理
335
- values = [formatterFn('month', timeArr[0]), formatterFn('day', timeArr[1])]
336
- break
337
- case DateModeEnum.HOUR_MINUTE:
338
- timeArr = value.toString().split(':')
339
- // 使用formatter格式化方法进行管道处理
340
- values = [formatterFn('hour', timeArr[0]), formatterFn('minute', timeArr[1])]
341
- break
342
- case DateModeEnum.MINUTE_SECOND:
343
- timeArr = value.toString().split(':')
344
- // 使用formatter格式化方法进行管道处理
345
- values = [formatterFn('minute', timeArr[0]), formatterFn('second', timeArr[1])]
346
- break
347
- default:
348
- values = [
349
- formatterFn('year', `${dayjs(value).year()}`),
350
- // 月份补0
351
- formatterFn('month', padZero(dayjs(value).month() + 1))
352
- ]
353
- if (props.mode === DateModeEnum.DATE) {
354
- // date模式,需要添加天列
355
- values.push(formatterFn('day', padZero(dayjs(value).date())))
356
- }
357
- if (props.mode === DateModeEnum.DATETIME) {
358
- // 数组的push方法,可以写入多个参数
359
- values.push(
360
- formatterFn('day', padZero(dayjs(value).date())),
361
- formatterFn('hour', padZero(dayjs(value).hour())),
362
- formatterFn('minute', padZero(dayjs(value).minute())),
363
- formatterFn('second', padZero(dayjs(value).second()))
364
- )
365
- }
366
- break
367
- }
368
- // 根据当前各列的所有值,从各列默认值中找到默认值在各列中的索引
369
- innerDefaultIndex.value = columns.value.map((column, index) => {
370
- // 通过取大值,可以保证不会出现找不到索引的"-1"情况
371
- return Math.max(
372
- 0,
373
- column.findIndex((item: string) => item === values[index])
374
- )
375
- })
376
- }
377
-
378
- /**
379
- * @description 获取每列数据
380
- * */
381
- const getOriginColumns = () => {
382
- // 生成各列的值
383
- return getRanges().map(({ type, range }) => {
384
- let values = times(range[1] - range[0] + 1, (index: number) => {
385
- let value: string | number = range[0] + index
386
- value = type === 'year' ? `${value}` : padZero(value)
387
- return value
388
- })
389
- // 进行过滤
390
- if (props.filter) {
391
- values = props.filter(type, values)
392
- if (!values || (values && values.length == 0)) {
393
- error('日期filter结果不能为空')
394
- }
395
- }
396
- return { type, values }
397
- })
398
- }
399
-
400
- /**
401
- * @description 得出合法的时间
402
- * */
403
- const correctValue = (value: number | string | Date): string | number => {
404
- const isDateMode = props.mode !== DateModeEnum.TIME
405
- // if (isDateMode && !test.date(value)) {
406
- if (!isDateMode && !value) {
407
- // 如果是时间类型,而又没有默认值的话,就用最小时间
408
- value = `${padZero(props.minHour)}:${padZero(props.minMinute)}`
409
- }
410
- // 时间类型
411
- if (validModes.has(props.mode)) {
412
- return value as string
413
- } else {
414
- // 如果是日期格式,控制在最小日期和最大日期之间
415
- value = dayjs(value).isBefore(dayjs(props.minDate)) ? props.minDate : value
416
- value = dayjs(value).isAfter(dayjs(props.maxDate)) ? props.maxDate : value
417
- return value as string | number
418
- }
419
- }
420
- /**
421
- * @description 获取每列的最大和最小值
422
- * */
423
- const getRanges = () => {
424
- if (props.mode === DateModeEnum.TIME) {
425
- return [
426
- {
427
- type: 'hour',
428
- range: [props.minHour, props.maxHour]
429
- },
430
- {
431
- type: 'minute',
432
- range: [props.minMinute, props.maxMinute]
433
- }
434
- ]
435
- }
436
- const { maxYear, maxDate, maxMonth, maxHour, maxMinute } = getBoundary('max', innerValue.value)
437
- const { minYear, minDate, minMonth, minHour, minMinute } = getBoundary('min', innerValue.value)
438
- const result = [
439
- {
440
- type: 'year',
441
- range: [minYear, maxYear]
442
- },
443
- {
444
- type: 'month',
445
- range: [minMonth, maxMonth]
446
- },
447
- {
448
- type: 'day',
449
- range: [minDate, maxDate]
450
- },
451
- {
452
- type: 'hour',
453
- range: [minHour, maxHour]
454
- },
455
- {
456
- type: 'minute',
457
- range: [minMinute, maxMinute]
458
- },
459
- {
460
- type: 'second',
461
- range: [minMinute, maxMinute]
462
- }
463
- ]
464
- let arr = result
465
- // 截取对应的列数
466
- if (props.mode === DateModeEnum.DATE) arr = result.splice(0, 3)
467
- if (props.mode === DateModeEnum.YEAR_MONTH) arr = result.splice(0, 2)
468
- if (props.mode === DateModeEnum.MONTH_DAY) arr = result.splice(1, 2)
469
- if (props.mode === DateModeEnum.HOUR_MINUTE) arr = result.splice(3, 2)
470
- if (props.mode === DateModeEnum.MINUTE_SECOND) arr = result.splice(4, 2)
471
- return arr
472
- }
473
- /**
474
- * @description 根据minDate、maxDate、minHour、maxHour等边界值,判断各列的开始和结束边界值
475
- * */
476
- const getBoundary = (type: string, innerVal: string | number) => {
477
- const value = new Date(innerVal)
478
- const boundary = new Date((props as any)[`${type}Date`])
479
- const year = dayjs(boundary).year()
480
- let month = 1
481
- let date = 1
482
- let hour = 0
483
- let minute = 0
484
- if (type === 'max') {
485
- month = 12
486
- // 月份的天数
487
- date = dayjs(value).daysInMonth() || 31
488
- hour = 23
489
- minute = 59
490
- }
491
- // 获取边界值,逻辑是:当年达到了边界值(最大或最小年),就检查月允许的最大和最小值,以此类推
492
- if (dayjs(value).year() === year) {
493
- month = dayjs(boundary).month() + 1
494
- if (dayjs(value).month() + 1 === month) {
495
- date = dayjs(boundary).date()
496
- if (dayjs(value).date() === date) {
497
- hour = dayjs(boundary).hour()
498
- if (dayjs(value).hour() === hour) {
499
- minute = dayjs(boundary).minute()
500
- }
501
- }
502
- }
503
- }
504
- return {
505
- [`${type}Year`]: year,
506
- [`${type}Month`]: month,
507
- [`${type}Date`]: date,
508
- [`${type}Hour`]: hour,
509
- [`${type}Minute`]: minute
510
- }
511
- }
512
- const onShowByClickInput = () => {
513
- if (!props.input?.disabled) {
514
- showByClickInput.value = !showByClickInput.value
515
- }
516
- }
517
- </script>
518
-
519
- <style lang="scss" scoped>
520
- @import './index.scss';
521
- </style>
1
+ <template>
2
+ <view :class="['hy-datetime-picker', customClass]" :style="customStyle">
3
+ <view v-if="hasInput" class="hy-datetime-picker__has-input" @click="onShowByClickInput">
4
+ <slot name="trigger" :value="inputValue">
5
+ <HyInput
6
+ v-model="inputValue"
7
+ :disabled="input?.disabled"
8
+ :disabledColor="input?.disabledColor"
9
+ :shape="input?.shape"
10
+ :border="input?.border"
11
+ :prefixIcon="input?.prefixIcon"
12
+ :suffixIcon="input?.suffixIcon"
13
+ :color="input?.color"
14
+ :fontSize="input?.fontSize"
15
+ :inputAlign="input?.inputAlign"
16
+ :placeholder="input?.placeholder || '请选择时间'"
17
+ :placeholderStyle="input?.placeholderStyle"
18
+ :placeholderClass="input?.placeholderClass"
19
+ :customStyle="Object.assign({ 'pointer-events': 'none' }, input?.customStyle)"
20
+ ></HyInput>
21
+ <view class="input-cover"></view>
22
+ </slot>
23
+ </view>
24
+ <HyPicker
25
+ :show="show || (hasInput && showByClickInput)"
26
+ :popupMode="popupMode"
27
+ :closeOnClickOverlay="closeOnClickOverlay"
28
+ :columns="columns"
29
+ :title="title"
30
+ :itemHeight="itemHeight"
31
+ :loading="loading"
32
+ :showToolbar="showToolbar"
33
+ :visibleItemCount="visibleItemCount"
34
+ :defaultIndex="innerDefaultIndex"
35
+ :cancelText="cancelText"
36
+ :cancelColor="cancelColor"
37
+ :confirmColor="confirmColor"
38
+ :toolbarRightSlot="toolbarRightSlot"
39
+ @close="close"
40
+ @cancel="cancel"
41
+ @confirm="confirm"
42
+ @change="change"
43
+ >
44
+ <template #toolbar-right>
45
+ <slot name="toolbar-right">
46
+ {{ confirmText }}
47
+ </slot>
48
+ </template>
49
+ <template #toolbar-bottom>
50
+ <slot name="toolbar-bottom"></slot>
51
+ </template>
52
+ </HyPicker>
53
+ </view>
54
+ </template>
55
+
56
+ <script lang="ts">
57
+ export default {
58
+ name: 'hy-datetime-picker',
59
+ options: {
60
+ addGlobalClass: true,
61
+ virtualHost: true,
62
+ styleIsolation: 'shared'
63
+ }
64
+ }
65
+ </script>
66
+
67
+ <script setup lang="ts">
68
+ import { computed, onMounted, ref, watch } from 'vue'
69
+ import dayjs from 'dayjs/esm'
70
+ import { error, padZero, DateModeEnum } from '../../libs'
71
+ import type { IDatetimePickerEmits } from './typing'
72
+ import datetimePickerProps from './props'
73
+ // 组件
74
+ import HyInput from '../hy-input/hy-input.vue'
75
+ import HyPicker from '../hy-picker/hy-picker.vue'
76
+
77
+ /**
78
+ * 此选择器用于时间日期选择
79
+ * @displayName hy-datetime-picker
80
+ */
81
+ defineOptions({})
82
+
83
+ const props = defineProps(datetimePickerProps)
84
+ const emit = defineEmits<IDatetimePickerEmits>()
85
+
86
+ // 原来的日期选择器不方便,这里增加一个hasInput选项支持类似element的自带输入框的功能。
87
+ const inputValue = ref<string | number>('') // 表单显示值
88
+ const innerValue = ref<string | number>('') // 表单显示值
89
+ const showByClickInput = ref<boolean>(false) // 是否在hasInput模式下显示日期选择弹唱
90
+ const columns = ref<any[]>([])
91
+ const innerDefaultIndex = ref<number[]>([])
92
+ let innerFormatter = (type: any, value: any) => value
93
+ const validModes = new Set([
94
+ DateModeEnum.TIME,
95
+ DateModeEnum.MONTH_DAY,
96
+ DateModeEnum.HOUR_MINUTE,
97
+ DateModeEnum.MINUTE_SECOND
98
+ ])
99
+
100
+ /**
101
+ * @description 更新各列的值
102
+ * */
103
+ const updateColumns = () => {
104
+ const formatterFn = props.formatter || innerFormatter
105
+ // 获取各列的值,并且map后,对各列的具体值进行补0操作
106
+ columns.value = getOriginColumns().map((column) =>
107
+ column.values.map((value) => formatterFn(column.type, value))
108
+ )
109
+ }
110
+
111
+ /**
112
+ * @description 更新各列的值,进行补0、格式化等操作
113
+ * */
114
+ const updateColumnValue = (value: string | number) => {
115
+ innerValue.value = value
116
+ updateColumns()
117
+ // 延迟执行,等待u-picker组件列数据更新完后再设置选中值索引
118
+ setTimeout(() => {
119
+ updateIndexes(value)
120
+ }, 100)
121
+ }
122
+
123
+ const init = () => {
124
+ // 获取当前值
125
+ innerValue.value = correctValue(props.modelValue)
126
+ // 更新列表
127
+ updateColumnValue(innerValue.value)
128
+
129
+ // 初始化hasInput展示
130
+ getInputValue(innerValue.value)
131
+ }
132
+
133
+ watch(
134
+ () => props.show,
135
+ (newValue) => {
136
+ if (newValue) {
137
+ updateColumnValue(innerValue.value)
138
+ }
139
+ }
140
+ )
141
+
142
+ watch(
143
+ () => props.modelValue,
144
+ () => init()
145
+ )
146
+
147
+ const propsChange = computed(() => {
148
+ return [
149
+ props.mode,
150
+ props.maxDate,
151
+ props.minDate,
152
+ props.minHour,
153
+ props.maxHour,
154
+ props.minMinute,
155
+ props.maxMinute,
156
+ props.filter
157
+ ]
158
+ })
159
+
160
+ watch(
161
+ () => propsChange.value,
162
+ () => init()
163
+ )
164
+
165
+ onMounted(() => {
166
+ init()
167
+ })
168
+
169
+ const getInputValue = (newValue: string | number) => {
170
+ if (newValue == '' || !newValue) {
171
+ inputValue.value = ''
172
+ return
173
+ }
174
+ if (props.mode === 'time') {
175
+ inputValue.value = newValue
176
+ } else {
177
+ if (props.format) {
178
+ inputValue.value = dayjs(newValue).format(props.format)
179
+ } else {
180
+ let format = ''
181
+ switch (props.mode) {
182
+ case DateModeEnum.DATE:
183
+ format = 'YYYY-MM-DD'
184
+ break
185
+ case DateModeEnum.YEAR_MONTH:
186
+ format = 'YYYY-MM'
187
+ break
188
+ case DateModeEnum.DATETIME:
189
+ format = 'YYYY-MM-DD HH:mm:ss'
190
+ break
191
+ case DateModeEnum.MONTH_DAY:
192
+ format = 'MM-DD'
193
+ break
194
+ case DateModeEnum.HOUR_MINUTE:
195
+ format = 'HH:mm'
196
+ break
197
+ case DateModeEnum.MINUTE_SECOND:
198
+ format = 'mm:ss'
199
+ break
200
+ default:
201
+ break
202
+ }
203
+ inputValue.value = dayjs(newValue).isValid() ? dayjs(newValue).format(format) : newValue
204
+ }
205
+ }
206
+ }
207
+ const times = (n: number, iteratee: Function) => {
208
+ let index = -1
209
+ const result = Array(n < 0 ? 0 : n)
210
+ while (++index < n) {
211
+ result[index] = iteratee(index)
212
+ }
213
+ return result
214
+ }
215
+
216
+ /**
217
+ * @description 关闭选择器
218
+ * */
219
+ const close = () => {
220
+ if (props.closeOnClickOverlay) {
221
+ if (props.hasInput) {
222
+ showByClickInput.value = false
223
+ }
224
+ emit('close')
225
+ }
226
+ }
227
+
228
+ /**
229
+ * @description 点击工具栏的取消按钮
230
+ * */
231
+ const cancel = () => {
232
+ if (props.hasInput) {
233
+ showByClickInput.value = false
234
+ }
235
+ emit('cancel')
236
+ }
237
+
238
+ /**
239
+ * @description 点击工具栏的确定按钮
240
+ * */
241
+ const confirm = () => {
242
+ emit('update:modelValue', innerValue.value)
243
+ if (props.hasInput) {
244
+ getInputValue(innerValue.value)
245
+ showByClickInput.value = false
246
+ }
247
+ emit('confirm', {
248
+ value: innerValue.value,
249
+ mode: props.mode
250
+ })
251
+ }
252
+
253
+ /**
254
+ * @description 用正则截取输出值,当出现多组数字时,抛出错误
255
+ * */
256
+ const intercept = (e: any, type?: string) => {
257
+ let judge = e.match(/\d+/g)
258
+ //判断是否掺杂数字
259
+ if (judge.length > 1) {
260
+ error('请勿在过滤或格式化函数时添加数字')
261
+ return 0
262
+ } else if (type && judge[0].length == 4) {
263
+ //判断是否是年份
264
+ return judge[0]
265
+ } else if (judge[0].length > 2) {
266
+ error('请勿在过滤或格式化函数时添加数字')
267
+ return 0
268
+ } else {
269
+ return judge[0]
270
+ }
271
+ }
272
+
273
+ /**
274
+ * @description 列发生变化时触发
275
+ * */
276
+ const change = (e: any) => {
277
+ const { indexs, values } = e
278
+ let selectValue: string | number = ''
279
+ if (validModes.has(props.mode) && props.mode !== DateModeEnum.MONTH_DAY) {
280
+ // 根据value各列索引,从各列数组中,取出当前时间的选中值
281
+ selectValue = `${intercept(values[0][indexs[0]])}:${intercept(values[1][indexs[1]])}`
282
+ } else if (props.mode === DateModeEnum.MONTH_DAY) {
283
+ // 根据value各列索引,从各列数组中,取出当前时间的选中值
284
+ selectValue = `${intercept(values[0][indexs[0]])}-${intercept(values[1][indexs[1]])}`
285
+ } else {
286
+ // 将选择的值转为数值,比如'03'转为数值的3,'2019'转为数值的2019
287
+ const year = parseInt(intercept(values[0][indexs[0]], 'year'))
288
+ const month = parseInt(intercept(values[1][indexs[1]]))
289
+ let date = parseInt(values[2] ? intercept(values[2][indexs[2]]) : 1)
290
+ let hour = 0,
291
+ minute = 0,
292
+ second = 0
293
+ // 此月份的最大天数
294
+ const maxDate = dayjs(`${year}-${month}`).daysInMonth()
295
+ // 不允许超过maxDate值
296
+ date = Math.min(maxDate, date)
297
+ if (props.mode === DateModeEnum.DATETIME) {
298
+ hour = parseInt(intercept(values[3][indexs[3]]))
299
+ minute = parseInt(intercept(values[4][indexs[4]]))
300
+ second = parseInt(intercept(values[5][indexs[5]]))
301
+ }
302
+ // 转为时间模式
303
+ selectValue = Number(new Date(year, month - 1, date, hour, minute, second))
304
+ }
305
+ // 取出准确的合法值,防止超越边界的情况
306
+ selectValue = correctValue(selectValue)
307
+ innerValue.value = selectValue
308
+ updateColumnValue(selectValue)
309
+ // 发出change时间,value为当前选中的时间戳
310
+ emit('change', {
311
+ value: selectValue,
312
+ mode: props.mode
313
+ })
314
+ }
315
+
316
+ /**
317
+ * @description 更新索引
318
+ * */
319
+ const updateIndexes = (value: number | string) => {
320
+ let values: string[] = []
321
+ let timeArr: string[] = []
322
+ const formatterFn = props.formatter || innerFormatter
323
+
324
+ switch (props.mode) {
325
+ case DateModeEnum.TIME:
326
+ timeArr = value.toString().split(':')
327
+ // 使用formatter格式化方法进行管道处理
328
+ values = [formatterFn('hour', timeArr[0]), formatterFn('minute', timeArr[1])]
329
+ break
330
+ case DateModeEnum.MONTH_DAY:
331
+ timeArr = value.toString().split('-')
332
+ // 使用formatter格式化方法进行管道处理
333
+ values = [formatterFn('month', timeArr[0]), formatterFn('day', timeArr[1])]
334
+ break
335
+ case DateModeEnum.HOUR_MINUTE:
336
+ timeArr = value.toString().split(':')
337
+ // 使用formatter格式化方法进行管道处理
338
+ values = [formatterFn('hour', timeArr[0]), formatterFn('minute', timeArr[1])]
339
+ break
340
+ case DateModeEnum.MINUTE_SECOND:
341
+ timeArr = value.toString().split(':')
342
+ // 使用formatter格式化方法进行管道处理
343
+ values = [formatterFn('minute', timeArr[0]), formatterFn('second', timeArr[1])]
344
+ break
345
+ default:
346
+ values = [
347
+ formatterFn('year', `${dayjs(value).year()}`),
348
+ // 月份补0
349
+ formatterFn('month', padZero(dayjs(value).month() + 1))
350
+ ]
351
+ if (props.mode === DateModeEnum.DATE) {
352
+ // date模式,需要添加天列
353
+ values.push(formatterFn('day', padZero(dayjs(value).date())))
354
+ }
355
+ if (props.mode === DateModeEnum.DATETIME) {
356
+ // 数组的push方法,可以写入多个参数
357
+ values.push(
358
+ formatterFn('day', padZero(dayjs(value).date())),
359
+ formatterFn('hour', padZero(dayjs(value).hour())),
360
+ formatterFn('minute', padZero(dayjs(value).minute())),
361
+ formatterFn('second', padZero(dayjs(value).second()))
362
+ )
363
+ }
364
+ break
365
+ }
366
+ // 根据当前各列的所有值,从各列默认值中找到默认值在各列中的索引
367
+ innerDefaultIndex.value = columns.value.map((column, index) => {
368
+ // 通过取大值,可以保证不会出现找不到索引的"-1"情况
369
+ return Math.max(
370
+ 0,
371
+ column.findIndex((item: string) => item === values[index])
372
+ )
373
+ })
374
+ }
375
+
376
+ /**
377
+ * @description 获取每列数据
378
+ * */
379
+ const getOriginColumns = () => {
380
+ // 生成各列的值
381
+ return getRanges().map(({ type, range }) => {
382
+ let values = times(range[1] - range[0] + 1, (index: number) => {
383
+ let value: string | number = range[0] + index
384
+ value = type === 'year' ? `${value}` : padZero(value)
385
+ return value
386
+ })
387
+ // 进行过滤
388
+ if (props.filter) {
389
+ values = props.filter(type, values)
390
+ if (!values || (values && values.length == 0)) {
391
+ error('日期filter结果不能为空')
392
+ }
393
+ }
394
+ return { type, values }
395
+ })
396
+ }
397
+
398
+ /**
399
+ * @description 得出合法的时间
400
+ * */
401
+ const correctValue = (value: number | string | Date): string | number => {
402
+ const isDateMode = props.mode !== DateModeEnum.TIME
403
+ // if (isDateMode && !test.date(value)) {
404
+ if (!isDateMode && !value) {
405
+ // 如果是时间类型,而又没有默认值的话,就用最小时间
406
+ value = `${padZero(props.minHour)}:${padZero(props.minMinute)}`
407
+ }
408
+ // 时间类型
409
+ if (validModes.has(props.mode)) {
410
+ return value as string
411
+ } else {
412
+ // 如果是日期格式,控制在最小日期和最大日期之间
413
+ value = dayjs(value).isBefore(dayjs(props.minDate)) ? props.minDate : value
414
+ value = dayjs(value).isAfter(dayjs(props.maxDate)) ? props.maxDate : value
415
+ return value as string | number
416
+ }
417
+ }
418
+ /**
419
+ * @description 获取每列的最大和最小值
420
+ * */
421
+ const getRanges = () => {
422
+ if (props.mode === DateModeEnum.TIME) {
423
+ return [
424
+ {
425
+ type: 'hour',
426
+ range: [props.minHour, props.maxHour]
427
+ },
428
+ {
429
+ type: 'minute',
430
+ range: [props.minMinute, props.maxMinute]
431
+ }
432
+ ]
433
+ }
434
+ const { maxYear, maxDate, maxMonth, maxHour, maxMinute } = getBoundary('max', innerValue.value)
435
+ const { minYear, minDate, minMonth, minHour, minMinute } = getBoundary('min', innerValue.value)
436
+ const result = [
437
+ {
438
+ type: 'year',
439
+ range: [minYear, maxYear]
440
+ },
441
+ {
442
+ type: 'month',
443
+ range: [minMonth, maxMonth]
444
+ },
445
+ {
446
+ type: 'day',
447
+ range: [minDate, maxDate]
448
+ },
449
+ {
450
+ type: 'hour',
451
+ range: [minHour, maxHour]
452
+ },
453
+ {
454
+ type: 'minute',
455
+ range: [minMinute, maxMinute]
456
+ },
457
+ {
458
+ type: 'second',
459
+ range: [minMinute, maxMinute]
460
+ }
461
+ ]
462
+ let arr = result
463
+ // 截取对应的列数
464
+ if (props.mode === DateModeEnum.DATE) arr = result.splice(0, 3)
465
+ if (props.mode === DateModeEnum.YEAR_MONTH) arr = result.splice(0, 2)
466
+ if (props.mode === DateModeEnum.MONTH_DAY) arr = result.splice(1, 2)
467
+ if (props.mode === DateModeEnum.HOUR_MINUTE) arr = result.splice(3, 2)
468
+ if (props.mode === DateModeEnum.MINUTE_SECOND) arr = result.splice(4, 2)
469
+ return arr
470
+ }
471
+ /**
472
+ * @description 根据minDate、maxDate、minHour、maxHour等边界值,判断各列的开始和结束边界值
473
+ * */
474
+ const getBoundary = (type: string, innerVal: string | number) => {
475
+ const value = new Date(innerVal)
476
+ const boundary = new Date((props as any)[`${type}Date`])
477
+ const year = dayjs(boundary).year()
478
+ let month = 1
479
+ let date = 1
480
+ let hour = 0
481
+ let minute = 0
482
+ if (type === 'max') {
483
+ month = 12
484
+ // 月份的天数
485
+ date = dayjs(value).daysInMonth() || 31
486
+ hour = 23
487
+ minute = 59
488
+ }
489
+ // 获取边界值,逻辑是:当年达到了边界值(最大或最小年),就检查月允许的最大和最小值,以此类推
490
+ if (dayjs(value).year() === year) {
491
+ month = dayjs(boundary).month() + 1
492
+ if (dayjs(value).month() + 1 === month) {
493
+ date = dayjs(boundary).date()
494
+ if (dayjs(value).date() === date) {
495
+ hour = dayjs(boundary).hour()
496
+ if (dayjs(value).hour() === hour) {
497
+ minute = dayjs(boundary).minute()
498
+ }
499
+ }
500
+ }
501
+ }
502
+ return {
503
+ [`${type}Year`]: year,
504
+ [`${type}Month`]: month,
505
+ [`${type}Date`]: date,
506
+ [`${type}Hour`]: hour,
507
+ [`${type}Minute`]: minute
508
+ }
509
+ }
510
+ const onShowByClickInput = () => {
511
+ if (!props.input?.disabled) {
512
+ showByClickInput.value = !showByClickInput.value
513
+ }
514
+ }
515
+ </script>
516
+
517
+ <style lang="scss" scoped>
518
+ @import './index.scss';
519
+ </style>