mirror of
https://github.com/ajnart/homarr.git
synced 2026-02-28 09:21:00 +01:00
34 lines
851 B
TypeScript
34 lines
851 B
TypeScript
import { useForm, zodResolver } from "@mantine/form";
|
|
|
|
import { useI18n } from "@homarr/translation/client";
|
|
import { z } from "@homarr/validation";
|
|
import type {
|
|
AnyZodObject,
|
|
ZodEffects,
|
|
ZodIntersection,
|
|
} from "@homarr/validation";
|
|
import { zodErrorMap } from "@homarr/validation/form";
|
|
|
|
export const useZodForm = <
|
|
TSchema extends
|
|
| AnyZodObject
|
|
| ZodEffects<AnyZodObject>
|
|
| ZodIntersection<AnyZodObject, AnyZodObject>,
|
|
>(
|
|
schema: TSchema,
|
|
options: Omit<
|
|
Exclude<Parameters<typeof useForm<z.infer<TSchema>>>[0], undefined>,
|
|
"validate" | "validateInputOnBlur" | "validateInputOnChange"
|
|
>,
|
|
) => {
|
|
const t = useI18n();
|
|
|
|
z.setErrorMap(zodErrorMap(t));
|
|
return useForm<z.infer<TSchema>>({
|
|
...options,
|
|
validateInputOnBlur: true,
|
|
validateInputOnChange: true,
|
|
validate: zodResolver(schema),
|
|
});
|
|
};
|