-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Usage of Checkbox
and Switch
with react hook form
#98
Comments
@vedantnn71 I think you need to use Controller for that. ...
import { Controller, useForm } from 'react-hook-form';
export default function Survey() {
const {
register,
handleSubmit,
formState: { errors },
reset,
control,
} = useForm<{
isDesktop: boolean;
}>();
...
return (
<form>
<Label htmlFor="isDesktop">
Are you using a desktop version of our app?
</Label>
<Controller
control={control}
name="isDesktop"
render={({ field: { onChange, value, ref } }) => (
<Checkbox onCheckedChange={onChange} checked={value} ref={ref} />
)}
/>
<button>Submit</button>
</form>
);
} |
@its-monotype thank you! that helped me to solve it! |
Example for select input with react hook form controller. Confusing part for me was that the HTML select element is the <Controller
control={control}
name="jobPostEmployment"
render={({
field: { onChange, onBlur, value, name, ref },
fieldState: { invalid, isTouched, isDirty, error },
formState,
}) => (
<div>
<Select name={name} onValueChange={onChange} defaultValue={value}>
<SelectTrigger className="w-[180px] mt-2">
<SelectValue placeholder={formatSelectValue(value)} />
</SelectTrigger>
<SelectContent ref={ref}>
<SelectItem
value={
AdbJobPostEmploymentType.JobPostEmploymentTypeFullTime
}
>
{formatSelectValue(
AdbJobPostEmploymentType.JobPostEmploymentTypeFullTime
)}
</SelectItem>
<SelectItem
value={AdbJobPostEmploymentType.JobPostEmploymentTypeHourly}
>
{formatSelectValue(
AdbJobPostEmploymentType.JobPostEmploymentTypeHourly
)}
</SelectItem>
<SelectItem
value={
AdbJobPostEmploymentType.JobPostEmploymentTypePartTime
}
>
{formatSelectValue(
AdbJobPostEmploymentType.JobPostEmploymentTypePartTime
)}
</SelectItem>
</SelectContent>
</Select> |
@austincollinpena Your comment saved my life!!! 🙏🏻 |
Using this approach, how can you define stuff like "required" and "minLength"? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When using the
register
method of react hook form, the component does not change the state of the provided form.The text was updated successfully, but these errors were encountered: