From 63ce0f8f6550e8444c73cc683dd1b05b4eecee31 Mon Sep 17 00:00:00 2001 From: Hunter Johnston Date: Fri, 1 Dec 2023 19:33:19 -0500 Subject: [PATCH] multiple calendar types --- src/lib/bits/calendar/_types.ts | 52 ++++++++++++++----- .../bits/calendar/components/calendar.svelte | 7 ++- src/lib/bits/calendar/ctx.ts | 2 +- src/lib/bits/calendar/types.ts | 3 +- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/lib/bits/calendar/_types.ts b/src/lib/bits/calendar/_types.ts index f889d1c3c..3af0c0009 100644 --- a/src/lib/bits/calendar/_types.ts +++ b/src/lib/bits/calendar/_types.ts @@ -8,21 +8,47 @@ import type { AsChild, OnChangeFn } from "$lib/internal"; import type { DateValue } from "@internationalized/date"; import type { CreateCalendarProps } from "@melt-ui/svelte"; -type Props = Expand< - Omit< - CreateCalendarProps, - | "placeholder" - | "defaultPlaceholder" - | "value" - | "defaultValue" - | "onPlaceholderChange" - | "onValueChange" - | "ids" - > & { +type OmitCalendarProps = Omit< + T, + | "placeholder" + | "defaultPlaceholder" + | "value" + | "defaultValue" + | "onPlaceholderChange" + | "onValueChange" + | "ids" +>; + +type Props = Expand< + OmitCalendarProps> & { + /** + * The selected date value. This updates as the user selects + * date(s) in the calendar. + * + * You can bind this to a value to programmatically control the + * value state. + */ + value?: CreateCalendarProps["defaultValue"]; + + /** + * A callback function called when the value changes. + */ + onValueChange?: OnChangeFn["defaultValue"]>; + + /** + * The placeholder date, used to display the calendar when no + * date is selected. This updates as the user navigates + * the calendar. + * + * You can bind this to a value to programmatically control the + * placeholder state. + */ placeholder?: DateValue; - value?: DateValue; + + /** + * A callback function called when the placeholder changes. + */ onPlaceholderChange?: OnChangeFn; - onValueChange?: OnChangeFn; } & AsChild >; diff --git a/src/lib/bits/calendar/components/calendar.svelte b/src/lib/bits/calendar/components/calendar.svelte index 121383571..32bb938aa 100644 --- a/src/lib/bits/calendar/components/calendar.svelte +++ b/src/lib/bits/calendar/components/calendar.svelte @@ -3,7 +3,8 @@ import { setCtx, getAttrs } from "../ctx.js"; import type { Props } from "../types.js"; - type $$Props = Props; + type Multiple = $$Generic; + type $$Props = Props; export let placeholder: $$Props["placeholder"] = undefined; export let onPlaceholderChange: $$Props["onPlaceholderChange"] = undefined; @@ -22,6 +23,7 @@ export let fixedWeeks: $$Props["fixedWeeks"] = undefined; export let calendarLabel: $$Props["calendarLabel"] = undefined; export let weekdayFormat: $$Props["weekdayFormat"] = undefined; + export let multiple: $$Props["multiple"] = false as Multiple; export let asChild: $$Props["asChild"] = false; export let id: $$Props["id"] = undefined; @@ -51,6 +53,7 @@ fixedWeeks, calendarLabel, weekdayFormat, + multiple, onPlaceholderChange: ({ next }) => { if (placeholder !== next) { onPlaceholderChange?.(next); @@ -58,7 +61,7 @@ } return next; }, - onValueChange: ({ next }) => { + onValueChange: ({ next }: { next: $$Props["value"] }) => { if (value !== next) { onValueChange?.(next); value = next; diff --git a/src/lib/bits/calendar/ctx.ts b/src/lib/bits/calendar/ctx.ts index a649a2069..af5ed595e 100644 --- a/src/lib/bits/calendar/ctx.ts +++ b/src/lib/bits/calendar/ctx.ts @@ -26,7 +26,7 @@ export const getAttrs = createBitAttrs(NAME, PARTS); type GetReturn = CalendarReturn; -export function setCtx(props: CreateCalendarProps) { +export function setCtx(props: CreateCalendarProps) { const calendar = createCalendar(removeUndefined(props)); setContext(NAME, calendar); return { diff --git a/src/lib/bits/calendar/types.ts b/src/lib/bits/calendar/types.ts index 3de587207..5a810ad5d 100644 --- a/src/lib/bits/calendar/types.ts +++ b/src/lib/bits/calendar/types.ts @@ -8,7 +8,8 @@ import type { } from "svelte/elements"; import type * as I from "./_types.js"; -type Props = I.Props & Omit; +type Props = I.Props & + Omit; type PrevButtonProps = I.PrevButtonProps & HTMLButtonAttributes;