Skip to content

Instantly share code, notes, and snippets.

@smashercosmo
Created November 25, 2024 07:02
Show Gist options
  • Save smashercosmo/f9629d5585d97d89d88b1bbbb3c3cb8e to your computer and use it in GitHub Desktop.
Save smashercosmo/f9629d5585d97d89d88b1bbbb3c3cb8e to your computer and use it in GitHub Desktop.
function Form() {
const { control } = useForm({
defaultValues: {
name: "",
};
});
return (
<form>
<SharedInput control={control} name="name" />
</form>
)
}
type Props<
TFieldValues extends FieldValues,
TPath extends FieldPathByValue<TFieldValues, string>,
> = {
control: Control<TFieldValues>;
name: TPath;
};
export function SharedInput<
TFieldValues extends FieldValues,
TPath extends FieldPathByValue<TFieldValues, string>,
>({ control, name }: Props<TFieldValues, TPath>) {
return (
<Controller
control={control}
name={name}
render={({ field: { onChange, value } }) => (
<input onChange={onChange} value={value} />
)}
/>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment