Replace entire codebase with homarr-labs/homarr
This commit is contained in:
39
packages/form/src/index.ts
Normal file
39
packages/form/src/index.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { useForm } from "@mantine/form";
|
||||
import { zod4Resolver } from "mantine-form-zod-resolver";
|
||||
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" | "validateInputOnChange"
|
||||
>,
|
||||
) => {
|
||||
const t = useI18n();
|
||||
|
||||
z.config({
|
||||
customError: zodErrorMap(t),
|
||||
});
|
||||
return useForm<inferPossibleSchema<TSchema>>({
|
||||
...options,
|
||||
validateInputOnBlur: true,
|
||||
validateInputOnChange: true,
|
||||
validate: zod4Resolver(schema),
|
||||
});
|
||||
};
|
||||
23
packages/form/src/types.ts
Normal file
23
packages/form/src/types.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { ChangeEvent, FocusEvent } from "react";
|
||||
|
||||
export interface InputPropsFor<T, TOnChangeArg, TComponent extends HTMLElement = HTMLInputElement> extends BasePropsFor<
|
||||
TOnChangeArg,
|
||||
TComponent
|
||||
> {
|
||||
value?: T;
|
||||
defaultValue?: T;
|
||||
}
|
||||
|
||||
interface BasePropsFor<TOnChangeArg, TComponent extends HTMLElement> {
|
||||
onChange: (value: TOnChangeArg) => void;
|
||||
error?: string;
|
||||
onBlur?: (event: FocusEvent<TComponent>) => void;
|
||||
onFocus?: (event: FocusEvent<TComponent>) => void;
|
||||
}
|
||||
|
||||
export interface CheckboxProps<
|
||||
TOnChangeArg = ChangeEvent<HTMLInputElement>,
|
||||
TComponent extends HTMLElement = HTMLInputElement,
|
||||
> extends BasePropsFor<TOnChangeArg, TComponent> {
|
||||
checked?: boolean;
|
||||
}
|
||||
Reference in New Issue
Block a user