|
| 1 | +<script lang="ts"> |
| 2 | + import type { ComponentProps } from "svelte"; |
| 3 | + import type Calendar from "./calendar.svelte"; |
| 4 | + import CalendarMonthSelect from "./calendar-month-select.svelte"; |
| 5 | + import CalendarYearSelect from "./calendar-year-select.svelte"; |
| 6 | + import { DateFormatter, getLocalTimeZone, type DateValue } from "@internationalized/date"; |
| 7 | +
|
| 8 | + let { |
| 9 | + captionLayout, |
| 10 | + months, |
| 11 | + monthFormat, |
| 12 | + years, |
| 13 | + yearFormat, |
| 14 | + month, |
| 15 | + locale, |
| 16 | + placeholder = $bindable(), |
| 17 | + monthIndex = 0, |
| 18 | + }: { |
| 19 | + captionLayout: ComponentProps<typeof Calendar>["captionLayout"]; |
| 20 | + months: ComponentProps<typeof CalendarMonthSelect>["months"]; |
| 21 | + monthFormat: ComponentProps<typeof CalendarMonthSelect>["monthFormat"]; |
| 22 | + years: ComponentProps<typeof CalendarYearSelect>["years"]; |
| 23 | + yearFormat: ComponentProps<typeof CalendarYearSelect>["yearFormat"]; |
| 24 | + month: DateValue; |
| 25 | + placeholder: DateValue | undefined; |
| 26 | + locale: string; |
| 27 | + monthIndex: number; |
| 28 | + } = $props(); |
| 29 | +
|
| 30 | + function formatYear(date: DateValue) { |
| 31 | + const dateObj = date.toDate(getLocalTimeZone()); |
| 32 | + if (typeof yearFormat === "function") return yearFormat(dateObj.getFullYear()); |
| 33 | + return new DateFormatter(locale, { year: yearFormat }).format(dateObj); |
| 34 | + } |
| 35 | +
|
| 36 | + function formatMonth(date: DateValue) { |
| 37 | + const dateObj = date.toDate(getLocalTimeZone()); |
| 38 | + if (typeof monthFormat === "function") return monthFormat(dateObj.getMonth() + 1); |
| 39 | + return new DateFormatter(locale, { month: monthFormat }).format(dateObj); |
| 40 | + } |
| 41 | +</script> |
| 42 | + |
| 43 | +{#snippet MonthSelect()} |
| 44 | + <CalendarMonthSelect |
| 45 | + {months} |
| 46 | + {monthFormat} |
| 47 | + value={month.month} |
| 48 | + onchange={(e) => { |
| 49 | + if (!placeholder) return; |
| 50 | + const v = Number.parseInt(e.currentTarget.value); |
| 51 | + const newPlaceholder = placeholder.set({ month: v }); |
| 52 | + placeholder = newPlaceholder.subtract({ months: monthIndex }); |
| 53 | + }} |
| 54 | + /> |
| 55 | +{/snippet} |
| 56 | + |
| 57 | +{#snippet YearSelect()} |
| 58 | + <CalendarYearSelect {years} {yearFormat} value={month.year} /> |
| 59 | +{/snippet} |
| 60 | + |
| 61 | +{#if captionLayout === "dropdown"} |
| 62 | + {@render MonthSelect()} |
| 63 | + {@render YearSelect()} |
| 64 | +{:else if captionLayout === "dropdown-months"} |
| 65 | + {@render MonthSelect()} |
| 66 | + {#if placeholder} |
| 67 | + {formatYear(placeholder)} |
| 68 | + {/if} |
| 69 | +{:else if captionLayout === "dropdown-years"} |
| 70 | + {#if placeholder} |
| 71 | + {formatMonth(placeholder)} |
| 72 | + {/if} |
| 73 | + {@render YearSelect()} |
| 74 | +{:else} |
| 75 | + {formatMonth(month)} {formatYear(month)} |
| 76 | +{/if} |
0 commit comments