42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import type { ComputedRef, InjectionKey, Ref } from "vue";
|
|
|
|
export type ValidationBehavior = "native" | "aria";
|
|
export type ValidationErrors = Record<string, string | string[]>;
|
|
export type ValidateResult = string | string[] | true | null | undefined;
|
|
export type FieldPart = "description" | "error" | "label";
|
|
export interface FieldContext {
|
|
controlId: ComputedRef<string>;
|
|
kind: "text" | "toggle" | "group";
|
|
disabled: ComputedRef<boolean>;
|
|
readOnly: ComputedRef<boolean>;
|
|
required: ComputedRef<boolean>;
|
|
invalid: ComputedRef<boolean>;
|
|
errors: ComputedRef<string[]>;
|
|
validationDetails?: ComputedRef<ValidityState | undefined>;
|
|
labelIds?: Ref<string[]>;
|
|
descriptionIds: Ref<string[]>;
|
|
errorIds: Ref<string[]>;
|
|
}
|
|
export const fieldContextKey: InjectionKey<FieldContext> =
|
|
Symbol("HeroUIVue.Field");
|
|
export interface FormContext {
|
|
validationBehavior: ComputedRef<ValidationBehavior>;
|
|
validationErrors: ComputedRef<ValidationErrors>;
|
|
}
|
|
export const formContextKey: InjectionKey<FormContext> =
|
|
Symbol("HeroUIVue.Form");
|
|
|
|
export function mergeIds(...values: (string | string[] | undefined)[]) {
|
|
const ids = values
|
|
.flatMap((value) =>
|
|
typeof value === "string" ? value.split(/\s+/) : (value ?? []),
|
|
)
|
|
.filter(Boolean);
|
|
return [...new Set(ids)].join(" ") || undefined;
|
|
}
|
|
export function errorMessages(result: ValidateResult | false): string[] {
|
|
return (
|
|
typeof result === "string" ? [result] : Array.isArray(result) ? result : []
|
|
).filter(Boolean);
|
|
}
|