1
1
import type { InternalMode , Locale , SharedPickerProps , SharedTimeProps } from '../interface' ;
2
2
import { getRowFormat , pickProps , toArray } from '../utils/miscUtil' ;
3
+ import { fillTimeFormat } from './useLocale' ;
3
4
4
5
function checkShow ( format : string , keywords : string [ ] , show ?: boolean ) {
5
6
return show ?? keywords . some ( ( keyword ) => format . includes ( keyword ) ) ;
@@ -48,28 +49,76 @@ function isStringFormat(format: any): format is string {
48
49
return format && typeof format === 'string' ;
49
50
}
50
51
51
- export function getTimeConfig < DateType extends object > ( componentProps : {
52
+ export interface ComponentProps < DateType extends object > {
52
53
picker ?: InternalMode ;
53
54
showTime ?: boolean | Partial < SharedTimeProps < DateType > > ;
54
55
locale : Locale ;
55
56
format ?: SharedPickerProps [ 'format' ] ;
56
- } ) : SharedTimeProps < DateType > {
57
- const { showTime, picker, locale, format : propFormat } = componentProps ;
57
+ }
58
58
59
- const isTimePicker = picker === 'time' ;
59
+ /**
60
+ * Get `showHour`, `showMinute`, `showSecond` or other from the props.
61
+ * This is pure function, will not get `showXXX` from the `format` prop.
62
+ */
63
+ export function getTimeProps < DateType extends object > (
64
+ componentProps : ComponentProps < DateType > ,
65
+ ) : [
66
+ showTimeProps : SharedTimeProps < DateType > ,
67
+ showTimePropsForLocale : SharedTimeProps < DateType > ,
68
+ showTimeFormat : string ,
69
+ propFormat : string ,
70
+ ] {
71
+ const { showTime, picker } = componentProps ;
72
+
73
+ const pickedProps = pickTimeProps ( componentProps ) ;
74
+ const isShowTimeConfig = showTime && typeof showTime === 'object' ;
75
+ const timeConfig = isShowTimeConfig ? showTime : pickedProps ;
76
+
77
+ const { showMillisecond } = timeConfig ;
78
+ let { showHour, showMinute, showSecond } = timeConfig ;
79
+
80
+ if ( ! showHour && ! showMinute && ! showSecond && ! showMillisecond ) {
81
+ showHour = true ;
82
+ showMinute = true ;
83
+ showSecond = true ;
84
+ }
85
+
86
+ const mergedFormat = isShowTimeConfig
87
+ ? showTime . format
88
+ : picker === 'time'
89
+ ? pickedProps . format
90
+ : null ;
60
91
61
- if ( showTime || isTimePicker ) {
62
- const isShowTimeConfig = showTime && typeof showTime === 'object' ;
92
+ return [
93
+ timeConfig ,
94
+ {
95
+ ...timeConfig ,
96
+ showHour,
97
+ showMinute,
98
+ showSecond,
99
+ showMillisecond,
100
+ } ,
101
+ mergedFormat ,
102
+ pickedProps . format ,
103
+ ] ;
104
+ }
63
105
64
- const timeConfig = isShowTimeConfig ? showTime : pickTimeProps ( componentProps ) ;
106
+ export function fillShowTimeConfig < DateType extends object > (
107
+ picker : InternalMode ,
108
+ showTimeFormat : string ,
109
+ propFormat : string ,
110
+ timeConfig : SharedTimeProps < DateType > ,
111
+ locale : Locale ,
112
+ ) : SharedTimeProps < DateType > {
113
+ const isTimePicker = picker === 'time' ;
65
114
66
- const pickedProps = pickProps ( timeConfig ) ;
115
+ if ( picker === 'datetime' || isTimePicker ) {
116
+ const pickedProps = timeConfig ;
67
117
68
118
// ====================== BaseFormat ======================
69
- const showTimeFormat = isShowTimeConfig ? showTime . format : isTimePicker && propFormat ;
70
- const defaultFormat = getRowFormat ( picker , locale , null ) as string ;
119
+ const defaultLocaleFormat = getRowFormat ( picker , locale , null ) as string ;
71
120
72
- let baselineFormat = defaultFormat ;
121
+ let baselineFormat = defaultLocaleFormat ;
73
122
74
123
const formatList = [ showTimeFormat , propFormat ] ;
75
124
for ( let i = 0 ; i < formatList . length ; i += 1 ) {
@@ -85,7 +134,7 @@ export function getTimeConfig<DateType extends object>(componentProps: {
85
134
let { showHour, showMinute, showSecond, showMillisecond } = pickedProps ;
86
135
const { use12Hours } = pickedProps ;
87
136
88
- const showMeridiem = checkShow ( baselineFormat , [ 'a' , 'A' , 'LT' , 'LLL' ] , use12Hours ) ;
137
+ const showMeridiem = checkShow ( baselineFormat , [ 'a' , 'A' , 'LT' , 'LLL' , 'LTS' ] , use12Hours ) ;
89
138
90
139
const hasShowConfig = [ showHour , showMinute , showSecond , showMillisecond ] . some (
91
140
( show ) => show !== undefined ,
@@ -107,36 +156,9 @@ export function getTimeConfig<DateType extends object>(componentProps: {
107
156
}
108
157
109
158
// ======================== 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
- }
159
+ const timeFormat =
160
+ showTimeFormat ||
161
+ fillTimeFormat ( showHour , showMinute , showSecond , showMillisecond , showMeridiem ) ;
140
162
141
163
// ======================== Props =========================
142
164
return {
0 commit comments