Files
gpx.studio/website/src/lib/components/ui/date-picker/DatePicker.svelte

51 lines
1.6 KiB
Svelte
Raw Normal View History

2024-06-13 17:36:43 +02:00
<script lang="ts">
2025-10-18 19:21:10 +02:00
import CalendarIcon from '@lucide/svelte/icons/calendar';
2025-06-21 21:07:36 +02:00
import { DateFormatter, type DateValue, getLocalTimeZone } from '@internationalized/date';
import { cn } from '$lib/utils.js';
2025-10-18 19:21:10 +02:00
import { buttonVariants } from '$lib/components/ui/button/index.js';
2025-06-21 21:07:36 +02:00
import { Calendar } from '$lib/components/ui/calendar/index.js';
import * as Popover from '$lib/components/ui/popover/index.js';
2024-06-13 17:36:43 +02:00
2025-10-18 19:21:10 +02:00
let {
value = $bindable<DateValue | undefined>(),
placeholder = 'Pick a date',
disabled = false,
locale,
class: className = '',
2025-10-24 20:07:15 +02:00
onchange = () => {},
2025-10-18 19:21:10 +02:00
}: {
value?: DateValue;
placeholder?: string;
disabled?: boolean;
locale: string;
class?: string;
2025-10-24 20:07:15 +02:00
onchange?: (date: DateValue | undefined) => void;
2025-10-18 19:21:10 +02:00
} = $props();
2024-06-13 17:36:43 +02:00
2025-06-21 21:07:36 +02:00
const df = new DateFormatter(locale, {
dateStyle: 'long',
});
2025-10-18 19:21:10 +02:00
let contentRef = $state<HTMLElement | null>(null);
2024-06-13 17:36:43 +02:00
</script>
<Popover.Root>
2025-10-18 19:21:10 +02:00
<Popover.Trigger
class={cn(
buttonVariants({
variant: 'outline',
class: 'justify-start text-left font-normal',
}),
!value && 'text-muted-foreground',
className
)}
{disabled}
>
<CalendarIcon />
{value ? df.format(value.toDate(getLocalTimeZone())) : placeholder}
2025-06-21 21:07:36 +02:00
</Popover.Trigger>
2025-10-18 19:21:10 +02:00
<Popover.Content bind:ref={contentRef} class="w-auto p-0">
2025-10-24 20:07:15 +02:00
<Calendar type="single" captionLayout="dropdown" bind:value onValueChange={onchange} />
2025-06-21 21:07:36 +02:00
</Popover.Content>
2024-06-13 17:36:43 +02:00
</Popover.Root>