Skip to content

Instantly share code, notes, and snippets.

@andyhite
Last active May 23, 2022 21:29
Show Gist options
  • Save andyhite/340b291c25d42828b0bed9da827c2490 to your computer and use it in GitHub Desktop.
Save andyhite/340b291c25d42828b0bed9da827c2490 to your computer and use it in GitHub Desktop.
form-spike.tsx
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