feat: establish shared field and form foundation

This commit is contained in:
maoyingjie
2026-10-21 22:33:19 +08:00
parent 4f43789f60
commit 7f11afeaa6
35 changed files with 1677 additions and 165 deletions
@@ -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);
}