Skip to content

Commit

Permalink
multiple calendar types
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte committed Dec 2, 2023
1 parent 5af6b8e commit 63ce0f8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
52 changes: 39 additions & 13 deletions src/lib/bits/calendar/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = Omit<
T,
| "placeholder"
| "defaultPlaceholder"
| "value"
| "defaultValue"
| "onPlaceholderChange"
| "onValueChange"
| "ids"
>;

type Props<Multiple extends boolean> = Expand<
OmitCalendarProps<CreateCalendarProps<Multiple>> & {
/**
* 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<Multiple>["defaultValue"];

/**
* A callback function called when the value changes.
*/
onValueChange?: OnChangeFn<CreateCalendarProps<Multiple>["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<DateValue>;
onValueChange?: OnChangeFn<DateValue | undefined>;
} & AsChild
>;

Expand Down
7 changes: 5 additions & 2 deletions src/lib/bits/calendar/components/calendar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import { setCtx, getAttrs } from "../ctx.js";
import type { Props } from "../types.js";
type $$Props = Props;
type Multiple = $$Generic<boolean>;
type $$Props = Props<Multiple>;
export let placeholder: $$Props["placeholder"] = undefined;
export let onPlaceholderChange: $$Props["onPlaceholderChange"] = undefined;
Expand All @@ -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;
Expand Down Expand Up @@ -51,14 +53,15 @@
fixedWeeks,
calendarLabel,
weekdayFormat,
multiple,
onPlaceholderChange: ({ next }) => {
if (placeholder !== next) {
onPlaceholderChange?.(next);
placeholder = next;
}
return next;
},
onValueChange: ({ next }) => {
onValueChange: ({ next }: { next: $$Props["value"] }) => {
if (value !== next) {
onValueChange?.(next);
value = next;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/bits/calendar/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getAttrs = createBitAttrs(NAME, PARTS);

type GetReturn = CalendarReturn;

export function setCtx(props: CreateCalendarProps) {
export function setCtx<Multiple extends boolean>(props: CreateCalendarProps<Multiple>) {
const calendar = createCalendar(removeUndefined(props));
setContext(NAME, calendar);
return {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/bits/calendar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import type {
} from "svelte/elements";
import type * as I from "./_types.js";

type Props = I.Props & Omit<HTMLDivAttributes, "placeholder">;
type Props<Multiple extends boolean = false> = I.Props<Multiple> &
Omit<HTMLDivAttributes, "placeholder">;

type PrevButtonProps = I.PrevButtonProps & HTMLButtonAttributes;

Expand Down

0 comments on commit 63ce0f8

Please sign in to comment.