buster/web/src/components/ui/date/DatePicker.tsx

56 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-03-08 07:02:56 +08:00
'use client';
import * as React from 'react';
import { Calendar as CalendarIcon } from '@/components/ui/icons';
import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/buttons';
2025-03-18 06:20:15 +08:00
import { Calendar, CalendarProps } from '@/components/ui/calendar';
2025-03-08 07:02:56 +08:00
import {
PopoverRoot as Popover,
PopoverContent,
PopoverTrigger
} from '@/components/ui/tooltip/PopoverBase';
import { formatDate } from '@/lib';
2025-04-03 07:01:11 +08:00
export type DatePickerProps = Omit<CalendarProps, 'selected'> & {
2025-03-08 07:02:56 +08:00
dateFormat?: string;
placeholder?: string;
2025-04-03 07:01:11 +08:00
selected?: Date;
onSelect: (date: Date | undefined) => void;
2025-03-18 06:20:15 +08:00
};
2025-03-08 07:02:56 +08:00
export function DatePicker({
dateFormat = 'lll',
2025-03-18 06:20:15 +08:00
placeholder = 'Pick a date',
selected,
2025-04-03 07:01:11 +08:00
onSelect,
2025-03-18 06:20:15 +08:00
...props
2025-03-08 07:02:56 +08:00
}: DatePickerProps) {
return (
<Popover>
<PopoverTrigger asChild>
<Button
2025-03-18 06:20:15 +08:00
variant={'ghost'}
prefix={<CalendarIcon />}
2025-03-08 07:02:56 +08:00
className={cn(
2025-03-18 06:20:15 +08:00
'justify-start text-left font-normal',
!selected && 'text-muted-foreground'
2025-03-08 07:02:56 +08:00
)}>
2025-03-18 06:20:15 +08:00
{selected ? (
2025-03-08 07:02:56 +08:00
formatDate({
2025-03-18 06:20:15 +08:00
date: selected as Date,
2025-03-08 07:02:56 +08:00
format: dateFormat
})
) : (
<span>{placeholder}</span>
)}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0">
2025-04-03 07:01:11 +08:00
<Calendar mode="single" selected={selected} onSelect={onSelect} initialFocus />
2025-03-08 07:02:56 +08:00
</PopoverContent>
</Popover>
);
}