feat: migrate public DateRangePicker with shared range field and calendar owners
This commit is contained in:
@@ -71,11 +71,12 @@
|
||||
@apply rounded-e-none pe-2;
|
||||
}
|
||||
|
||||
/* The pinned slot="start"/"end" endpoint selector becomes data-endpoint. */
|
||||
.hv-date-input-group:has(.hv-date-range-picker__range-separator) & {
|
||||
&[slot="start"] {
|
||||
&[data-endpoint="start"] {
|
||||
@apply flex-none pe-0;
|
||||
}
|
||||
&[slot="end"] {
|
||||
&[data-endpoint="end"] {
|
||||
@apply ps-0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/* Adapted from HeroUI ce90ea3 date-range-picker.css. Copyright 2025 NextUI Inc.
|
||||
* Apache-2.0; see third-party/HEROUI-LICENSE. hv- namespace, Vue/Reka states. */
|
||||
|
||||
.hv-date-range-picker {
|
||||
@apply inline-flex flex-col gap-1;
|
||||
|
||||
.hv-date-input-group__suffix,
|
||||
.hv-date-input-group__prefix {
|
||||
@apply pointer-events-auto;
|
||||
}
|
||||
}
|
||||
|
||||
.hv-date-range-picker__trigger {
|
||||
@apply inline-flex w-full items-center rounded-field p-1 text-sm no-highlight;
|
||||
|
||||
cursor: var(--cursor-interactive);
|
||||
|
||||
/**
|
||||
* Transitions
|
||||
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
|
||||
*/
|
||||
transition: box-shadow 150ms var(--ease-out);
|
||||
@apply motion-reduce:transition-none;
|
||||
|
||||
&:focus-visible:not([data-focused]),
|
||||
&[data-focus-visible="true"] {
|
||||
@apply status-focused;
|
||||
}
|
||||
|
||||
&:disabled,
|
||||
&[data-disabled="true"],
|
||||
&[aria-disabled="true"] {
|
||||
@apply status-disabled;
|
||||
}
|
||||
}
|
||||
|
||||
.hv-date-range-picker__trigger-indicator {
|
||||
@apply inline-flex size-4 items-center justify-center text-field-placeholder;
|
||||
}
|
||||
|
||||
.hv-date-range-picker__range-separator {
|
||||
@apply px-1 text-field-placeholder select-none;
|
||||
}
|
||||
|
||||
.hv-date-range-picker__popover {
|
||||
@apply w-fit origin-(--reka-popper-transform-origin) scrollbar-none overflow-y-auto overscroll-contain bg-overlay p-3;
|
||||
@apply motion-reduce:transition-none;
|
||||
|
||||
box-shadow: var(--overlay-shadow);
|
||||
border-radius: min(32px, calc(var(--radius) * 2.5));
|
||||
|
||||
&:focus-visible:not([data-focused]),
|
||||
&[data-focus-visible="true"] {
|
||||
@apply outline-none;
|
||||
}
|
||||
|
||||
&[data-entering="true"] {
|
||||
@apply animate-in duration-150 ease-smooth fade-in-0 zoom-in-95;
|
||||
|
||||
&[data-side^="top"] {
|
||||
@apply slide-in-from-bottom-1;
|
||||
}
|
||||
|
||||
&[data-side^="bottom"] {
|
||||
@apply slide-in-from-top-1;
|
||||
}
|
||||
|
||||
&[data-side^="left"] {
|
||||
@apply slide-in-from-right-1;
|
||||
}
|
||||
|
||||
&[data-side^="right"] {
|
||||
@apply slide-in-from-left-1;
|
||||
}
|
||||
}
|
||||
|
||||
&[data-exiting="true"] {
|
||||
@apply animate-out duration-100 ease-smooth zoom-out-95 fade-out;
|
||||
/* Stays non-interactive while closing, even if the exit animation is interrupted. */
|
||||
@apply pointer-events-none;
|
||||
}
|
||||
|
||||
&[data-exiting="true"],
|
||||
&[data-entering="true"] {
|
||||
@apply transition-[opacity,transform] will-change-[opacity,transform];
|
||||
}
|
||||
}
|
||||
|
||||
.hv-date-range-picker__popover .hv-range-calendar__nav-button-icon,
|
||||
.hv-date-range-picker__popover .hv-calendar__nav-button-icon {
|
||||
@apply rotate-0;
|
||||
}
|
||||
@@ -164,6 +164,7 @@
|
||||
@import "./components/date-input-group.css" layer(components);
|
||||
@import "./components/date-field.css" layer(components);
|
||||
@import "./components/date-picker.css" layer(components);
|
||||
@import "./components/date-range-picker.css" layer(components);
|
||||
@import "./components/time-field.css" layer(components);
|
||||
|
||||
@import "./components/calendar.css" layer(components);
|
||||
|
||||
@@ -1469,6 +1469,19 @@ export const datePickerVariants = tv({
|
||||
},
|
||||
});
|
||||
export type DatePickerVariants = VariantProps<typeof datePickerVariants>;
|
||||
// Ported from HeroUI ce90ea3, Apache-2.0; hv namespace only.
|
||||
export const dateRangePickerVariants = tv({
|
||||
slots: {
|
||||
base: "hv-date-range-picker",
|
||||
popover: "hv-date-range-picker__popover",
|
||||
rangeSeparator: "hv-date-range-picker__range-separator",
|
||||
trigger: "hv-date-range-picker__trigger",
|
||||
triggerIndicator: "hv-date-range-picker__trigger-indicator",
|
||||
},
|
||||
});
|
||||
export type DateRangePickerVariants = VariantProps<
|
||||
typeof dateRangePickerVariants
|
||||
>;
|
||||
export const dateInputGroupVariants = tv({
|
||||
slots: {
|
||||
base: "hv-date-input-group",
|
||||
|
||||
@@ -1,17 +1,23 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
mergeProps,
|
||||
provide,
|
||||
type ComputedRef,
|
||||
type PropType,
|
||||
type SlotsType,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import { DateRangeFieldInput } from "reka-ui";
|
||||
|
||||
import { dateInputGroupVariants } from "@heroui-vue/styles";
|
||||
import {
|
||||
dateSegmentBaseKey,
|
||||
dateGroupKey,
|
||||
dateRangeFieldKey,
|
||||
dateSegmentsKey,
|
||||
useDateField,
|
||||
type DateSegment,
|
||||
@@ -20,34 +26,72 @@ import DateInputGroupSegment from "./DateInputGroupSegment.vue";
|
||||
export default defineComponent({
|
||||
name: "DateInputGroupInput",
|
||||
inheritAttrs: false,
|
||||
props: { as: { type: String, default: "div" } },
|
||||
props: {
|
||||
as: { type: String, default: "div" },
|
||||
/** Range endpoint selector. Vue reserves `slot`, so the pinned source's
|
||||
* `slot="start"` / `slot="end"` becomes this typed prop. */
|
||||
endpoint: String as PropType<"start" | "end">,
|
||||
},
|
||||
slots: Object as SlotsType<{
|
||||
default?: (props: { segment: DateSegment; index: number }) => VNodeChild;
|
||||
}>,
|
||||
setup(props, { attrs, slots }) {
|
||||
const field = useDateField();
|
||||
const adapter = inject(dateSegmentBaseKey)!;
|
||||
const base = adapter.base;
|
||||
const styles = inject(dateGroupKey, undefined);
|
||||
const segments = inject(dateSegmentsKey)!;
|
||||
const range = inject(dateRangeFieldKey, undefined);
|
||||
const singleAdapter = inject(dateSegmentBaseKey, undefined);
|
||||
const singleSegments = inject(dateSegmentsKey, undefined);
|
||||
let adapter: NonNullable<typeof singleAdapter>;
|
||||
let segments: ComputedRef<{ part: DateSegment["part"]; value: string }[]>;
|
||||
if (props.endpoint) {
|
||||
if (!range)
|
||||
throw new Error(
|
||||
"DateFieldInput endpoint requires a DateRangePicker root.",
|
||||
);
|
||||
const root = range.root;
|
||||
const endpoint = props.endpoint;
|
||||
adapter = {
|
||||
// Endpoint-specific segment values share the rest of the range field
|
||||
// context, so parts keep using one locale/formatter/disabled source.
|
||||
base: {
|
||||
...root,
|
||||
segmentValues: computed(() => root.segmentValues[endpoint].value),
|
||||
},
|
||||
input: DateRangeFieldInput,
|
||||
kind: "date",
|
||||
endpoint,
|
||||
} as unknown as NonNullable<typeof singleAdapter>;
|
||||
segments = computed(() => root.segmentContents.value[endpoint]);
|
||||
provide(dateSegmentBaseKey, adapter);
|
||||
} else {
|
||||
if (!singleAdapter || !singleSegments)
|
||||
throw new Error(
|
||||
'DateFieldInput requires a DateField or TimeField root, or endpoint="start"/"end" inside DateRangePicker.',
|
||||
);
|
||||
adapter = singleAdapter;
|
||||
segments = singleSegments;
|
||||
}
|
||||
const base = adapter.base;
|
||||
return () =>
|
||||
h(
|
||||
props.as,
|
||||
mergeProps(attrs, {
|
||||
class: (styles?.value ?? dateInputGroupVariants()).input(),
|
||||
"data-slot": "date-input-group-input",
|
||||
"data-endpoint": props.endpoint || undefined,
|
||||
}),
|
||||
segments.value.map((part, index) => {
|
||||
const partName = part.part;
|
||||
const isPlaceholder =
|
||||
part.part in base.segmentValues.value &&
|
||||
partName in base.segmentValues.value &&
|
||||
base.segmentValues.value[
|
||||
part.part as keyof typeof base.segmentValues.value
|
||||
partName as keyof typeof base.segmentValues.value
|
||||
] === null;
|
||||
let text = part.value;
|
||||
if (
|
||||
!isPlaceholder &&
|
||||
(adapter.kind !== "time" || part.part === "hour") &&
|
||||
["month", "day", "hour", "minute", "second"].includes(part.part) &&
|
||||
(adapter.kind !== "time" || partName === "hour") &&
|
||||
["month", "day", "hour", "minute", "second"].includes(partName) &&
|
||||
/^\d+$/.test(text)
|
||||
) {
|
||||
if (field.leadingZeros() === true) text = text.padStart(2, "0");
|
||||
@@ -56,15 +100,16 @@ export default defineComponent({
|
||||
}
|
||||
const segment: DateSegment = {
|
||||
...part,
|
||||
type: part.part,
|
||||
part: partName,
|
||||
type: partName,
|
||||
text,
|
||||
value: text,
|
||||
isPlaceholder,
|
||||
isEditable: part.part !== "literal" && part.part !== "timeZoneName",
|
||||
isEditable: partName !== "literal" && partName !== "timeZoneName",
|
||||
};
|
||||
return h(
|
||||
"span",
|
||||
{ key: `${part.part}-${index}`, style: { display: "contents" } },
|
||||
{ key: `${partName}-${index}`, style: { display: "contents" } },
|
||||
slots.default?.({ segment, index }) ??
|
||||
h(DateInputGroupSegment, { segment }),
|
||||
);
|
||||
|
||||
@@ -47,7 +47,11 @@ export default defineComponent({
|
||||
return () =>
|
||||
h(
|
||||
adapter.input,
|
||||
{ part: props.segment.part, asChild: true },
|
||||
{
|
||||
part: props.segment.part,
|
||||
...(adapter.endpoint ? { type: adapter.endpoint } : {}),
|
||||
asChild: true,
|
||||
},
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
|
||||
@@ -49,4 +49,14 @@ export const dateSegmentBaseKey: InjectionKey<{
|
||||
>;
|
||||
input: Component;
|
||||
kind: "date" | "time";
|
||||
/** Range endpoints reuse the same segment parts with an explicit start/end field. */
|
||||
endpoint?: "start" | "end";
|
||||
}> = Symbol("hv-date-segment-base");
|
||||
|
||||
/** Marker provided by the DateRangePicker private field body. Only inputs with
|
||||
* an explicit endpoint consume it; nested TimeFields keep their own segment base. */
|
||||
export const dateRangeFieldKey: InjectionKey<{
|
||||
root: NonNullable<
|
||||
ReturnType<typeof import("reka-ui").injectDateRangeFieldRootContext>
|
||||
>;
|
||||
}> = Symbol("hv-date-range-field");
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<script setup lang="ts">
|
||||
import { provide } from "vue";
|
||||
import { injectDateRangeFieldRootContext } from "reka-ui";
|
||||
import { dateRangeFieldKey } from "../date-field/context";
|
||||
defineOptions({ name: "DateRangeFieldBody" });
|
||||
const root = injectDateRangeFieldRootContext();
|
||||
provide(dateRangeFieldKey, { root });
|
||||
</script>
|
||||
<template>
|
||||
<slot />
|
||||
</template>
|
||||
@@ -0,0 +1,306 @@
|
||||
<script lang="ts">
|
||||
// Adapted from HeroUI ce90ea3 DateRangePicker, Apache-2.0; see third-party/HEROUI-LICENSE.
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
nextTick,
|
||||
onBeforeUnmount,
|
||||
onMounted,
|
||||
provide,
|
||||
shallowRef,
|
||||
watch,
|
||||
type PropType,
|
||||
type SlotsType,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import { PopoverRoot } from "reka-ui";
|
||||
import type { DateValue } from "@internationalized/date";
|
||||
import { dateRangePickerVariants } from "@heroui-vue/styles";
|
||||
import { useConfig } from "../../providers/context";
|
||||
import { useOverlayState } from "../../composables/useOverlayState";
|
||||
import { createPopoverContext, popoverKey } from "../popover/context";
|
||||
import { overlayNodes } from "../overlay/OverlayAction";
|
||||
import type { ValidationBehavior, ValidateResult } from "../field/context";
|
||||
import type { CalendarWeekday } from "../date/calendar";
|
||||
import type { CalendarRangeModel } from "../date/range-selection";
|
||||
import { useDateRangePickerOwner } from "./owner";
|
||||
import {
|
||||
dateRangePickerKey,
|
||||
type DateRangePickerEscapeOptions,
|
||||
type DateRangePickerRenderProps,
|
||||
} from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "DateRangePicker",
|
||||
inheritAttrs: false,
|
||||
slots: Object as SlotsType<{
|
||||
default?: (state: DateRangePickerRenderProps) => VNodeChild;
|
||||
}>,
|
||||
props: {
|
||||
modelValue: {
|
||||
type: Object as PropType<CalendarRangeModel>,
|
||||
default: undefined,
|
||||
},
|
||||
value: {
|
||||
type: Object as PropType<CalendarRangeModel>,
|
||||
default: undefined,
|
||||
},
|
||||
defaultValue: Object as PropType<CalendarRangeModel>,
|
||||
placeholderValue: [Object, null] as PropType<DateValue | null>,
|
||||
minValue: [Object, null] as PropType<DateValue | null>,
|
||||
maxValue: [Object, null] as PropType<DateValue | null>,
|
||||
isDateUnavailable: Function as PropType<(value: DateValue) => boolean>,
|
||||
allowsNonContiguousRanges: Boolean,
|
||||
granularity: String as PropType<"day" | "hour" | "minute" | "second">,
|
||||
hourCycle: Number as PropType<12 | 24>,
|
||||
hideTimeZone: Boolean,
|
||||
shouldForceLeadingZeros: { type: Boolean, default: undefined },
|
||||
locale: String,
|
||||
dir: String as PropType<"ltr" | "rtl">,
|
||||
firstDayOfWeek: String as PropType<CalendarWeekday>,
|
||||
pageBehavior: String as PropType<"visible" | "single">,
|
||||
as: { type: String, default: "div" },
|
||||
id: String,
|
||||
startName: String,
|
||||
endName: String,
|
||||
form: String,
|
||||
isDisabled: Boolean,
|
||||
disabled: Boolean,
|
||||
isReadOnly: Boolean,
|
||||
isRequired: Boolean,
|
||||
isInvalid: Boolean,
|
||||
autoFocus: Boolean,
|
||||
autoComplete: String,
|
||||
validationBehavior: String as PropType<ValidationBehavior>,
|
||||
validate: Function as PropType<
|
||||
(value: CalendarRangeModel) => ValidateResult
|
||||
>,
|
||||
open: { type: Boolean, default: undefined },
|
||||
isOpen: { type: Boolean, default: undefined },
|
||||
defaultOpen: Boolean,
|
||||
shouldCloseOnSelect: { type: Boolean, default: true },
|
||||
},
|
||||
emits: {
|
||||
"update:modelValue": (_value: CalendarRangeModel) => true,
|
||||
change: (_value: CalendarRangeModel) => true,
|
||||
"update:open": (_value: boolean) => true,
|
||||
"update:isOpen": (_value: boolean) => true,
|
||||
openChange: (_value: boolean) => true,
|
||||
},
|
||||
setup(props, { attrs, slots, emit, expose }) {
|
||||
const config = useConfig();
|
||||
const dir = computed<"ltr" | "rtl">(
|
||||
() => props.dir ?? config?.dir?.value ?? "ltr",
|
||||
);
|
||||
const state = useOverlayState({
|
||||
isOpen: () => props.open ?? props.isOpen,
|
||||
defaultOpen: props.defaultOpen,
|
||||
onOpenChange(value) {
|
||||
emit("update:open", value);
|
||||
emit("update:isOpen", value);
|
||||
emit("openChange", value);
|
||||
},
|
||||
});
|
||||
const owner = useDateRangePickerOwner(props, attrs, {
|
||||
onChange: (value) => {
|
||||
emit("update:modelValue", value);
|
||||
emit("change", value);
|
||||
},
|
||||
onReset: () => {},
|
||||
shouldCloseOnSelect: () => props.shouldCloseOnSelect,
|
||||
onSelectComplete: () => state.close(),
|
||||
});
|
||||
watch(state.isOpen, (open) => {
|
||||
if (!open) owner.settleField();
|
||||
});
|
||||
const trigger = shallowRef<HTMLElement>();
|
||||
const triggerId = shallowRef<string>();
|
||||
const escapeOptions = shallowRef<DateRangePickerEscapeOptions>({
|
||||
disabled: false,
|
||||
handlers: [],
|
||||
});
|
||||
const popover = createPopoverContext(state);
|
||||
provide(popoverKey, popover);
|
||||
provide(dateRangePickerKey, {
|
||||
state,
|
||||
trigger,
|
||||
triggerId,
|
||||
value: owner.accepted.value,
|
||||
dates: computed(() => {
|
||||
const value = owner.accepted.value.value;
|
||||
return value ? [value.start, value.end] : [];
|
||||
}),
|
||||
timeRange: owner.timeRange,
|
||||
setTimeRange: owner.setTimeRange,
|
||||
requestRange: owner.requestRange,
|
||||
minValue: computed(() => props.minValue ?? undefined),
|
||||
maxValue: computed(() => props.maxValue ?? undefined),
|
||||
isDateUnavailable: (value) => props.isDateUnavailable?.(value) ?? false,
|
||||
allowsNonContiguousRanges: () => !!props.allowsNonContiguousRanges,
|
||||
disabled: owner.field.field.disabled,
|
||||
readOnly: owner.field.field.readOnly,
|
||||
invalid: owner.field.field.invalid,
|
||||
locale: owner.locale,
|
||||
dir: owner.dir,
|
||||
labelledBy: owner.field.labelledBy,
|
||||
describedBy: owner.field.describedBy,
|
||||
firstDayOfWeek: () => props.firstDayOfWeek,
|
||||
pageBehavior: () => props.pageBehavior,
|
||||
placeholder: owner.placeholder,
|
||||
calendarFallback: owner.calendarFallback,
|
||||
escapeOptions,
|
||||
shouldCloseOnSelect: () => props.shouldCloseOnSelect,
|
||||
});
|
||||
let restoreFocus = false;
|
||||
const doc = typeof document === "undefined" ? undefined : document;
|
||||
function trackKeyboard(event: KeyboardEvent) {
|
||||
if (!event.metaKey && !event.ctrlKey && !event.altKey)
|
||||
restoreFocus = true;
|
||||
}
|
||||
function closeFromPicker(event: KeyboardEvent) {
|
||||
if (
|
||||
event.key !== "Escape" ||
|
||||
event.isComposing ||
|
||||
event.defaultPrevented ||
|
||||
!state.isOpen.value
|
||||
)
|
||||
return;
|
||||
const target = event.target as HTMLElement | null;
|
||||
if (!target) return;
|
||||
const content = doc?.getElementById(popover.contentId.value) ?? null;
|
||||
const inField = !!owner.root.value?.contains(target);
|
||||
const inContent = !!content?.contains(target);
|
||||
if (!inField && !inContent) return;
|
||||
// An open year layer owns its own Escape and restores focus itself.
|
||||
if (
|
||||
content?.querySelector(
|
||||
'[data-slot="calendar-year-picker-grid"][data-open]',
|
||||
)
|
||||
)
|
||||
return;
|
||||
const options = escapeOptions.value;
|
||||
for (const handler of options.handlers) handler(event);
|
||||
const cancelled = event.defaultPrevented;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (options.disabled || cancelled) return;
|
||||
state.close();
|
||||
}
|
||||
function onDocumentKeydown(event: KeyboardEvent) {
|
||||
trackKeyboard(event);
|
||||
closeFromPicker(event);
|
||||
}
|
||||
watch(
|
||||
state.isOpen,
|
||||
(open) => {
|
||||
if (open) doc?.addEventListener("keydown", onDocumentKeydown, true);
|
||||
else {
|
||||
doc?.removeEventListener("keydown", onDocumentKeydown, true);
|
||||
if (restoreFocus) {
|
||||
const run = () => trigger.value?.focus({ preventScroll: true });
|
||||
if (typeof requestAnimationFrame === "function")
|
||||
requestAnimationFrame(run);
|
||||
else void nextTick(run);
|
||||
}
|
||||
restoreFocus = false;
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
onMounted(() => {
|
||||
if (props.autoFocus) owner.focus();
|
||||
});
|
||||
onBeforeUnmount(() => {
|
||||
doc?.removeEventListener("keydown", onDocumentKeydown, true);
|
||||
});
|
||||
expose({
|
||||
element: owner.root,
|
||||
focus: owner.focus,
|
||||
open: state.open,
|
||||
close: state.close,
|
||||
toggle: state.toggle,
|
||||
});
|
||||
function setValue(value: CalendarRangeModel) {
|
||||
owner.accepted.request(value);
|
||||
}
|
||||
return () => {
|
||||
const base = owner.state();
|
||||
const pickerState = {
|
||||
value: owner.accepted.value.value,
|
||||
timeRange: owner.timeRange.value,
|
||||
setValue,
|
||||
setTimeRange: owner.setTimeRange,
|
||||
};
|
||||
const renderState: DateRangePickerRenderProps = {
|
||||
...base,
|
||||
isOpen: state.isOpen.value,
|
||||
timeRange: owner.timeRange.value,
|
||||
state: pickerState,
|
||||
open: state.open,
|
||||
close: state.close,
|
||||
toggle: state.toggle,
|
||||
};
|
||||
const raw = slots.default?.(renderState);
|
||||
const children = owner.field.prepare(
|
||||
Array.isArray(raw) ? raw : raw ? [raw] : [],
|
||||
true,
|
||||
);
|
||||
const overrides = {
|
||||
id: owner.field.field.controlId.value,
|
||||
role: "group",
|
||||
class: dateRangePickerVariants().base(),
|
||||
"data-slot": "date-range-picker",
|
||||
"data-disabled": base.isDisabled || undefined,
|
||||
"data-readonly": base.isReadOnly || undefined,
|
||||
"data-invalid": base.isInvalid || undefined,
|
||||
"data-required": base.isRequired || undefined,
|
||||
"data-focus-within": owner.focused.value || undefined,
|
||||
"aria-label": attrs["aria-label"],
|
||||
"aria-labelledby": owner.field.labelledBy(),
|
||||
"aria-describedby": owner.field.describedBy(),
|
||||
"aria-invalid": base.isInvalid || undefined,
|
||||
"aria-required": base.isRequired || undefined,
|
||||
dir: dir.value,
|
||||
ref: (element: unknown) => {
|
||||
owner.root.value = element as HTMLElement;
|
||||
},
|
||||
onFocusin: (event: FocusEvent) => {
|
||||
owner.focused.value = true;
|
||||
owner.focusVisible.value = (event.target as HTMLElement).matches(
|
||||
":focus-visible",
|
||||
);
|
||||
},
|
||||
onFocusout: (event: FocusEvent) => {
|
||||
if (
|
||||
!(event.currentTarget as HTMLElement).contains(
|
||||
event.relatedTarget as Node | null,
|
||||
)
|
||||
) {
|
||||
owner.focused.value = false;
|
||||
owner.focusVisible.value = false;
|
||||
}
|
||||
},
|
||||
};
|
||||
return h(
|
||||
PopoverRoot,
|
||||
{
|
||||
open: state.isOpen.value,
|
||||
modal: true,
|
||||
"onUpdate:open": state.setOpen,
|
||||
},
|
||||
{
|
||||
default: () =>
|
||||
owner.renderOwner(
|
||||
[
|
||||
...overlayNodes(children),
|
||||
...owner.inputNodes("date-range-picker"),
|
||||
],
|
||||
{ as: props.as, consumer: attrs, overrides },
|
||||
),
|
||||
},
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,82 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, useAttrs, watchEffect } from "vue";
|
||||
import { dateRangePickerVariants } from "@heroui-vue/styles";
|
||||
import type { OverlayPlacement } from "../overlay/placement";
|
||||
import PopoverContent from "../popover/PopoverContent.vue";
|
||||
import { useDateRangePicker } from "./context";
|
||||
defineOptions({ name: "DateRangePickerPopover", inheritAttrs: false });
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
as?: string;
|
||||
placement?: OverlayPlacement;
|
||||
offset?: number;
|
||||
crossOffset?: number;
|
||||
shouldFlip?: boolean;
|
||||
containerPadding?: number;
|
||||
to?: string | HTMLElement;
|
||||
isNonModal?: boolean;
|
||||
isDismissable?: boolean;
|
||||
isKeyboardDismissDisabled?: boolean;
|
||||
}>(),
|
||||
{
|
||||
as: "div",
|
||||
placement: "bottom",
|
||||
offset: 8,
|
||||
crossOffset: 0,
|
||||
shouldFlip: true,
|
||||
containerPadding: 12,
|
||||
isNonModal: undefined,
|
||||
isDismissable: true,
|
||||
isKeyboardDismissDisabled: false,
|
||||
},
|
||||
);
|
||||
const picker = useDateRangePicker();
|
||||
const attrs = useAttrs();
|
||||
const escapeHandlers = computed<((event: Event) => void)[]>(() => {
|
||||
const value = attrs.onEscapeKeyDown;
|
||||
return (Array.isArray(value) ? value : [value]).filter(
|
||||
(handler): handler is (event: Event) => void =>
|
||||
typeof handler === "function",
|
||||
);
|
||||
});
|
||||
watchEffect(() => {
|
||||
picker.escapeOptions.value = {
|
||||
disabled: props.isKeyboardDismissDisabled,
|
||||
handlers: escapeHandlers.value,
|
||||
};
|
||||
});
|
||||
onBeforeUnmount(() => {
|
||||
picker.escapeOptions.value = { disabled: false, handlers: [] };
|
||||
});
|
||||
const labelAttrs = computed(() => {
|
||||
const explicit = attrs["aria-labelledby"];
|
||||
const id = typeof explicit === "string" ? explicit : picker.triggerId.value;
|
||||
return id ? { "aria-labelledby": id } : {};
|
||||
});
|
||||
const contentAttrs = computed(() => ({ ...attrs, ...labelAttrs.value }));
|
||||
function openAutoFocus(event: Event) {
|
||||
// The calendar cell owns initial focus; the dialog surface must not steal it.
|
||||
event.preventDefault();
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<PopoverContent
|
||||
v-bind="contentAttrs"
|
||||
:as="as"
|
||||
:placement="placement"
|
||||
:offset="offset"
|
||||
:cross-offset="crossOffset"
|
||||
:should-flip="shouldFlip"
|
||||
:container-padding="containerPadding"
|
||||
:to="to"
|
||||
:is-non-modal="isNonModal"
|
||||
:is-dismissable="isDismissable"
|
||||
:is-keyboard-dismiss-disabled="isKeyboardDismissDisabled"
|
||||
:lang="(attrs.lang as string | undefined) ?? picker.locale.value"
|
||||
:class="dateRangePickerVariants().popover()"
|
||||
data-slot="date-range-picker-popover"
|
||||
@open-auto-focus="openAutoFocus"
|
||||
>
|
||||
<slot />
|
||||
</PopoverContent>
|
||||
</template>
|
||||
@@ -0,0 +1,25 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h, mergeProps, type PropType } from "vue";
|
||||
import { dateRangePickerVariants } from "@heroui-vue/styles";
|
||||
|
||||
export default defineComponent({
|
||||
name: "DateRangePickerRangeSeparator",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
as: { type: String, default: "span" },
|
||||
children: { type: String as PropType<string>, default: " - " },
|
||||
},
|
||||
setup(props, { attrs, slots }) {
|
||||
return () =>
|
||||
h(
|
||||
props.as,
|
||||
mergeProps(attrs, {
|
||||
"aria-hidden": "true",
|
||||
class: dateRangePickerVariants().rangeSeparator(),
|
||||
"data-slot": "date-range-picker-range-separator",
|
||||
}),
|
||||
slots.default?.() ?? props.children,
|
||||
);
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,86 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, useAttrs, watch } from "vue";
|
||||
import {
|
||||
injectPopoverRootContext,
|
||||
PopoverAnchor,
|
||||
useForwardExpose,
|
||||
} from "reka-ui";
|
||||
import { dateRangePickerVariants } from "@heroui-vue/styles";
|
||||
import OverlayAction from "../overlay/OverlayAction";
|
||||
import { usePopover } from "../popover/context";
|
||||
import { useDateRangePicker } from "./context";
|
||||
defineOptions({ name: "DateRangePickerTrigger", inheritAttrs: false });
|
||||
const props = withDefaults(
|
||||
defineProps<{ as?: string; asChild?: boolean; isDisabled?: boolean }>(),
|
||||
{ as: "button" },
|
||||
);
|
||||
const picker = useDateRangePicker();
|
||||
const popover = usePopover();
|
||||
const root = injectPopoverRootContext();
|
||||
const attrs = useAttrs();
|
||||
const { forwardRef, currentElement } = useForwardExpose();
|
||||
const focusVisible = ref(false);
|
||||
const triggerId = computed(
|
||||
() => (attrs.id as string | undefined) ?? popover.triggerId,
|
||||
);
|
||||
const disabled = computed(
|
||||
() => props.isDisabled || picker.disabled.value || picker.readOnly.value,
|
||||
);
|
||||
const defaultLabelledBy = computed(() => {
|
||||
if (attrs["aria-label"] || attrs["aria-labelledby"]) return undefined;
|
||||
const label = picker.labelledBy();
|
||||
return label ? [triggerId.value, label].join(" ") : undefined;
|
||||
});
|
||||
const defaultLabel = computed(() => {
|
||||
if (attrs["aria-label"] || attrs["aria-labelledby"]) return undefined;
|
||||
if (defaultLabelledBy.value) return undefined;
|
||||
return picker.locale.value.toLowerCase().startsWith("zh")
|
||||
? "日历"
|
||||
: "Calendar";
|
||||
});
|
||||
watch(
|
||||
[currentElement, triggerId],
|
||||
([element, id]) => {
|
||||
picker.trigger.value = element;
|
||||
picker.triggerId.value = id;
|
||||
root.triggerElement.value = element;
|
||||
root.triggerId = id;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<PopoverAnchor as-child>
|
||||
<OverlayAction
|
||||
:ref="forwardRef"
|
||||
v-bind="$attrs"
|
||||
:id="triggerId"
|
||||
:as="as"
|
||||
:as-child="asChild"
|
||||
:is-disabled="disabled"
|
||||
:activate="popover.state.toggle"
|
||||
:class="dateRangePickerVariants().trigger()"
|
||||
data-slot="date-range-picker-trigger"
|
||||
aria-haspopup="dialog"
|
||||
:aria-expanded="popover.state.isOpen.value"
|
||||
:aria-controls="
|
||||
popover.state.isOpen.value ? popover.contentId.value : undefined
|
||||
"
|
||||
:aria-label="($attrs['aria-label'] as string | undefined) ?? defaultLabel"
|
||||
:aria-labelledby="
|
||||
($attrs['aria-labelledby'] as string | undefined) ?? defaultLabelledBy
|
||||
"
|
||||
:aria-describedby="
|
||||
($attrs['aria-describedby'] as string | undefined) ??
|
||||
picker.describedBy()
|
||||
"
|
||||
:data-open="popover.state.isOpen.value || undefined"
|
||||
:data-disabled="disabled || undefined"
|
||||
:data-focus-visible="focusVisible || undefined"
|
||||
@focus="focusVisible = true"
|
||||
@blur="focusVisible = false"
|
||||
>
|
||||
<slot />
|
||||
</OverlayAction>
|
||||
</PopoverAnchor>
|
||||
</template>
|
||||
@@ -0,0 +1,33 @@
|
||||
<script setup lang="ts">
|
||||
import { dateRangePickerVariants } from "@heroui-vue/styles";
|
||||
defineOptions({ name: "DateRangePickerTriggerIndicator", inheritAttrs: false });
|
||||
withDefaults(defineProps<{ as?: string }>(), { as: "span" });
|
||||
</script>
|
||||
<template>
|
||||
<component
|
||||
:is="as"
|
||||
v-bind="$attrs"
|
||||
aria-hidden="true"
|
||||
:class="dateRangePickerVariants().triggerIndicator()"
|
||||
data-slot="date-range-picker-trigger-indicator"
|
||||
>
|
||||
<slot>
|
||||
<!-- Adapted from HeroUI ce90ea3 IconCalendar; Apache-2.0. -->
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
fill="none"
|
||||
height="1em"
|
||||
role="presentation"
|
||||
viewBox="0 0 13 14"
|
||||
width="1em"
|
||||
>
|
||||
<path
|
||||
clip-rule="evenodd"
|
||||
d="M3.75 4.5A.75.75 0 0 1 3 3.75v-.748a1.5 1.5 0 0 0-1.5 1.5v1h10v-1a1.5 1.5 0 0 0-1.5-1.5v.75a.75.75 0 1 1-1.5 0v-.75h-4v.747a.75.75 0 0 1-.75.75ZM8.5 1.501h-4V.75a.75.75 0 0 0-1.5 0v.752a3 3 0 0 0-3 3v6a3 3 0 0 0 3 3h7a3 3 0 0 0 3-3v-6a3 3 0 0 0-3-3v-.75a.75.75 0 0 0-1.5 0v.75Zm-7 5.5v3.5a1.5 1.5 0 0 0 1.5 1.5h7a1.5 1.5 0 0 0 1.5-1.5v-3.5h-10Z"
|
||||
fill="currentColor"
|
||||
fill-rule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</slot>
|
||||
</component>
|
||||
</template>
|
||||
@@ -0,0 +1,30 @@
|
||||
import { defineComponent, inject, watch } from "vue";
|
||||
import { dateRangeFieldKey } from "../date-field/context";
|
||||
|
||||
/** Reka keeps partially typed endpoint segments after an accepted empty range.
|
||||
* Clear both endpoint drafts on reset/external empty without replacing DOM,
|
||||
* focus or element ids. */
|
||||
export default defineComponent({
|
||||
name: "EmptyRangeDateBridge",
|
||||
props: { revision: { type: Number, required: true } },
|
||||
setup(props) {
|
||||
const field = inject(dateRangeFieldKey)!;
|
||||
watch(
|
||||
() => props.revision,
|
||||
() => {
|
||||
const segments = field.root.segmentValues;
|
||||
for (const endpoint of ["start", "end"] as const) {
|
||||
const values = segments[endpoint].value;
|
||||
segments[endpoint].value = Object.fromEntries(
|
||||
Object.keys(values).map((part) => [
|
||||
part,
|
||||
part === "dayPeriod" ? "AM" : null,
|
||||
]),
|
||||
) as typeof values;
|
||||
}
|
||||
},
|
||||
{ flush: "post" },
|
||||
);
|
||||
return () => null;
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,98 @@
|
||||
import {
|
||||
inject,
|
||||
type ComputedRef,
|
||||
type InjectionKey,
|
||||
type ShallowRef,
|
||||
} from "vue";
|
||||
import type {
|
||||
CalendarDate,
|
||||
CalendarDateTime,
|
||||
DateValue,
|
||||
Time,
|
||||
ZonedDateTime,
|
||||
} from "@internationalized/date";
|
||||
import type { OverlayState } from "../../composables/useOverlayState";
|
||||
import type { CalendarRangeModel } from "../date/range-selection";
|
||||
import type { CalendarWeekday } from "../date/calendar";
|
||||
|
||||
export type DateRangePickerTimeValue = Time | CalendarDateTime | ZonedDateTime;
|
||||
|
||||
/** Both endpoints stay independently editable; a missing endpoint is an
|
||||
* uncommitted time draft while the accepted range is empty. */
|
||||
export interface DateRangePickerTimeRange {
|
||||
start: DateRangePickerTimeValue | null;
|
||||
end: DateRangePickerTimeValue | null;
|
||||
}
|
||||
|
||||
export interface DateRangePickerState {
|
||||
value: CalendarRangeModel;
|
||||
timeRange: DateRangePickerTimeRange | null;
|
||||
setValue: (value: CalendarRangeModel) => void;
|
||||
setTimeRange: (value: DateRangePickerTimeRange | null) => void;
|
||||
}
|
||||
|
||||
export interface DateRangePickerRenderProps {
|
||||
value: CalendarRangeModel;
|
||||
isOpen: boolean;
|
||||
isDisabled: boolean;
|
||||
isReadOnly: boolean;
|
||||
isRequired: boolean;
|
||||
isInvalid: boolean;
|
||||
isFocusWithin: boolean;
|
||||
isFocused: boolean;
|
||||
isFocusVisible: boolean;
|
||||
validationErrors: string[];
|
||||
timeRange: DateRangePickerTimeRange | null;
|
||||
state: DateRangePickerState;
|
||||
open: () => void;
|
||||
close: () => void;
|
||||
toggle: () => void;
|
||||
}
|
||||
|
||||
export interface DateRangePickerEscapeOptions {
|
||||
disabled: boolean;
|
||||
handlers: ((event: Event) => void)[];
|
||||
}
|
||||
|
||||
/** One accepted range owner shared by the two field endpoints, an unbound
|
||||
* RangeCalendar and the native submission layer. */
|
||||
export interface DateRangePickerContext {
|
||||
state: OverlayState;
|
||||
trigger: ShallowRef<HTMLElement | undefined>;
|
||||
triggerId: ShallowRef<string | undefined>;
|
||||
value: ComputedRef<CalendarRangeModel>;
|
||||
dates: ComputedRef<DateValue[]>;
|
||||
timeRange: ComputedRef<DateRangePickerTimeRange | null>;
|
||||
setTimeRange: (value: DateRangePickerTimeRange | null) => void;
|
||||
requestRange: (value: CalendarRangeModel) => void;
|
||||
minValue: ComputedRef<DateValue | undefined>;
|
||||
maxValue: ComputedRef<DateValue | undefined>;
|
||||
isDateUnavailable: (
|
||||
value: DateValue,
|
||||
anchorDate?: CalendarDate | null,
|
||||
) => boolean;
|
||||
allowsNonContiguousRanges: () => boolean;
|
||||
disabled: ComputedRef<boolean>;
|
||||
readOnly: ComputedRef<boolean>;
|
||||
invalid: ComputedRef<boolean>;
|
||||
locale: ComputedRef<string>;
|
||||
dir: ComputedRef<"ltr" | "rtl">;
|
||||
labelledBy: () => string | undefined;
|
||||
describedBy: () => string | undefined;
|
||||
firstDayOfWeek: () => CalendarWeekday | undefined;
|
||||
pageBehavior: () => "visible" | "single" | undefined;
|
||||
placeholder: ComputedRef<DateValue>;
|
||||
calendarFallback: () => CalendarRangeModel;
|
||||
escapeOptions: ShallowRef<DateRangePickerEscapeOptions>;
|
||||
shouldCloseOnSelect: () => boolean;
|
||||
}
|
||||
|
||||
export const dateRangePickerKey: InjectionKey<DateRangePickerContext> = Symbol(
|
||||
"hv-date-range-picker",
|
||||
);
|
||||
|
||||
export function useDateRangePicker() {
|
||||
const value = inject(dateRangePickerKey, undefined);
|
||||
if (!value) throw new Error("DateRangePicker parts require DateRangePicker.");
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
import {
|
||||
computed,
|
||||
getCurrentInstance,
|
||||
h,
|
||||
nextTick,
|
||||
provide,
|
||||
ref,
|
||||
shallowRef,
|
||||
watch,
|
||||
watchEffect,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import { DateRangeFieldRoot } from "reka-ui";
|
||||
import {
|
||||
getLocalTimeZone,
|
||||
Time,
|
||||
toCalendarDate,
|
||||
toCalendarDateTime,
|
||||
today,
|
||||
type DateValue,
|
||||
} from "@internationalized/date";
|
||||
import { useConfig } from "../../providers/context";
|
||||
import {
|
||||
useChoiceField,
|
||||
useChoiceForm,
|
||||
validationInput,
|
||||
type ChoiceFieldProps,
|
||||
} from "../choice/shared";
|
||||
import type { ValidateResult } from "../field/context";
|
||||
import { dateFieldKey } from "../date-field/context";
|
||||
import { disabledContextKey } from "../field/disabled";
|
||||
import { useAcceptedCalendarRange } from "../date/model";
|
||||
import {
|
||||
copyCalendarRange,
|
||||
type CalendarRangeModel,
|
||||
} from "../date/range-selection";
|
||||
import {
|
||||
copyDateValue,
|
||||
dateForLocale,
|
||||
serializeDateValue,
|
||||
} from "../date/value";
|
||||
import PrimitiveNode from "../date-field/PrimitiveNode";
|
||||
import DateRangeFieldBody from "./DateRangeFieldBody.vue";
|
||||
import EmptyRangeDateBridge from "./EmptyRangeDateBridge";
|
||||
import type {
|
||||
DateRangePickerTimeRange,
|
||||
DateRangePickerTimeValue,
|
||||
} from "./context";
|
||||
|
||||
export interface DateRangePickerOwnerProps extends ChoiceFieldProps {
|
||||
modelValue?: CalendarRangeModel;
|
||||
value?: CalendarRangeModel;
|
||||
defaultValue?: CalendarRangeModel;
|
||||
placeholderValue?: DateValue | null;
|
||||
minValue?: DateValue | null;
|
||||
maxValue?: DateValue | null;
|
||||
isDateUnavailable?: (value: DateValue) => boolean;
|
||||
granularity?: "day" | "hour" | "minute" | "second";
|
||||
hourCycle?: 12 | 24;
|
||||
hideTimeZone?: boolean;
|
||||
shouldForceLeadingZeros?: boolean;
|
||||
allowsNonContiguousRanges?: boolean;
|
||||
locale?: string;
|
||||
dir?: "ltr" | "rtl";
|
||||
autoComplete?: string;
|
||||
startName?: string;
|
||||
endName?: string;
|
||||
validate?: (value: CalendarRangeModel) => ValidateResult;
|
||||
}
|
||||
|
||||
export interface DateRangePickerOwnerRoot {
|
||||
as: string;
|
||||
consumer?: Record<string, unknown>;
|
||||
overrides: Record<string, unknown>;
|
||||
}
|
||||
|
||||
const EMPTY_FIELD_RANGE: { start?: DateValue; end?: DateValue } = {
|
||||
start: undefined,
|
||||
end: undefined,
|
||||
};
|
||||
|
||||
function timeOf(
|
||||
value: DateValue | null | undefined,
|
||||
): DateRangePickerTimeValue | null {
|
||||
if (!value || !("hour" in value)) return null;
|
||||
return new Time(value.hour, value.minute, value.second, value.millisecond);
|
||||
}
|
||||
|
||||
function applyTime(
|
||||
value: DateValue,
|
||||
time: DateRangePickerTimeValue | null | undefined,
|
||||
): DateValue {
|
||||
if (!time) return value;
|
||||
const base: DateValue = "hour" in value ? value : toCalendarDateTime(value);
|
||||
return copyDateValue(
|
||||
base.set({
|
||||
hour: time.hour,
|
||||
minute: time.minute,
|
||||
second: time.second,
|
||||
millisecond: time.millisecond,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function copyTime(value: DateRangePickerTimeValue): DateRangePickerTimeValue {
|
||||
return new Time(value.hour, value.minute, value.second, value.millisecond);
|
||||
}
|
||||
|
||||
/** One accepted range, one field/validation/native submission layer and one
|
||||
* partial-endpoint draft. The accepted model never holds half a range. */
|
||||
export function useDateRangePickerOwner(
|
||||
props: DateRangePickerOwnerProps,
|
||||
attrs: Record<string, unknown>,
|
||||
options: {
|
||||
onChange: (value: CalendarRangeModel) => void;
|
||||
onReset?: () => void;
|
||||
onSelectComplete?: () => void;
|
||||
shouldCloseOnSelect?: () => boolean;
|
||||
},
|
||||
) {
|
||||
const config = useConfig();
|
||||
const instance = getCurrentInstance()!;
|
||||
const accepted = useAcceptedCalendarRange({
|
||||
value: () =>
|
||||
["modelValue", "model-value"].some((key) =>
|
||||
Object.hasOwn(instance.vnode.props ?? {}, key),
|
||||
)
|
||||
? props.modelValue
|
||||
: props.value,
|
||||
defaultValue: () => props.defaultValue,
|
||||
onChange: (value) => {
|
||||
field.edit();
|
||||
options.onChange(value);
|
||||
},
|
||||
disabled: () => field.field.disabled.value,
|
||||
readOnly: () => field.field.readOnly.value,
|
||||
});
|
||||
const field = useChoiceField<CalendarRangeModel>(props, attrs, {
|
||||
kind: "group",
|
||||
value: accepted.value,
|
||||
satisfied: () => accepted.value.value !== null,
|
||||
requiredMessage: "请填写此字段。",
|
||||
validate: (value) => {
|
||||
if (value) {
|
||||
if (props.minValue) {
|
||||
if (value.start.compare(props.minValue) < 0)
|
||||
return "开始日期不能早于最早日期。";
|
||||
if (value.end.compare(props.minValue) < 0)
|
||||
return "结束日期不能早于最早日期。";
|
||||
}
|
||||
if (props.maxValue) {
|
||||
if (value.start.compare(props.maxValue) > 0)
|
||||
return "开始日期不能晚于最晚日期。";
|
||||
if (value.end.compare(props.maxValue) > 0)
|
||||
return "结束日期不能晚于最晚日期。";
|
||||
}
|
||||
if (value.end.compare(value.start) < 0)
|
||||
return "结束日期不能早于开始日期。";
|
||||
if (props.isDateUnavailable?.(value.start)) return "此日期不可用。";
|
||||
if (props.isDateUnavailable?.(value.end)) return "此日期不可用。";
|
||||
if (!props.allowsNonContiguousRanges && props.isDateUnavailable) {
|
||||
// An interior unavailable day invalidates a contiguous range, while
|
||||
// the calendar still keeps the explicit non-contiguous policy.
|
||||
let day = toCalendarDate(value.start).add({ days: 1 });
|
||||
const end = toCalendarDate(value.end);
|
||||
while (day.compare(end) <= 0) {
|
||||
if (props.isDateUnavailable(day.copy())) return "此日期不可用。";
|
||||
const next = day.add({ days: 1 });
|
||||
if (next.compare(day) === 0) break;
|
||||
day = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return props.validate?.(value);
|
||||
},
|
||||
});
|
||||
provide(disabledContextKey, field.field.disabled);
|
||||
provide(dateFieldKey, {
|
||||
value: computed(() => accepted.value.value?.start ?? null),
|
||||
disabled: field.field.disabled,
|
||||
readOnly: field.field.readOnly,
|
||||
required: field.field.required,
|
||||
invalid: field.field.invalid,
|
||||
leadingZeros: () => props.shouldForceLeadingZeros,
|
||||
labelledBy: field.labelledBy,
|
||||
describedBy: field.describedBy,
|
||||
ariaLabel: () => attrs["aria-label"],
|
||||
});
|
||||
const locale = computed(
|
||||
() => props.locale ?? config?.locale.value ?? "en-US",
|
||||
);
|
||||
const dir = computed<"ltr" | "rtl">(
|
||||
() => props.dir ?? config?.dir?.value ?? "ltr",
|
||||
);
|
||||
const initialPlaceholder = (
|
||||
props.placeholderValue ??
|
||||
props.defaultValue?.start ??
|
||||
accepted.value.value?.start ??
|
||||
today(getLocalTimeZone())
|
||||
).copy();
|
||||
const lastPlaceholder = shallowRef(initialPlaceholder);
|
||||
const placeholder = computed(() => {
|
||||
let value = dateForLocale(
|
||||
props.placeholderValue ??
|
||||
accepted.value.value?.start ??
|
||||
lastPlaceholder.value,
|
||||
locale.value,
|
||||
);
|
||||
if (props.granularity && props.granularity !== "day" && !("hour" in value))
|
||||
value = toCalendarDateTime(value);
|
||||
return value;
|
||||
});
|
||||
const fieldRange = computed<{ start?: DateValue; end?: DateValue }>(() => {
|
||||
const value = accepted.wire.value;
|
||||
return value ? { start: value.start, end: value.end } : EMPTY_FIELD_RANGE;
|
||||
});
|
||||
const fieldRevision = ref(0);
|
||||
let incompleteDraft = false;
|
||||
const pendingTime = shallowRef<DateRangePickerTimeRange | null>(null);
|
||||
const timeRange = computed<DateRangePickerTimeRange | null>(() => {
|
||||
const value = accepted.value.value;
|
||||
if (value) return { start: timeOf(value.start), end: timeOf(value.end) };
|
||||
const pending = pendingTime.value;
|
||||
return pending
|
||||
? {
|
||||
start: pending.start ? copyTime(pending.start) : null,
|
||||
end: pending.end ? copyTime(pending.end) : null,
|
||||
}
|
||||
: null;
|
||||
});
|
||||
function setTimeRange(value: DateRangePickerTimeRange | null) {
|
||||
if (!value) {
|
||||
pendingTime.value = null;
|
||||
return;
|
||||
}
|
||||
const current = accepted.value.value;
|
||||
if (!current) {
|
||||
pendingTime.value = {
|
||||
start: value.start ? copyTime(value.start) : null,
|
||||
end: value.end ? copyTime(value.end) : null,
|
||||
};
|
||||
return;
|
||||
}
|
||||
const next = {
|
||||
start: applyTime(current.start, value.start),
|
||||
end: applyTime(current.end, value.end),
|
||||
};
|
||||
accepted.request(copyCalendarRange(next));
|
||||
}
|
||||
function calendarFallback(): CalendarRangeModel {
|
||||
const current = accepted.value.value;
|
||||
if (current) return current;
|
||||
const pending = pendingTime.value;
|
||||
if (!pending) return null;
|
||||
const base = placeholder.value;
|
||||
return {
|
||||
start: applyTime(base, pending.start),
|
||||
end: applyTime(base, pending.end),
|
||||
};
|
||||
}
|
||||
function requestRange(value: CalendarRangeModel) {
|
||||
if (!value) return;
|
||||
accepted.request(copyCalendarRange(value));
|
||||
if (options.shouldCloseOnSelect?.()) options.onSelectComplete?.();
|
||||
}
|
||||
watch(accepted.value, (value, previous) => {
|
||||
if (value) {
|
||||
lastPlaceholder.value = value.start.copy();
|
||||
pendingTime.value = null;
|
||||
} else if (previous) {
|
||||
pendingTime.value = null;
|
||||
incompleteDraft = false;
|
||||
fieldRevision.value++;
|
||||
}
|
||||
});
|
||||
/** Segment editing only exposes fields up to the visible granularity. Reka
|
||||
* builds the edited endpoint from one shared placeholder, so restore the
|
||||
* precision that the endpoint itself owned and could not display. */
|
||||
function preservePrecision(next: DateValue, previous?: DateValue | null) {
|
||||
if (!previous || !("hour" in previous) || !("hour" in next)) return next;
|
||||
const granularity = props.granularity ?? "minute";
|
||||
const patch: Record<string, number> = {
|
||||
millisecond: previous.millisecond,
|
||||
};
|
||||
if (granularity === "day") {
|
||||
patch.hour = previous.hour;
|
||||
patch.minute = previous.minute;
|
||||
patch.second = previous.second;
|
||||
} else if (granularity === "hour") {
|
||||
patch.minute = previous.minute;
|
||||
patch.second = previous.second;
|
||||
} else if (granularity !== "second") {
|
||||
patch.second = previous.second;
|
||||
}
|
||||
return copyDateValue(next.set(patch));
|
||||
}
|
||||
function onFieldRange(next: { start?: DateValue; end?: DateValue }) {
|
||||
if (field.field.disabled.value || field.field.readOnly.value) return;
|
||||
const previous = accepted.value.value;
|
||||
const start = next.start
|
||||
? preservePrecision(next.start, previous?.start)
|
||||
: null,
|
||||
end = next.end ? preservePrecision(next.end, previous?.end) : null;
|
||||
if (!start && !end) {
|
||||
incompleteDraft = false;
|
||||
accepted.request(null);
|
||||
return;
|
||||
}
|
||||
if (!start || !end) {
|
||||
incompleteDraft = true;
|
||||
return;
|
||||
}
|
||||
incompleteDraft = false;
|
||||
accepted.request(copyCalendarRange({ start, end }));
|
||||
}
|
||||
/** A half-edited endpoint must not survive closing or an external reset. */
|
||||
function settleField() {
|
||||
if (!incompleteDraft) return;
|
||||
incompleteDraft = false;
|
||||
accepted.restore();
|
||||
if (!accepted.value.value) fieldRevision.value++;
|
||||
}
|
||||
const proxy = ref<HTMLInputElement>();
|
||||
const nativeStart = ref<HTMLInputElement>();
|
||||
const nativeEnd = ref<HTMLInputElement>();
|
||||
const root = ref<HTMLElement>();
|
||||
const focused = ref(false);
|
||||
const focusVisible = ref(false);
|
||||
const focus = () =>
|
||||
root.value
|
||||
?.querySelector<HTMLElement>(
|
||||
'[data-slot="date-input-group-segment"][tabindex="0"]',
|
||||
)
|
||||
?.focus();
|
||||
useChoiceForm(
|
||||
proxy,
|
||||
() => {
|
||||
accepted.reset();
|
||||
field.reset();
|
||||
pendingTime.value = null;
|
||||
incompleteDraft = false;
|
||||
options.onReset?.();
|
||||
},
|
||||
() => {},
|
||||
);
|
||||
watchEffect(() => field.validity(proxy.value), { flush: "post" });
|
||||
const signature = computed(
|
||||
() =>
|
||||
`${props.granularity ?? "auto"}|${props.hourCycle ?? "auto"}|${placeholder.value.calendar.identifier}|${"hour" in placeholder.value}`,
|
||||
);
|
||||
watch(signature, () => {
|
||||
const ownerDocument = root.value?.ownerDocument;
|
||||
const old = ownerDocument?.activeElement as HTMLElement | null;
|
||||
const endpoint =
|
||||
old && root.value?.contains(old)
|
||||
? (old.closest("[data-range-endpoint]") as HTMLElement | null)?.dataset
|
||||
.rangeEndpoint
|
||||
: undefined;
|
||||
const part =
|
||||
old && root.value?.contains(old)
|
||||
? old.dataset.rekaDateFieldSegment
|
||||
: undefined;
|
||||
if (endpoint && part)
|
||||
void nextTick(() => {
|
||||
const current = ownerDocument?.activeElement;
|
||||
if (current === old || current === ownerDocument?.body)
|
||||
root.value
|
||||
?.querySelector<HTMLElement>(
|
||||
`[data-range-endpoint="${endpoint}"] [data-reka-date-field-segment="${part}"]`,
|
||||
)
|
||||
?.focus();
|
||||
});
|
||||
});
|
||||
function state() {
|
||||
return {
|
||||
value: accepted.value.value,
|
||||
isDisabled: field.field.disabled.value,
|
||||
isReadOnly: field.field.readOnly.value,
|
||||
isRequired: field.field.required.value,
|
||||
isInvalid: field.field.invalid.value,
|
||||
isFocusWithin: focused.value,
|
||||
isFocused: focused.value,
|
||||
isFocusVisible: focusVisible.value,
|
||||
validationErrors: field.field.errors.value,
|
||||
};
|
||||
}
|
||||
function valueInput(
|
||||
part: "start" | "end",
|
||||
name: string | undefined,
|
||||
slot: string,
|
||||
refTarget: typeof nativeStart,
|
||||
) {
|
||||
return h("input", {
|
||||
ref: refTarget,
|
||||
type: "text",
|
||||
class: "hv-choice-input",
|
||||
tabindex: -1,
|
||||
"aria-hidden": "true",
|
||||
autoComplete: props.autoComplete ?? "off",
|
||||
readonly: field.field.readOnly.value,
|
||||
name,
|
||||
form: props.form,
|
||||
value: serializeDateValue(
|
||||
part === "start"
|
||||
? (accepted.value.value?.start ?? null)
|
||||
: (accepted.value.value?.end ?? null),
|
||||
),
|
||||
disabled: field.field.disabled.value,
|
||||
"data-slot": `${slot}-${part}-value`,
|
||||
"data-part": part,
|
||||
});
|
||||
}
|
||||
function inputNodes(slot = "date-range-picker") {
|
||||
return [
|
||||
valueInput("start", props.startName, slot, nativeStart),
|
||||
valueInput("end", props.endName, slot, nativeEnd),
|
||||
validationInput(
|
||||
proxy,
|
||||
props,
|
||||
field.field.disabled.value || field.field.readOnly.value,
|
||||
field.behavior.value,
|
||||
(event) => field.invalidEvent(event, focus),
|
||||
),
|
||||
];
|
||||
}
|
||||
function renderOwner(
|
||||
children: VNodeChild[],
|
||||
rootConfig: DateRangePickerOwnerRoot,
|
||||
) {
|
||||
return h(
|
||||
DateRangeFieldRoot,
|
||||
{
|
||||
key: signature.value,
|
||||
asChild: true,
|
||||
modelValue: fieldRange.value as never,
|
||||
placeholder: placeholder.value,
|
||||
granularity: props.granularity,
|
||||
hourCycle: props.hourCycle,
|
||||
hideTimeZone: props.hideTimeZone,
|
||||
locale: locale.value,
|
||||
dir: props.dir ?? config?.dir?.value,
|
||||
minValue: props.minValue ?? undefined,
|
||||
maxValue: props.maxValue ?? undefined,
|
||||
isDateUnavailable: (date: DateValue) =>
|
||||
!!props.isDateUnavailable?.(date),
|
||||
disabled: field.field.disabled.value,
|
||||
readonly: field.field.readOnly.value,
|
||||
// Submission stays in this owner: the base field must not add a third
|
||||
// combined name/value next to the two public endpoint names.
|
||||
"onUpdate:modelValue": onFieldRange,
|
||||
},
|
||||
{
|
||||
default: () =>
|
||||
h(DateRangeFieldBody, null, {
|
||||
default: () =>
|
||||
h(
|
||||
PrimitiveNode,
|
||||
{
|
||||
as: rootConfig.as,
|
||||
consumer: rootConfig.consumer,
|
||||
overrides: rootConfig.overrides,
|
||||
},
|
||||
{
|
||||
default: () => [
|
||||
h(EmptyRangeDateBridge, {
|
||||
revision:
|
||||
accepted.emptyRevision.value + fieldRevision.value,
|
||||
}),
|
||||
...children,
|
||||
],
|
||||
},
|
||||
),
|
||||
}),
|
||||
},
|
||||
);
|
||||
}
|
||||
return {
|
||||
accepted,
|
||||
field,
|
||||
locale,
|
||||
dir,
|
||||
placeholder,
|
||||
root,
|
||||
nativeStart,
|
||||
proxy,
|
||||
focused,
|
||||
focusVisible,
|
||||
focus,
|
||||
timeRange,
|
||||
setTimeRange,
|
||||
calendarFallback,
|
||||
requestRange,
|
||||
onFieldRange,
|
||||
settleField,
|
||||
signature,
|
||||
state,
|
||||
inputNodes,
|
||||
renderOwner,
|
||||
};
|
||||
}
|
||||
@@ -2,6 +2,11 @@ import { computed, nextTick, ref } from "vue";
|
||||
import type { DateValue, Time } from "@internationalized/date";
|
||||
import { useChoiceValue } from "../choice/shared";
|
||||
import { copyDateValue, sameDateValue } from "./value";
|
||||
import {
|
||||
copyCalendarRange,
|
||||
sameCalendarRange,
|
||||
type CalendarRangeModel,
|
||||
} from "./range-selection";
|
||||
|
||||
/** One accepted model, with a copied wire value to resynchronize editable
|
||||
* Reka segments after a completed proposal. Partial drafts stay in the base. */
|
||||
@@ -60,3 +65,59 @@ export function useAcceptedDateValue<
|
||||
}
|
||||
return { value, wire, emptyRevision, request, reset };
|
||||
}
|
||||
|
||||
/** One accepted range, with copied wire endpoints so an endpoint editor can
|
||||
* resynchronize after a completed or rejected proposal. Partial endpoint drafts
|
||||
* live in that editor, never in this accepted model. */
|
||||
export function useAcceptedCalendarRange(options: {
|
||||
value: () => CalendarRangeModel | undefined;
|
||||
defaultValue: () => CalendarRangeModel | undefined;
|
||||
onChange: (value: CalendarRangeModel) => void;
|
||||
disabled?: () => boolean;
|
||||
readOnly?: () => boolean;
|
||||
}) {
|
||||
const model = useChoiceValue<CalendarRangeModel>(
|
||||
() => copyCalendarRange(options.value()),
|
||||
() => copyCalendarRange(options.defaultValue()),
|
||||
["modelValue", "model-value", "value"],
|
||||
(value) => options.onChange(copyCalendarRange(value)),
|
||||
);
|
||||
const revision = ref(0);
|
||||
const emptyRevision = ref(0);
|
||||
let requestId = 0;
|
||||
const value = computed(() => {
|
||||
const copy = copyCalendarRange(model.value.value);
|
||||
return copy ? Object.freeze(copy) : null;
|
||||
});
|
||||
const wire = computed(() => {
|
||||
void revision.value;
|
||||
return copyCalendarRange(value.value);
|
||||
});
|
||||
function restore() {
|
||||
revision.value++;
|
||||
void nextTick(() => {
|
||||
revision.value++;
|
||||
});
|
||||
}
|
||||
function request(next: CalendarRangeModel) {
|
||||
if (
|
||||
!options.disabled?.() &&
|
||||
!options.readOnly?.() &&
|
||||
!sameCalendarRange(value.value, next)
|
||||
)
|
||||
model.request(copyCalendarRange(next));
|
||||
restore();
|
||||
const id = ++requestId;
|
||||
if (next)
|
||||
void nextTick(() => {
|
||||
if (id === requestId && value.value === null) emptyRevision.value++;
|
||||
});
|
||||
}
|
||||
function reset() {
|
||||
requestId++;
|
||||
model.reset();
|
||||
restore();
|
||||
if (value.value === null) emptyRevision.value++;
|
||||
}
|
||||
return { value, wire, emptyRevision, request, reset, restore };
|
||||
}
|
||||
|
||||
@@ -21,6 +21,15 @@ export const copyCalendarRange = (
|
||||
: null;
|
||||
export const calendarRangeKey = (value: CalendarRangeModel) =>
|
||||
value ? `${dateValueKey(value.start)}|${dateValueKey(value.end)}` : "empty";
|
||||
export const sameCalendarRange = (
|
||||
a: CalendarRangeModel | undefined,
|
||||
b: CalendarRangeModel | undefined,
|
||||
) =>
|
||||
(!a && !b) ||
|
||||
(!!a &&
|
||||
!!b &&
|
||||
dateValueKey(a.start) === dateValueKey(b.start) &&
|
||||
dateValueKey(a.end) === dateValueKey(b.end));
|
||||
export function orderedCalendarRange(
|
||||
a: DateValue,
|
||||
b: DateValue,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<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 { inject, nextTick, onMounted, useAttrs, watch } from "vue";
|
||||
import { toCalendar, type DateValue } from "@internationalized/date";
|
||||
import {
|
||||
useRangeCalendar,
|
||||
type RangeCalendarOptions,
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
type CalendarRangeModel,
|
||||
} from "../date/range-selection";
|
||||
import { calendarHeading } from "../date/calendar";
|
||||
import { dateRangePickerKey } from "../date-range-picker/context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = defineProps<RangeCalendarOptions>();
|
||||
const emit = defineEmits<{
|
||||
@@ -35,7 +36,31 @@ defineSlots<{
|
||||
}) => unknown;
|
||||
}>();
|
||||
const attrs = useAttrs();
|
||||
const view = useRangeCalendar(props, emit);
|
||||
const picker = inject(dateRangePickerKey, undefined);
|
||||
const view = useRangeCalendar(
|
||||
props,
|
||||
emit,
|
||||
picker
|
||||
? {
|
||||
value: picker.value,
|
||||
request: picker.requestRange,
|
||||
fallback: picker.calendarFallback,
|
||||
minValue: () => picker.minValue.value,
|
||||
maxValue: () => picker.maxValue.value,
|
||||
isDateUnavailable: picker.isDateUnavailable,
|
||||
allowsNonContiguousRanges: picker.allowsNonContiguousRanges,
|
||||
locale: () => picker.locale.value,
|
||||
dir: () => picker.dir.value,
|
||||
firstDayOfWeek: () => picker.firstDayOfWeek(),
|
||||
pageBehavior: () => picker.pageBehavior(),
|
||||
isDisabled: () => picker.disabled.value,
|
||||
isReadOnly: () => picker.readOnly.value,
|
||||
isInvalid: () => picker.invalid.value,
|
||||
defaultFocusedValue: () =>
|
||||
picker.value.value?.start ?? picker.placeholder.value,
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
const {
|
||||
root,
|
||||
focus,
|
||||
@@ -53,6 +78,20 @@ const {
|
||||
anchorDate,
|
||||
highlightedRange,
|
||||
} = view;
|
||||
function focusForPicker(open: boolean) {
|
||||
if (!picker || !open || !view.inherits.value) return;
|
||||
void nextTick(() => {
|
||||
if (!picker.state.isOpen.value) return;
|
||||
const target = picker.value.value?.start ?? picker.placeholder.value;
|
||||
focusDate(toCalendar(target, focus.value.calendar), true);
|
||||
});
|
||||
}
|
||||
onMounted(() => focusForPicker(!!picker?.state.isOpen.value));
|
||||
watch(
|
||||
() => picker?.state.isOpen.value,
|
||||
(open) => focusForPicker(!!open),
|
||||
{ flush: "post" },
|
||||
);
|
||||
defineExpose({
|
||||
get element() {
|
||||
return root.value;
|
||||
|
||||
@@ -41,6 +41,28 @@ export interface RangeCalendarOptions extends Omit<
|
||||
anchorDate: CalendarDate | null,
|
||||
) => boolean;
|
||||
}
|
||||
/** Constraints a DateRangePicker owner passes to an otherwise unbound calendar.
|
||||
* An explicitly bound RangeCalendar keeps its own accepted value and rules. */
|
||||
export interface RangeCalendarInherited {
|
||||
value: ComputedRef<CalendarRangeModel>;
|
||||
request: (value: CalendarRangeModel) => void;
|
||||
fallback?: () => CalendarRangeModel;
|
||||
minValue?: () => DateValue | undefined;
|
||||
maxValue?: () => DateValue | undefined;
|
||||
isDateUnavailable?: (
|
||||
date: DateValue,
|
||||
anchorDate: CalendarDate | null,
|
||||
) => boolean;
|
||||
allowsNonContiguousRanges?: () => boolean;
|
||||
locale?: () => string;
|
||||
dir?: () => "ltr" | "rtl";
|
||||
firstDayOfWeek?: () => import("../date/calendar").CalendarWeekday | undefined;
|
||||
pageBehavior?: () => "visible" | "single" | undefined;
|
||||
isDisabled?: () => boolean;
|
||||
isReadOnly?: () => boolean;
|
||||
isInvalid?: () => boolean;
|
||||
defaultFocusedValue?: () => DateValue | undefined;
|
||||
}
|
||||
interface RangeEmit {
|
||||
(event: "update:modelValue", value: CalendarRangeModel): void;
|
||||
(event: "change", value: CalendarRangeModel): void;
|
||||
@@ -59,45 +81,79 @@ interface Gesture {
|
||||
}
|
||||
/** Range selection owns only the accepted value and an uncommitted anchor.
|
||||
* All calendar geometry, focus, paging and year navigation have one shared owner. */
|
||||
export function useRangeCalendar(props: RangeCalendarOptions, emit: RangeEmit) {
|
||||
export function useRangeCalendar(
|
||||
props: RangeCalendarOptions,
|
||||
emit: RangeEmit,
|
||||
inherited?: RangeCalendarInherited,
|
||||
) {
|
||||
const instance = getCurrentInstance()!;
|
||||
const has = (key: string) => Object.hasOwn(instance.vnode.props ?? {}, key);
|
||||
const fallback = copyCalendarRange(props.defaultValue);
|
||||
const model = useChoiceValue<CalendarRangeModel>(
|
||||
const inheritsValue = computed(
|
||||
() =>
|
||||
copyCalendarRange(
|
||||
has("modelValue") || has("model-value")
|
||||
? props.modelValue
|
||||
: props.value,
|
||||
),
|
||||
() => copyCalendarRange(fallback),
|
||||
["modelValue", "model-value", "value"],
|
||||
(value) => {
|
||||
emit("update:modelValue", copyCalendarRange(value));
|
||||
emit("change", copyCalendarRange(value));
|
||||
},
|
||||
!!inherited &&
|
||||
!has("modelValue") &&
|
||||
!has("model-value") &&
|
||||
!has("value") &&
|
||||
!has("defaultValue") &&
|
||||
!has("default-value"),
|
||||
);
|
||||
const fallback = (): CalendarRangeModel =>
|
||||
copyCalendarRange(
|
||||
inheritsValue.value ? inherited?.fallback?.() : props.defaultValue,
|
||||
);
|
||||
const model = inheritsValue.value
|
||||
? {
|
||||
value: computed(() => copyCalendarRange(inherited!.value.value)),
|
||||
request: (value: CalendarRangeModel) =>
|
||||
inherited!.request(copyCalendarRange(value)),
|
||||
reset: () => {},
|
||||
}
|
||||
: useChoiceValue<CalendarRangeModel>(
|
||||
() =>
|
||||
copyCalendarRange(
|
||||
has("modelValue") || has("model-value")
|
||||
? props.modelValue
|
||||
: props.value,
|
||||
),
|
||||
() => fallback(),
|
||||
["modelValue", "model-value", "value"],
|
||||
(value) => {
|
||||
emit("update:modelValue", copyCalendarRange(value));
|
||||
emit("change", copyCalendarRange(value));
|
||||
},
|
||||
);
|
||||
const anchorDate = shallowRef<CalendarDate | null>(null);
|
||||
const draftEnd = shallowRef<CalendarDate | null>(null);
|
||||
const dates = computed(() =>
|
||||
model.value.value ? [model.value.value.start, model.value.value.end] : [],
|
||||
);
|
||||
const unavailable = (date: DateValue) =>
|
||||
!!props.isDateUnavailable?.(date.copy(), anchorDate.value?.copy() ?? null);
|
||||
!!(props.isDateUnavailable ?? inherited?.isDateUnavailable)?.(
|
||||
date.copy(),
|
||||
anchorDate.value?.copy() ?? null,
|
||||
);
|
||||
const baseDisabled = (date: DateValue) =>
|
||||
calendarDayState(date, {
|
||||
isDateDisabled: props.isDateDisabled,
|
||||
minValue: props.minValue ?? view.min.value,
|
||||
maxValue: props.maxValue ?? view.max.value,
|
||||
minValue: props.minValue ?? inherited?.minValue?.() ?? view.min.value,
|
||||
maxValue: props.maxValue ?? inherited?.maxValue?.() ?? view.max.value,
|
||||
isDisabled: view.disabled.value,
|
||||
}).isDisabled;
|
||||
// Vue gives boolean props a false default, so an unset prop must not shadow
|
||||
// the inherited policy. Only an explicitly bound prop overrides it.
|
||||
const nonContiguous = () =>
|
||||
has("allowsNonContiguousRanges") || has("allows-non-contiguous-ranges")
|
||||
? !!props.allowsNonContiguousRanges
|
||||
: (inherited?.allowsNonContiguousRanges?.() ?? false);
|
||||
// Only an active draft constrains cross-barrier navigation. Cache by semantics,
|
||||
// not mutable DateValue identity; invalidated by reactive rules/bounds below.
|
||||
let barriers = new Map<string, ComputedRef<DateValue | undefined>>();
|
||||
function barrier(date: DateValue) {
|
||||
if (
|
||||
!anchorDate.value ||
|
||||
(!props.isDateUnavailable && !props.isDateDisabled)
|
||||
(!props.isDateUnavailable &&
|
||||
!props.isDateDisabled &&
|
||||
!inherited?.isDateUnavailable)
|
||||
)
|
||||
return undefined;
|
||||
const key = dateValueKey(date);
|
||||
@@ -111,7 +167,7 @@ export function useRangeCalendar(props: RangeCalendarOptions, emit: RangeEmit) {
|
||||
date,
|
||||
baseDisabled,
|
||||
unavailable,
|
||||
!!props.allowsNonContiguousRanges,
|
||||
nonContiguous(),
|
||||
)
|
||||
: undefined,
|
||||
),
|
||||
@@ -169,7 +225,7 @@ export function useRangeCalendar(props: RangeCalendarOptions, emit: RangeEmit) {
|
||||
return false;
|
||||
}
|
||||
const range = orderedCalendarRange(anchorDate.value, date);
|
||||
const value = convertCalendarRange(range, model.value.value ?? fallback);
|
||||
const value = convertCalendarRange(range, model.value.value ?? fallback());
|
||||
anchorDate.value = null;
|
||||
draftEnd.value = null;
|
||||
barriers.clear();
|
||||
@@ -398,6 +454,21 @@ export function useRangeCalendar(props: RangeCalendarOptions, emit: RangeEmit) {
|
||||
},
|
||||
emit,
|
||||
"range-calendar",
|
||||
inherited
|
||||
? {
|
||||
minValue: () => props.minValue ?? inherited.minValue?.(),
|
||||
maxValue: () => props.maxValue ?? inherited.maxValue?.(),
|
||||
defaultFocusedValue: () => inherited.defaultFocusedValue?.(),
|
||||
locale: () => props.locale ?? inherited.locale?.() ?? "en-US",
|
||||
dir: () => props.dir ?? inherited.dir?.() ?? "ltr",
|
||||
firstDayOfWeek: () =>
|
||||
props.firstDayOfWeek ?? inherited.firstDayOfWeek?.(),
|
||||
pageBehavior: () => props.pageBehavior ?? inherited.pageBehavior?.(),
|
||||
isDisabled: () => !!inherited.isDisabled?.(),
|
||||
isReadOnly: () => !!inherited.isReadOnly?.(),
|
||||
isInvalid: () => !!inherited.isInvalid?.(),
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
watch(
|
||||
() => dateValueKey(view.focus.value),
|
||||
@@ -420,6 +491,8 @@ export function useRangeCalendar(props: RangeCalendarOptions, emit: RangeEmit) {
|
||||
() => props.isDateUnavailable,
|
||||
() => props.isDateDisabled,
|
||||
() => props.allowsNonContiguousRanges,
|
||||
() => !!inherited?.isDateUnavailable,
|
||||
() => inherited?.allowsNonContiguousRanges?.(),
|
||||
() => dateValueKey(props.minValue),
|
||||
() => dateValueKey(props.maxValue),
|
||||
() => view.options.value.locale,
|
||||
@@ -449,5 +522,12 @@ export function useRangeCalendar(props: RangeCalendarOptions, emit: RangeEmit) {
|
||||
stopGesture();
|
||||
resetClick();
|
||||
});
|
||||
return { ...view, model, anchorDate, highlightedRange, cancel };
|
||||
return {
|
||||
...view,
|
||||
model,
|
||||
anchorDate,
|
||||
highlightedRange,
|
||||
cancel,
|
||||
inherits: inheritsValue,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -805,6 +805,33 @@ export type {
|
||||
DatePickerPopoverProps,
|
||||
} from "./types";
|
||||
|
||||
export {
|
||||
default as DateRangePicker,
|
||||
default as DateRangePickerRoot,
|
||||
} from "./components/date-range-picker/DateRangePicker.vue";
|
||||
export { default as DateRangePickerTrigger } from "./components/date-range-picker/DateRangePickerTrigger.vue";
|
||||
export { default as DateRangePickerTriggerIndicator } from "./components/date-range-picker/DateRangePickerTriggerIndicator.vue";
|
||||
export { default as DateRangePickerRangeSeparator } from "./components/date-range-picker/DateRangePickerRangeSeparator.vue";
|
||||
export { default as DateRangePickerPopover } from "./components/date-range-picker/DateRangePickerPopover.vue";
|
||||
export type {
|
||||
DateRangePickerRenderProps,
|
||||
DateRangePickerState,
|
||||
DateRangePickerTimeRange,
|
||||
DateRangePickerTimeValue,
|
||||
} from "./components/date-range-picker/context";
|
||||
export {
|
||||
dateRangePickerVariants,
|
||||
type DateRangePickerVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
export type {
|
||||
DateRangePickerProps,
|
||||
DateRangePickerRootProps,
|
||||
DateRangePickerTriggerProps,
|
||||
DateRangePickerTriggerIndicatorProps,
|
||||
DateRangePickerRangeSeparatorProps,
|
||||
DateRangePickerPopoverProps,
|
||||
} from "./types";
|
||||
|
||||
export {
|
||||
dateFieldVariants,
|
||||
dateInputGroupVariants,
|
||||
|
||||
@@ -662,6 +662,28 @@ export type DatePickerPopoverProps = InstanceType<
|
||||
typeof DatePickerPopover
|
||||
>["$props"];
|
||||
|
||||
import type DateRangePicker from "./components/date-range-picker/DateRangePicker.vue";
|
||||
import type DateRangePickerTrigger from "./components/date-range-picker/DateRangePickerTrigger.vue";
|
||||
import type DateRangePickerTriggerIndicator from "./components/date-range-picker/DateRangePickerTriggerIndicator.vue";
|
||||
import type DateRangePickerRangeSeparator from "./components/date-range-picker/DateRangePickerRangeSeparator.vue";
|
||||
import type DateRangePickerPopover from "./components/date-range-picker/DateRangePickerPopover.vue";
|
||||
export type DateRangePickerProps = InstanceType<
|
||||
typeof DateRangePicker
|
||||
>["$props"];
|
||||
export type DateRangePickerRootProps = DateRangePickerProps;
|
||||
export type DateRangePickerTriggerProps = InstanceType<
|
||||
typeof DateRangePickerTrigger
|
||||
>["$props"];
|
||||
export type DateRangePickerTriggerIndicatorProps = InstanceType<
|
||||
typeof DateRangePickerTriggerIndicator
|
||||
>["$props"];
|
||||
export type DateRangePickerRangeSeparatorProps = InstanceType<
|
||||
typeof DateRangePickerRangeSeparator
|
||||
>["$props"];
|
||||
export type DateRangePickerPopoverProps = InstanceType<
|
||||
typeof DateRangePickerPopover
|
||||
>["$props"];
|
||||
|
||||
import type ToastProvider from "./components/toast/ToastProvider.vue";
|
||||
import type Toast from "./components/toast/Toast.vue";
|
||||
import type ToastContent from "./components/toast/ToastContent.vue";
|
||||
|
||||
Reference in New Issue
Block a user