|
1 | | -import type { PickerMode, SharedTimeProps } from '../interface'; |
2 | | -import { pickProps } from '../utils/miscUtil'; |
| 1 | +import type { InternalMode, Locale, SharedPickerProps, SharedTimeProps } from '../interface'; |
| 2 | +import { getRowFormat, pickProps, toArray } from '../utils/miscUtil'; |
| 3 | + |
| 4 | +function checkShow(format: string, keywords: string[], show?: boolean) { |
| 5 | + return show ?? keywords.some((keyword) => format.includes(keyword)); |
| 6 | +} |
3 | 7 |
|
4 | 8 | const showTimeKeys = [ |
5 | 9 | 'format', |
@@ -40,23 +44,113 @@ function pickTimeProps<DateType extends object = any>(props: any): SharedTimePro |
40 | 44 | return timeProps; |
41 | 45 | } |
42 | 46 |
|
43 | | -export function getTimeConfig<DateType extends object>( |
44 | | - componentProps: { |
45 | | - picker?: PickerMode; |
46 | | - showTime?: boolean | Partial<SharedTimeProps<DateType>>; |
47 | | - } = {}, |
48 | | -): SharedTimeProps<DateType> { |
49 | | - const { showTime, picker } = componentProps; |
| 47 | +function isStringFormat(format: any): format is string { |
| 48 | + return format && typeof format === 'string'; |
| 49 | +} |
| 50 | + |
| 51 | +export function getTimeConfig<DateType extends object>(componentProps: { |
| 52 | + picker?: InternalMode; |
| 53 | + showTime?: boolean | Partial<SharedTimeProps<DateType>>; |
| 54 | + locale: Locale; |
| 55 | + format?: SharedPickerProps['format']; |
| 56 | +}): SharedTimeProps<DateType> { |
| 57 | + const { showTime, picker, locale, format: propFormat } = componentProps; |
50 | 58 |
|
51 | | - if (showTime || picker === 'time') { |
52 | | - const timeConfig = |
53 | | - showTime && typeof showTime === 'object' ? showTime : pickTimeProps(componentProps); |
| 59 | + const isTimePicker = picker === 'time'; |
| 60 | + |
| 61 | + if (showTime || isTimePicker) { |
| 62 | + const isShowTimeConfig = showTime && typeof showTime === 'object'; |
| 63 | + |
| 64 | + const timeConfig = isShowTimeConfig ? showTime : pickTimeProps(componentProps); |
54 | 65 |
|
55 | 66 | const pickedProps = pickProps(timeConfig); |
56 | 67 |
|
| 68 | + // ====================== BaseFormat ====================== |
| 69 | + const showTimeFormat = isShowTimeConfig ? showTime.format : isTimePicker && propFormat; |
| 70 | + const defaultFormat = getRowFormat(picker, locale, null) as string; |
| 71 | + |
| 72 | + let baselineFormat = defaultFormat; |
| 73 | + |
| 74 | + const formatList = [showTimeFormat, propFormat]; |
| 75 | + for (let i = 0; i < formatList.length; i += 1) { |
| 76 | + const format = toArray(formatList[i])[0]; |
| 77 | + |
| 78 | + if (isStringFormat(format)) { |
| 79 | + baselineFormat = format; |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // ========================= Show ========================= |
| 85 | + let { showHour, showMinute, showSecond, showMillisecond } = pickedProps; |
| 86 | + const { use12Hours } = pickedProps; |
| 87 | + |
| 88 | + const showMeridiem = checkShow(baselineFormat, ['a', 'A', 'LT', 'LLL'], use12Hours); |
| 89 | + |
| 90 | + const hasShowConfig = [showHour, showMinute, showSecond, showMillisecond].some( |
| 91 | + (show) => show !== undefined, |
| 92 | + ); |
| 93 | + |
| 94 | + // Fill with format, if needed |
| 95 | + if (!hasShowConfig) { |
| 96 | + showHour = checkShow(baselineFormat, ['H', 'h', 'k', 'LT', 'LLL']); |
| 97 | + showMinute = checkShow(baselineFormat, ['m', 'LT', 'LLL']); |
| 98 | + showSecond = checkShow(baselineFormat, ['s', 'LTS']); |
| 99 | + showMillisecond = checkShow(baselineFormat, ['SSS']); |
| 100 | + } |
| 101 | + |
| 102 | + // Fallback if all can not see |
| 103 | + if (!hasShowConfig && !showHour && !showMinute && !showSecond && !showMillisecond) { |
| 104 | + showHour = true; |
| 105 | + showMinute = true; |
| 106 | + showSecond = true; |
| 107 | + } |
| 108 | + |
| 109 | + // ======================== Format ======================== |
| 110 | + let timeFormat = isStringFormat(showTimeFormat) ? showTimeFormat : null; |
| 111 | + |
| 112 | + if (!timeFormat) { |
| 113 | + timeFormat = ''; |
| 114 | + |
| 115 | + // Base HH:mm:ss |
| 116 | + const cells = []; |
| 117 | + |
| 118 | + if (showHour) { |
| 119 | + cells.push('HH'); |
| 120 | + } |
| 121 | + if (showMinute) { |
| 122 | + cells.push('mm'); |
| 123 | + } |
| 124 | + if (showSecond) { |
| 125 | + cells.push('ss'); |
| 126 | + } |
| 127 | + |
| 128 | + timeFormat = cells.join(':'); |
| 129 | + |
| 130 | + // Millisecond |
| 131 | + if (showMillisecond) { |
| 132 | + timeFormat += '.SSS'; |
| 133 | + } |
| 134 | + |
| 135 | + // Meridiem |
| 136 | + if (showMeridiem) { |
| 137 | + timeFormat += ' A'; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // ======================== Props ========================= |
57 | 142 | return { |
58 | | - format: pickedProps.use12Hours ? 'HH:mm:ss A' : 'HH:mm:ss', |
59 | 143 | ...pickedProps, |
| 144 | + |
| 145 | + // Format |
| 146 | + format: timeFormat, |
| 147 | + |
| 148 | + // Show Config |
| 149 | + showHour, |
| 150 | + showMinute, |
| 151 | + showSecond, |
| 152 | + showMillisecond, |
| 153 | + use12Hours: showMeridiem, |
60 | 154 | }; |
61 | 155 | } |
62 | 156 |
|
|
0 commit comments