| 13 | import { CalendarIcon } from 'lucide-react'; |
| 14 | |
| 15 | export function DateTimePicker({ |
| 16 | value, |
| 17 | onChange, |
| 18 | }: { value: Date; onChange: (date: Date) => void }) { |
| 19 | function handleDateSelect(date: Date | undefined) { |
| 20 | if (date) { |
| 21 | onChange(date); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | function handleTimeChange(type: 'hour' | 'minute', value: string) { |
| 26 | const currentDate = value || new Date(); |
| 27 | const newDate = new Date(currentDate); |
| 28 | |
| 29 | if (type === 'hour') { |
| 30 | const hour = Number.parseInt(value, 10); |
| 31 | newDate.setHours(hour); |
| 32 | } else if (type === 'minute') { |
| 33 | newDate.setMinutes(Number.parseInt(value, 10)); |
| 34 | } |
| 35 | |
| 36 | onChange(newDate); |
| 37 | } |
| 38 | |
| 39 | return ( |
| 40 | <Popover> |
| 41 | <PopoverTrigger asChild> |
| 42 | <Button |
| 43 | variant={'outline'} |
| 44 | className={cn( |
| 45 | 'w-full pl-3 text-left font-normal', |
| 46 | !value && 'text-muted-foreground', |
| 47 | )} |
| 48 | > |
| 49 | {value ? ( |
| 50 | format(value, 'MM/dd/yyyy HH:mm') |
| 51 | ) : ( |
| 52 | <span>MM/DD/YYYY HH:mm</span> |
| 53 | )} |
| 54 | <CalendarIcon className="ml-auto h-4 w-4 opacity-50" /> |
| 55 | </Button> |
| 56 | </PopoverTrigger> |
| 57 | <PopoverPortal> |
| 58 | <PopoverContent className="w-auto p-0"> |
| 59 | <div className="sm:flex"> |
| 60 | <Calendar |
| 61 | mode="single" |
| 62 | selected={value} |
| 63 | onSelect={handleDateSelect} |
| 64 | initialFocus |
| 65 | /> |
| 66 | <div className="flex flex-col sm:flex-row sm:h-[300px] divide-y sm:divide-y-0 sm:divide-x"> |
| 67 | <ScrollArea className="w-64 sm:w-auto"> |
| 68 | <div className="flex sm:flex-col p-2"> |
| 69 | {Array.from({ length: 24 }, (_, i) => i) |
| 70 | .reverse() |
| 71 | .map((hour) => ( |
| 72 | <Button |