Example Request: Custom form hook that takes generic schema #423
Answered
by
edmundhung
dan-gamble
asked this question in
Q&A
Replies: 1 comment 1 reply
-
import { useForm } from '@conform-to/react';
import { parseWithZod, getZodConstraint } from '@conform-to/zod';
import { type ZodTypeAny, type input, type output } from 'zod';
function useLocalForm<Schema extends ZodTypeAny>({
id,
schema,
}: {
id: string;
schema: Schema;
}) {
const [form, fields] = useForm<input<Schema>, output<Schema>, string[]>({
id,
constraint: getZodConstraint(schema),
onValidate ({ formData }) {
return parseWithZod(formData, { schema })
},
shouldValidate: 'onBlur',
shouldRevalidate: 'onInput',
onSubmit (e) {
e.preventDefault()
},
})
return { form, fields }
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
dan-gamble
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've been trying to work out how the
useForm
returns the correctSchema
generic when it's passed azod
schema but I can't work it out.I have a custom hook I want to use that looks like this
However, with my hook currently the
form
type isform: FormMetadata<any, string[]>
instead ofform: FormMetadata<{firstName: string}, string[]>
How would I change the
options
orschema
to make this work generically?Beta Was this translation helpful? Give feedback.
All reactions