@@ -10,11 +10,13 @@ import { CalendarIcon } from "lucide-react";
1010interface DatePickerDemoProps {
1111 value : string ; // ISO string from formData
1212 onChange : ( value : string ) => void ; // callback to update formData
13+ allowTimeSelection ?: boolean ; // Make time selection optional
1314}
1415
1516export default function DatePickerDemo ( {
1617 value,
1718 onChange,
19+ allowTimeSelection = true , // Default to true for backward compatibility
1820} : DatePickerDemoProps ) {
1921 const today = new Date ( ) ;
2022 const [ date , setDate ] = useState < Date | undefined > (
@@ -72,8 +74,16 @@ export default function DatePickerDemo({
7274 const handleDateSelect = ( newDate : Date | undefined ) => {
7375 if ( ! newDate ) return ;
7476 setDate ( newDate ) ;
75- setTime ( undefined ) ;
7677
78+ if ( ! allowTimeSelection ) {
79+ // If time selection is not allowed, use start of day
80+ const dateOnly = new Date ( newDate ) ;
81+ dateOnly . setHours ( 0 , 0 , 0 , 0 ) ;
82+ onChange ( dateOnly . toISOString ( ) ) ;
83+ return ;
84+ }
85+
86+ setTime ( undefined ) ;
7787 // Reset time in formData if time was previously set
7888 if ( value ) onChange ( "" ) ;
7989 } ;
@@ -91,11 +101,12 @@ export default function DatePickerDemo({
91101 < CalendarIcon className = "mr-2 h-4 w-4" />
92102 { date ? (
93103 < span className = "text-zinc-900 dark:text-white" >
94- { format ( date , "PPP" ) } { time ? ` - ${ time } ` : "" }
104+ { format ( date , "PPP" ) } { " " }
105+ { allowTimeSelection && time ? ` - ${ time } ` : "" }
95106 </ span >
96107 ) : (
97108 < span className = "text-zinc-500 dark:text-zinc-400" >
98- Pick a date and time
109+ { allowTimeSelection ? " Pick a date and time" : "Pick a date" }
99110 </ span >
100111 ) }
101112 </ Button >
@@ -113,37 +124,39 @@ export default function DatePickerDemo({
113124 className = "p-2 sm:pe-5"
114125 disabled = { [ { before : today } ] }
115126 />
116- < div className = "relative w-full max-sm:h-48 sm:w-40" >
117- < div className = "absolute inset-0 py-4 max-sm:border-t border-zinc-200 dark:border-zinc-700" >
118- < ScrollArea className = "h-full sm:border-s border-zinc-200 dark:border-zinc-700" >
119- < div className = "space-y-3" >
120- < div className = "flex h-5 shrink-0 items-center px-5" >
121- < p className = "text-sm font-medium text-zinc-900 dark:text-white" >
122- { date ? format ( date , "EEEE, d" ) : "Pick a date" }
123- </ p >
124- </ div >
125- < div className = "grid gap-1.5 px-5 max-sm:grid-cols-2" >
126- { timeSlots . map ( ( { time : timeSlot , available } ) => (
127- < Button
128- key = { timeSlot }
129- variant = { time === timeSlot ? "primary" : "outline" }
130- size = "sm"
131- className = { `w-full ${
132- time === timeSlot
133- ? "bg-blue-600 text-white hover:bg-blue-700"
134- : "bg-transparent border-zinc-300 dark:border-zinc-600 text-zinc-900 dark:text-white hover:bg-zinc-100 dark:hover:bg-zinc-700"
135- } `}
136- onClick = { ( ) => handleTimeSelect ( timeSlot ) }
137- disabled = { ! available }
138- >
139- { timeSlot }
140- </ Button >
141- ) ) }
127+ { allowTimeSelection && (
128+ < div className = "relative w-full max-sm:h-48 sm:w-40" >
129+ < div className = "absolute inset-0 py-4 max-sm:border-t border-zinc-200 dark:border-zinc-700" >
130+ < ScrollArea className = "h-full sm:border-s border-zinc-200 dark:border-zinc-700" >
131+ < div className = "space-y-3" >
132+ < div className = "flex h-5 shrink-0 items-center px-5" >
133+ < p className = "text-sm font-medium text-zinc-900 dark:text-white" >
134+ { date ? format ( date , "EEEE, d" ) : "Pick a date" }
135+ </ p >
136+ </ div >
137+ < div className = "grid gap-1.5 px-5 max-sm:grid-cols-2" >
138+ { timeSlots . map ( ( { time : timeSlot , available } ) => (
139+ < Button
140+ key = { timeSlot }
141+ variant = { time === timeSlot ? "primary" : "outline" }
142+ size = "sm"
143+ className = { `w-full ${
144+ time === timeSlot
145+ ? "bg-blue-600 text-white hover:bg-blue-700"
146+ : "bg-transparent border-zinc-300 dark:border-zinc-600 text-zinc-900 dark:text-white hover:bg-zinc-100 dark:hover:bg-zinc-700"
147+ } `}
148+ onClick = { ( ) => handleTimeSelect ( timeSlot ) }
149+ disabled = { ! available }
150+ >
151+ { timeSlot }
152+ </ Button >
153+ ) ) }
154+ </ div >
142155 </ div >
143- </ div >
144- </ ScrollArea >
156+ </ ScrollArea >
157+ </ div >
145158 </ div >
146- </ div >
159+ ) }
147160 </ div >
148161 </ PopoverContent >
149162 </ Popover >
0 commit comments