feat: migrate DateField and shared date input group

This commit is contained in:
maoyingjie
2026-10-23 23:56:18 +08:00
parent 3c878d2582
commit 01f0776805
63 changed files with 3974 additions and 90 deletions
@@ -0,0 +1,25 @@
import { defineComponent, watch } from "vue";
import { injectDateFieldRootContext } from "reka-ui";
/** Reka does not watch equal primitive null values after a rejected edit.
* Clear its existing segment refs without replacing DOM, focus or IDs. */
export default defineComponent({
name: "EmptyDateBridge",
props: { revision: { type: Number, required: true } },
setup(props) {
const base = injectDateFieldRootContext();
watch(
() => props.revision,
() => {
base.segmentValues.value = Object.fromEntries(
Object.keys(base.segmentValues.value).map((part) => [
part,
part === "dayPeriod" ? "AM" : null,
]),
) as typeof base.segmentValues.value;
},
{ flush: "post" },
);
return () => null;
},
});
+60
View File
@@ -0,0 +1,60 @@
import { computed, nextTick, ref } from "vue";
import type { DateValue } from "@internationalized/date";
import { useChoiceValue } from "../choice/shared";
import { copyDateValue, sameDateValue } from "./value";
/** One accepted model, with a copied wire value to resynchronize editable
* Reka segments after a completed proposal. Partial drafts stay in the base. */
export function useAcceptedDateValue(options: {
value: () => DateValue | null | undefined;
defaultValue: () => DateValue | null | undefined;
onChange: (value: DateValue | null) => void;
disabled?: () => boolean;
readOnly?: () => boolean;
}) {
const model = useChoiceValue<DateValue | null | undefined>(
() => copyDateValue(options.value()),
() => copyDateValue(options.defaultValue() ?? null),
["modelValue", "model-value", "value"],
(value) => options.onChange(copyDateValue(value ?? null)),
);
const revision = ref(0);
const emptyRevision = ref(0);
let requestId = 0;
const value = computed(() => {
const copy = copyDateValue(model.value.value ?? null);
return copy ? (Object.freeze(copy) as DateValue) : null;
});
const wire = computed(() => {
void revision.value;
return copyDateValue(value.value);
});
function restore() {
revision.value++;
// Parent props and the base's segment edit both settle before this copy.
void nextTick(() => {
revision.value++;
});
}
function request(next: DateValue | null | undefined) {
if (
!options.disabled?.() &&
!options.readOnly?.() &&
!sameDateValue(value.value, next)
)
model.request(copyDateValue(next ?? null));
restore();
const id = ++requestId;
if (next != null)
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 };
}
+112
View File
@@ -0,0 +1,112 @@
import {
DateFormatter,
createCalendar,
toCalendar,
toCalendarDate,
type CalendarIdentifier,
type DateValue,
} from "@internationalized/date";
/** Private date boundary: preserve calendar/time/zone, never round-trip JS Date. */
export function copyDateValue<T extends DateValue | null | undefined>(
value: T,
): T {
return (value == null ? value : value.copy()) as T;
}
export function dateValueKey(value: DateValue | null | undefined): string {
if (value == null) return "empty";
const time =
"hour" in value
? [value.hour, value.minute, value.second, value.millisecond]
: [];
const zone = "timeZone" in value ? [value.timeZone, value.offset] : [];
return JSON.stringify([
value.calendar.identifier,
value.era,
value.year,
value.month,
value.day,
...time,
...zone,
]);
}
export function sameDateValue(
a: DateValue | null | undefined,
b: DateValue | null | undefined,
): boolean {
return dateValueKey(a) === dateValueKey(b);
}
export function copyDateValues<T extends DateValue>(values: readonly T[]): T[] {
const seen = new Set<string>();
return values
.filter((value) => {
const key = dateValueKey(value);
if (seen.has(key)) return false;
seen.add(key);
return true;
})
.map(copyDateValue);
}
/** Full ISO boundary; display granularity must not truncate the accepted model. */
export function serializeDateValue(
value: DateValue | null | undefined,
): string {
return value?.toString() ?? "";
}
export interface CalendarDayRules {
minValue?: DateValue;
maxValue?: DateValue;
isDisabled?: boolean;
isDateDisabled?: (value: DateValue) => boolean;
isDateUnavailable?: (value: DateValue) => boolean;
}
export function calendarDayState(
value: DateValue,
rules: CalendarDayRules = {},
) {
const day = toCalendarDate(value);
const outOfRange = !!(
(rules.minValue && day.compare(toCalendarDate(rules.minValue)) < 0) ||
(rules.maxValue && day.compare(toCalendarDate(rules.maxValue)) > 0)
);
const isDisabled = !!(
rules.isDisabled ||
outOfRange ||
rules.isDateDisabled?.(value)
);
const isUnavailable = !!rules.isDateUnavailable?.(value);
return {
isDisabled,
isUnavailable,
isSelectable: !isDisabled && !isUnavailable,
};
}
export interface DateRangeValue<T extends DateValue = DateValue> {
start: T | null;
end: T | null;
}
export function copyDateRange<T extends DateValue>(
range: DateRangeValue<T>,
): DateRangeValue<T> {
return { start: copyDateValue(range.start), end: copyDateValue(range.end) };
}
/** Calendar ranges compare local calendar days; time-field bounds are separate. */
export function calendarRangeStatus(
range: DateRangeValue,
): "empty" | "incomplete" | "inverted" | "valid" {
if (!range.start && !range.end) return "empty";
if (!range.start || !range.end) return "incomplete";
return toCalendarDate(range.start).compare(toCalendarDate(range.end)) > 0
? "inverted"
: "valid";
}
/** View-calendar conversion is explicit and must not replace the public model. */
export function dateForLocale<T extends DateValue>(
value: T,
locale: string,
): T {
const identifier = new DateFormatter(locale).resolvedOptions()
.calendar as CalendarIdentifier;
return toCalendar(value, createCalendar(identifier)) as T;
}