Last active
May 23, 2022 21:29
-
-
Save andyhite/340b291c25d42828b0bed9da827c2490 to your computer and use it in GitHub Desktop.
form-spike.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Form = ({ onSubmit, ...props }) => { | |
const formBag = useForm(props); | |
return ( | |
<form onSubmit={formContext.handleSubmit(onSubmit)}> | |
<FormProvider value={formBag}>{children}</FormProvider> | |
</form> | |
); | |
}; | |
type FieldProps = { | |
label: string; | |
name: string; | |
} | |
const Field = ({ label, name }: FieldProps) => { | |
const { register, getValues } = useFormContext(); | |
const values = getValues(name); | |
return ( | |
<> | |
<label>{label}</label> | |
<input {...register(name as FieldPath<typeof values>)} /> | |
</> | |
); | |
}; | |
const schema = yup.schema()...; | |
const FieldValues = typeof schema; | |
const PurchaseLabelForm = () => { | |
const defaultValues = schema.cast({}); | |
return ( | |
<Form | |
onSubmit={(values) => console.log(values)} | |
defaultValues={defaultValues} | |
> | |
<Field label="Name" name="name" /> | |
</Form> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment