1
1
import { Input } from '@/components/ui/input' ;
2
- import { Label } from '@/components/ui/label' ;
3
2
import {
4
3
Select ,
5
4
SelectContent ,
@@ -16,6 +15,7 @@ import {
16
15
setDateByType ,
17
16
} from '@/lib/time-picker-utils' ;
18
17
import { cn } from '@/lib/utils' ;
18
+ import { ClockIcon } from 'lucide-react' ;
19
19
import React from 'react' ;
20
20
21
21
export interface TimePickerInputProps extends React . InputHTMLAttributes < HTMLInputElement > {
@@ -73,7 +73,7 @@ const TimePickerInput = React.forwardRef<HTMLInputElement, TimePickerInputProps>
73
73
* If picker is '12hours' and the first digit is 0, then the second digit is automatically set to 1.
74
74
* The second entered digit will break the condition and the value will be set to 10-12.
75
75
*/
76
- if ( picker === '12hours ' ) {
76
+ if ( picker === 'hours ' ) {
77
77
if ( flag && calculatedValue . slice ( 1 , 2 ) === '1' && prevIntKey === '0' ) return '0' + key ;
78
78
}
79
79
@@ -93,7 +93,7 @@ const TimePickerInput = React.forwardRef<HTMLInputElement, TimePickerInputProps>
93
93
setDate ( setDateByType ( tempDate , newValue , picker , period ) ) ;
94
94
}
95
95
if ( e . key >= '0' && e . key <= '9' ) {
96
- if ( picker === '12hours ' ) setPrevIntKey ( e . key ) ;
96
+ if ( picker === 'hours ' ) setPrevIntKey ( e . key ) ;
97
97
98
98
const newValue = calculateNewValue ( e . key ) ;
99
99
if ( flag ) onRightFocus ?.( ) ;
@@ -157,9 +157,7 @@ const TimePeriodSelect = React.forwardRef<HTMLButtonElement, PeriodSelectorProps
157
157
if ( date ) {
158
158
const tempDate = new Date ( date ) ;
159
159
const hours = display12HourValue ( date . getHours ( ) ) ;
160
- setDate (
161
- setDateByType ( tempDate , hours . toString ( ) , '12hours' , period === 'AM' ? 'PM' : 'AM' )
162
- ) ;
160
+ setDate ( setDateByType ( tempDate , hours . toString ( ) , 'hours' , period === 'AM' ? 'PM' : 'AM' ) ) ;
163
161
}
164
162
} ;
165
163
@@ -191,55 +189,42 @@ interface TimePickerProps {
191
189
}
192
190
193
191
function TimePicker ( { date, setDate } : TimePickerProps ) {
194
- const [ period , setPeriod ] = React . useState < Period > ( 'PM' ) ;
192
+ const [ period , setPeriod ] = React . useState < Period > ( date && date . getHours ( ) >= 12 ? 'PM' : 'AM ') ;
195
193
196
194
const minuteRef = React . useRef < HTMLInputElement > ( null ) ;
197
195
const hourRef = React . useRef < HTMLInputElement > ( null ) ;
198
196
const periodRef = React . useRef < HTMLButtonElement > ( null ) ;
199
197
200
198
return (
201
- < div className = "flex items-end gap-2 justify-center" >
202
- < div className = "grid gap-1 text-center" >
203
- < Label htmlFor = "hours" className = "text-xs" >
204
- Hours
205
- </ Label >
206
- < TimePickerInput
207
- id = "hours"
208
- picker = "12hours"
209
- period = { period }
210
- date = { date }
211
- setDate = { setDate }
212
- ref = { hourRef }
213
- onRightFocus = { ( ) => minuteRef . current ?. focus ( ) }
214
- />
215
- </ div >
216
- < div className = "grid gap-1 text-center" >
217
- < Label htmlFor = "minutes" className = "text-xs" >
218
- Minutes
219
- </ Label >
220
- < TimePickerInput
221
- id = "minutes"
222
- picker = "minutes"
223
- date = { date }
224
- setDate = { setDate }
225
- ref = { minuteRef }
226
- onLeftFocus = { ( ) => hourRef . current ?. focus ( ) }
227
- onRightFocus = { ( ) => periodRef . current ?. focus ( ) }
228
- />
229
- </ div >
230
- < div className = "grid gap-1 text-center" >
231
- < Label htmlFor = "period" className = "text-xs" >
232
- Period
233
- </ Label >
234
- < TimePeriodSelect
235
- period = { period }
236
- setPeriod = { setPeriod }
237
- date = { date }
238
- setDate = { setDate }
239
- ref = { periodRef }
240
- onLeftFocus = { ( ) => periodRef . current ?. focus ( ) }
241
- />
242
- </ div >
199
+ < div className = "flex items-center gap-4" >
200
+ < ClockIcon className = "size-5 text-current" />
201
+ < TimePickerInput
202
+ id = "hours"
203
+ picker = "hours"
204
+ period = { period }
205
+ date = { date }
206
+ setDate = { setDate }
207
+ ref = { hourRef }
208
+ onRightFocus = { ( ) => minuteRef . current ?. focus ( ) }
209
+ />
210
+ < span className = "-mx-1.5" > :</ span >
211
+ < TimePickerInput
212
+ id = "minutes"
213
+ picker = "minutes"
214
+ date = { date }
215
+ setDate = { setDate }
216
+ ref = { minuteRef }
217
+ onLeftFocus = { ( ) => hourRef . current ?. focus ( ) }
218
+ onRightFocus = { ( ) => periodRef . current ?. focus ( ) }
219
+ />
220
+ < TimePeriodSelect
221
+ period = { period }
222
+ setPeriod = { setPeriod }
223
+ date = { date }
224
+ setDate = { setDate }
225
+ ref = { periodRef }
226
+ onLeftFocus = { ( ) => periodRef . current ?. focus ( ) }
227
+ />
243
228
</ div >
244
229
) ;
245
230
}
0 commit comments