feat: migrate RangeCalendar public components and demos

This commit is contained in:
maoyingjie
2026-10-26 21:25:25 +08:00
parent ebe81f28c7
commit 07f6aeadb6
43 changed files with 2420 additions and 53 deletions
@@ -489,6 +489,12 @@ export const CalendarCell = defineComponent({
"data-selected": value.isSelected || undefined,
"data-selection-start": value.isSelectionStart || undefined,
"data-selection-end": value.isSelectionEnd || undefined,
"data-selection-in-range":
(ctx.range &&
value.isSelected &&
!value.isSelectionStart &&
!value.isSelectionEnd) ||
undefined,
"data-invalid": value.isInvalid || undefined,
"aria-invalid": value.isInvalid || undefined,
"data-today": value.isToday || undefined,
@@ -566,6 +572,9 @@ export const CalendarCell = defineComponent({
{
class: `hv-${ctx.prefix}__cell-button`,
"data-slot": `${ctx.prefix}-cell-button`,
"data-disabled": value.isDisabled || undefined,
"data-hovered": value.isHovered || undefined,
"data-pressed": value.isPressed || undefined,
"data-selected":
value.isSelectionStart ||
value.isSelectionEnd ||
@@ -0,0 +1,98 @@
<script setup lang="ts">
// Adapted from HeroUI ce90ea3, Apache-2.0; see third-party/HEROUI-LICENSE.
import { useAttrs } from "vue";
import type { DateValue } from "@internationalized/date";
import {
useRangeCalendar,
type RangeCalendarOptions,
} from "./use-range-calendar";
import {
copyCalendarRange,
type CalendarRangeModel,
} from "../date/range-selection";
import { calendarHeading } from "../date/calendar";
defineOptions({ inheritAttrs: false });
const props = defineProps<RangeCalendarOptions>();
const emit = defineEmits<{
"update:modelValue": [value: CalendarRangeModel];
change: [value: CalendarRangeModel];
"update:focusedValue": [value: DateValue];
focusChange: [value: DateValue];
"update:isYearPickerOpen": [value: boolean];
yearPickerOpenChange: [value: boolean];
}>();
defineSlots<{
default?: (state: {
value: CalendarRangeModel;
anchorDate: import("@internationalized/date").CalendarDate | null;
highlightedRange: CalendarRangeModel;
isDisabled: boolean;
isReadOnly: boolean;
isInvalid: boolean;
focusedValue: DateValue;
visibleRange: { start: DateValue; end: DateValue };
isYearPickerOpen: boolean;
}) => unknown;
}>();
const attrs = useAttrs();
const view = useRangeCalendar(props, emit);
const {
root,
focus,
tabStop,
focusDate,
anchor,
options,
visible,
dir,
disabled,
readOnly,
invalid,
yearOpen,
model,
anchorDate,
highlightedRange,
} = view;
defineExpose({
get element() {
return root.value;
},
focus: () => {
if (tabStop.value) focusDate(tabStop.value, true);
},
});
</script>
<template>
<component
:is="as ?? 'div'"
ref="root"
v-bind="attrs"
class="hv-range-calendar"
:class="{
'hv-range-calendar--week-view': visible.unit === 'weeks',
'hv-range-calendar--day-view': visible.unit === 'days',
}"
role="group"
:dir="dir"
data-slot="range-calendar"
:aria-label="
(attrs['aria-label'] as string | undefined) ??
(attrs['aria-labelledby'] ? undefined : calendarHeading(anchor, options))
"
:aria-disabled="disabled || undefined"
:data-disabled="disabled || undefined"
:aria-invalid="invalid || undefined"
>
<slot
:value="copyCalendarRange(model.value.value)"
:anchor-date="anchorDate?.copy() ?? null"
:highlighted-range="copyCalendarRange(highlightedRange)"
:is-disabled="disabled"
:is-read-only="readOnly"
:is-invalid="invalid"
:focused-value="focus.copy()"
:visible-range="{ start: visible.start.copy(), end: visible.end.copy() }"
:is-year-picker-open="yearOpen"
/>
</component>
</template>
@@ -0,0 +1,21 @@
// Shared parts derive their namespace and state from the nearest calendar owner.
export {
CalendarHeader as RangeCalendarHeader,
CalendarHeading as RangeCalendarHeading,
CalendarNavButton as RangeCalendarNavButton,
CalendarGrid as RangeCalendarGrid,
CalendarGridHeader as RangeCalendarGridHeader,
CalendarGridBody as RangeCalendarGridBody,
CalendarHeaderCell as RangeCalendarHeaderCell,
CalendarCellIndicator as RangeCalendarCellIndicator,
} from "../calendar/parts";
import { CalendarCell } from "../calendar/parts";
// Vue's options-component expose() is runtime-only in inferred declarations.
// Preserve the existing typed props/slots while declaring its actual public ref.
export const RangeCalendarCell = CalendarCell as typeof CalendarCell & {
new (): InstanceType<typeof CalendarCell> & {
readonly element: HTMLButtonElement | undefined;
focus: () => void;
};
};
@@ -27,7 +27,7 @@ import {
import { calendarDayState, dateValueKey } from "../date/value";
import { calendarDuration, calendarRange } from "../date/calendar";
/** Private schema for the next public shell; not exported from the package root. */
/** Public range selection options; geometry and focus share the Calendar owner. */
export interface RangeCalendarOptions extends Omit<
CalendarViewProps,
"isDateUnavailable"