11import type { InternalMode , Locale , SharedPickerProps , SharedTimeProps } from '../interface' ;
22import { getRowFormat , pickProps , toArray } from '../utils/miscUtil' ;
3+ import { fillTimeFormat } from './useLocale' ;
34
45function checkShow ( format : string , keywords : string [ ] , show ?: boolean ) {
56 return show ?? keywords . some ( ( keyword ) => format . includes ( keyword ) ) ;
@@ -48,28 +49,76 @@ function isStringFormat(format: any): format is string {
4849 return format && typeof format === 'string' ;
4950}
5051
51- export function getTimeConfig < DateType extends object > ( componentProps : {
52+ export interface ComponentProps < DateType extends object > {
5253 picker ?: InternalMode ;
5354 showTime ?: boolean | Partial < SharedTimeProps < DateType > > ;
5455 locale : Locale ;
5556 format ?: SharedPickerProps [ 'format' ] ;
56- } ) : SharedTimeProps < DateType > {
57- const { showTime, picker, locale, format : propFormat } = componentProps ;
57+ }
5858
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 ;
6091
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+ }
63105
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' ;
65114
66- const pickedProps = pickProps ( timeConfig ) ;
115+ if ( picker === 'datetime' || isTimePicker ) {
116+ const pickedProps = timeConfig ;
67117
68118 // ====================== 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 ;
71120
72- let baselineFormat = defaultFormat ;
121+ let baselineFormat = defaultLocaleFormat ;
73122
74123 const formatList = [ showTimeFormat , propFormat ] ;
75124 for ( let i = 0 ; i < formatList . length ; i += 1 ) {
@@ -85,7 +134,7 @@ export function getTimeConfig<DateType extends object>(componentProps: {
85134 let { showHour, showMinute, showSecond, showMillisecond } = pickedProps ;
86135 const { use12Hours } = pickedProps ;
87136
88- const showMeridiem = checkShow ( baselineFormat , [ 'a' , 'A' , 'LT' , 'LLL' ] , use12Hours ) ;
137+ const showMeridiem = checkShow ( baselineFormat , [ 'a' , 'A' , 'LT' , 'LLL' , 'LTS' ] , use12Hours ) ;
89138
90139 const hasShowConfig = [ showHour , showMinute , showSecond , showMillisecond ] . some (
91140 ( show ) => show !== undefined ,
@@ -107,36 +156,9 @@ export function getTimeConfig<DateType extends object>(componentProps: {
107156 }
108157
109158 // ======================== 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 ) ;
140162
141163 // ======================== Props =========================
142164 return {
0 commit comments