Skip to content
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

Closed
valerius21 opened this issue Feb 18, 2023 · 5 comments
Closed

Usage of Checkbox and Switch with react hook form #98

valerius21 opened this issue Feb 18, 2023 · 5 comments

Comments

@valerius21
Copy link

When using the register method of react hook form, the component does not change the state of the provided form.

export default function Survey() {
  const {
    register,
    handleSubmit,
    formState: { errors },
    reset,
  } = useForm<{isDesktop: boolean}>();
 // ....
 return (<form>
      <Label htmlFor="isDesktop">Are you using a desktop version of our app?</Label>
      <Checkbox {...register("isDesktop")} />
     <button>Submit</button>
</form>)
}
@its-monotype
Copy link
Contributor

@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>
  );
}

@valerius21
Copy link
Author

@its-monotype thank you! that helped me to solve it!

@austincollinpena
Copy link

Example for select input with react hook form controller. Confusing part for me was that the HTML select element is the SelectContent element

 <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>

@Angelfire
Copy link

@austincollinpena Your comment saved my life!!! 🙏🏻

@MaximilianLloyd
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants