63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import {
|
|
inject,
|
|
type ComputedRef,
|
|
type InjectionKey,
|
|
type Component,
|
|
} from "vue";
|
|
import type { DateValue, Time } from "@internationalized/date";
|
|
import type { DateFieldInputProps } from "reka-ui";
|
|
import type { dateInputGroupVariants } from "@heroui-vue/styles";
|
|
export interface DateSegment {
|
|
part: DateFieldInputProps["part"];
|
|
type: DateFieldInputProps["part"];
|
|
value: string;
|
|
text: string;
|
|
isPlaceholder: boolean;
|
|
isEditable: boolean;
|
|
}
|
|
export interface DateFieldContext {
|
|
value: ComputedRef<DateValue | Time | null>;
|
|
disabled: ComputedRef<boolean>;
|
|
readOnly: ComputedRef<boolean>;
|
|
required: ComputedRef<boolean>;
|
|
invalid: ComputedRef<boolean>;
|
|
form?: () => HTMLFormElement | null | undefined;
|
|
leadingZeros: () => boolean | undefined;
|
|
labelledBy: () => string | undefined;
|
|
describedBy: () => string | undefined;
|
|
ariaLabel: () => unknown;
|
|
}
|
|
export const dateFieldKey: InjectionKey<DateFieldContext> =
|
|
Symbol("hv-date-field");
|
|
export const dateGroupKey: InjectionKey<
|
|
ComputedRef<ReturnType<typeof dateInputGroupVariants>>
|
|
> = Symbol("hv-date-group");
|
|
export function useDateField() {
|
|
const value = inject(dateFieldKey, undefined);
|
|
if (!value)
|
|
throw new Error("Date input parts require a DateField or TimeField root.");
|
|
return value;
|
|
}
|
|
|
|
export const dateSegmentsKey: InjectionKey<
|
|
ComputedRef<{ part: DateFieldInputProps["part"]; value: string }[]>
|
|
> = Symbol("hv-date-segments");
|
|
|
|
export const dateSegmentBaseKey: InjectionKey<{
|
|
base: NonNullable<
|
|
ReturnType<typeof import("reka-ui").injectDateFieldRootContext>
|
|
>;
|
|
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");
|