Files
homarr/packages/form/src/index.ts
T
Thomas fcca57ddf7 feat(custom-widgets): improve authoring workbench (#6689)
* feat(custom-widgets): improve authoring workbench

* fix(custom-widgets): address review and CI

* refactor(custom-widgets): satisfy architecture checks

* fix(custom-widgets): isolate workbench typing updates

* fix(custom-widgets): harden workbench state
2026-08-24 08:55:00 +02:00

39 lines
1.2 KiB
TypeScript

import { schemaResolver, useForm } from "@mantine/form";
import type { ZodDiscriminatedUnion, ZodIntersection, ZodObject, ZodPipe } from "zod/v4";
import { z } from "zod/v4";
import { useI18n } from "@homarr/translation/client";
import { zodErrorMap } from "@homarr/validation/form/i18n";
type inferPossibleSchema<
TSchema extends
| ZodObject
| ZodPipe<ZodObject>
| ZodIntersection<ZodObject | ZodDiscriminatedUnion<ZodObject[]>, ZodObject>,
> = z.infer<TSchema> extends Record<string, unknown> ? z.infer<TSchema> : never;
export const useZodForm = <
TSchema extends
| ZodObject
| ZodPipe<ZodObject>
| ZodIntersection<ZodObject | ZodDiscriminatedUnion<ZodObject[]>, ZodObject>,
>(
schema: TSchema,
options: Omit<
Exclude<Parameters<typeof useForm<inferPossibleSchema<TSchema>>>[0], undefined>,
"validate" | "validateInputOnBlur"
>,
) => {
const t = useI18n();
z.config({
customError: zodErrorMap(t),
});
return useForm<inferPossibleSchema<TSchema>>({
...options,
validateInputOnBlur: true,
validateInputOnChange: options.validateInputOnChange ?? true,
validate: schemaResolver(schema, { sync: true }),
});
};