Files
heroui-vue/tests/package/range-calendar-contract.ts
T

138 lines
4.2 KiB
TypeScript

import { h } from "vue";
import {
CalendarDate,
CalendarDateTime,
type DateValue,
} from "@internationalized/date";
import {
RangeCalendar,
RangeCalendarRoot,
RangeCalendarCell,
RangeCalendarGrid,
CalendarYearPickerCell,
rangeCalendarVariants,
type RangeCalendarProps,
type RangeCalendarRootProps,
type RangeCalendarCellProps,
type RangeCalendarGridProps,
type RangeCalendarCellRenderProps,
type CalendarDateRange,
type CalendarRangeModel,
type RangeCalendarVariants,
} from "@heroui-vue/components";
const day = new CalendarDate(2024, 2, 10);
const range: CalendarDateRange<CalendarDate> = {
start: day,
end: day.add({ days: 2 }),
};
const mixed: CalendarDateRange = {
start: new CalendarDateTime(2024, 2, 10, 9),
end: day,
};
const props: RangeCalendarProps = {
modelValue: range,
defaultValue: null,
focusedValue: day,
allowsNonContiguousRanges: true,
firstDayOfWeek: "mon",
visibleDuration: { weeks: 2 },
isDateUnavailable: (date, anchor) => {
const value: DateValue = date;
const original: CalendarDate | null = anchor;
// @ts-expect-error anchor has calendar semantics, not a JS Date
const invalid: Date | null = anchor;
void [value, original, invalid];
return false;
},
"onUpdate:modelValue": (value) => {
const model: CalendarRangeModel = value;
// @ts-expect-error proposal is a range or null
const invalid: string = value;
void [model, invalid];
},
"onUpdate:focusedValue": (date) => {
const value: DateValue = date;
void value;
},
"onUpdate:isYearPickerOpen": (open) => {
const value: boolean = open;
void value;
},
};
const root: RangeCalendarRootProps = props;
const cell: RangeCalendarCellProps = { date: day };
const grid: RangeCalendarGridProps = {
offset: { months: 1 },
weekdayStyle: "long",
};
// @ts-expect-error a scalar DateValue is not a range
const scalar: RangeCalendarProps = { modelValue: day };
// @ts-expect-error ranges require both endpoints
const partial: RangeCalendarProps = { modelValue: { start: day } };
// @ts-expect-error a range is not an array
const array: RangeCalendarProps = { modelValue: [day, day] };
// @ts-expect-error dates must retain DateValue semantics
const native: RangeCalendarCellProps = { date: new Date() };
// @ts-expect-error no extra selection mode
const mode: RangeCalendarProps = { selectionMode: "multiple" };
const duration: RangeCalendarProps = {
// @ts-expect-error visible duration takes exactly one unit
visibleDuration: { months: 1, weeks: 1 },
};
type ActualRoot = InstanceType<typeof RangeCalendar>["$props"];
// @ts-expect-error actual component rejects partial values too
const actual: ActualRoot = { modelValue: { end: day } };
type RootSlot = Parameters<
NonNullable<InstanceType<typeof RangeCalendar>["$slots"]["default"]>
>[0];
const snapshot = {} as RootSlot;
const value: CalendarRangeModel = snapshot.value;
const anchor: CalendarDate | null = snapshot.anchorDate;
const focused: DateValue = snapshot.focusedValue;
// @ts-expect-error slot value is not any or a scalar
const badValue: DateValue = snapshot.value;
type CellSlot = Parameters<
NonNullable<InstanceType<typeof RangeCalendarCell>["$slots"]["default"]>
>[0];
const state = {} as CellSlot;
const renderProps: RangeCalendarCellRenderProps = state;
const start: boolean = state.isSelectionStart;
// @ts-expect-error cell states remain typed
const badStart: string = state.isSelectionStart;
const variants: RangeCalendarVariants = {};
const classes: string = rangeCalendarVariants(variants).cell();
h(RangeCalendarRoot, props);
h(RangeCalendarGrid, grid);
h(CalendarYearPickerCell, { year: 2024 });
void [
range,
mixed,
root,
cell,
scalar,
partial,
array,
native,
mode,
duration,
actual,
value,
anchor,
focused,
badValue,
renderProps,
start,
badStart,
classes,
];
const instance = {} as InstanceType<typeof RangeCalendar>;
instance.focus();
const element: HTMLElement | undefined = instance.element;
const cellInstance = {} as InstanceType<typeof RangeCalendarCell>;
cellInstance.focus();
const button: HTMLButtonElement | undefined = cellInstance.element;
// @ts-expect-error a button ref is not an input
const input: HTMLInputElement | undefined = cellInstance.element;
void [element, button, input];