feat: establish shared field and form foundation
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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";
|
||||
export interface FieldContext {
|
||||
controlId: ComputedRef<string>;
|
||||
kind: "text" | "toggle";
|
||||
disabled: ComputedRef<boolean>;
|
||||
readOnly: ComputedRef<boolean>;
|
||||
required: ComputedRef<boolean>;
|
||||
invalid: ComputedRef<boolean>;
|
||||
errors: ComputedRef<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);
|
||||
}
|
||||
Reference in New Issue
Block a user