feat: implement TimePicker component with date and time selection
This commit is contained in:
parent
4813c78b13
commit
f5eb4fe7d5
1 changed files with 84 additions and 0 deletions
84
src/components/time-picker.tsx
Normal file
84
src/components/time-picker.tsx
Normal file
|
@ -0,0 +1,84 @@
|
|||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
import { ChevronDownIcon } from 'lucide-react';
|
||||
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Calendar } from '@/components/custom-ui/calendar';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from '@/components/ui/popover';
|
||||
|
||||
export default function TimePicker({
|
||||
dateLabel = 'Date',
|
||||
timeLabel = 'Time',
|
||||
}: {
|
||||
dateLabel?: string;
|
||||
timeLabel?: string;
|
||||
}) {
|
||||
const [open, setOpen] = React.useState(false);
|
||||
const [date, setDate] = React.useState<Date | undefined>(undefined);
|
||||
|
||||
return (
|
||||
<div className='flex gap-4'>
|
||||
<div className='flex flex-col gap-3'>
|
||||
<Label htmlFor='date' className='px-1'>
|
||||
{dateLabel}
|
||||
</Label>
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
variant='outline'
|
||||
id='date'
|
||||
className='w-32 justify-between font-normal'
|
||||
>
|
||||
{date ? date.toLocaleDateString() : 'Select date'}
|
||||
<ChevronDownIcon />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className='w-auto overflow-hidden p-0' align='start'>
|
||||
<Calendar
|
||||
mode='single'
|
||||
selected={date}
|
||||
captionLayout='dropdown'
|
||||
onSelect={(date) => {
|
||||
setDate(date);
|
||||
setOpen(false);
|
||||
}}
|
||||
modifiers={{
|
||||
today: new Date(),
|
||||
}}
|
||||
modifiersClassNames={{
|
||||
today: 'bg-secondary text-secondary-foreground rounded-full',
|
||||
}}
|
||||
classNames={{
|
||||
day: 'text-center hover:bg-gray-500 hover:rounded-md',
|
||||
}}
|
||||
weekStartsOn={1} // Set Monday as the first day of the week
|
||||
/>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</div>
|
||||
<div className='flex flex-col gap-3'>
|
||||
<Label htmlFor='time' className='px-1'>
|
||||
{timeLabel}
|
||||
</Label>
|
||||
<Input
|
||||
type='time'
|
||||
id='time'
|
||||
step='60'
|
||||
defaultValue={new Date().toLocaleTimeString('en-GB', {
|
||||
hour12: false,
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
})}
|
||||
className='bg-background appearance-none [&::-webkit-calendar-picker-indicator]:hidden [&::-webkit-calendar-picker-indicator]:appearance-none'
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue