0

I have 3 checkboxes CheckboxA, CheckboxB, CheckboxC. If checkboxB or checkboxC are checked, I want checkboxA to be checked as well

basic checkbox example from react-hook-form https://codesandbox.io/s/cool-clarke-y8cjsw?file=/src/App.js:1792-1801

enter image description here

2
  • what does that mean If checkboxB or checkboxC are checked, I want checkboxA to be checked as well. Please be specific. Commented Jul 12, 2022 at 7:28
  • Think of it this way, there are 3 checkboxes. When I tick the 2nd or 3rd checkbox, the 1st checkbox will be automatically marked I added a picture maybe it will be more understandable and there is a codesandbox link @NullPointerException Commented Jul 12, 2022 at 7:42

2 Answers 2

1

In onChange you can check if current input is not A and its value is checked so you can update your new value

if (option !== "a" && e.target.checked) valueCopy[0] = "a";

Checkboxes

const Checkboxes = ({ options, control, name }) => {
  const { field } = useController({
    control,
    name
  });

  return (
    <>
      {options.map((option, index) => (
        <input
          onChange={(e) => {
            const valueCopy = [...field.value];

            valueCopy[index] = e.target.checked ? e.target.value : null;
            if (option !== "a" && e.target.checked) valueCopy[0] = "a";
            field.onChange(valueCopy);
          }}
          key={option}
          type="checkbox"
          checked={field.value.includes(option)}
          value={option}
        />
      ))}
    </>
  );
};

You can check in my codesandbox

Sign up to request clarification or add additional context in comments.

4 Comments

Hello @iamhuynq Thanks a lot, it works according to the situation I posted, but it's a bit more complicated because I'm using react-hooks-form with Material-UI. and i could not work the solutions in the answers you gave above with Material Ui. I recreated the code in CodeSandBox. I hope there is a solution codesandbox.io/s/muddy-night-3gfto0?file=/src/App.js
ohh, i'll check
so what i want to do in this case When I tick the 2nd or 3rd checkbox, the 1st checkbox will be automatically marked only for group2 chekboxes
You can use setValue. Check in my codesandbox. Hope it help @IbrahimYolbir
1

Try adding this on your line 27:

(valueCopy[1] === 'b' || valueCopy[2] === 'c') && (valueCopy.shift(), valueCopy.unshift('a'));

2 Comments

Thanks a lot, it works according to the situation I posted, but it's a bit more complicated because I'm using react-hooks-form with Material-UI. and i could not work the solutions in the answers you gave above with Material Ui. I recreated the code in CodeSandBox. I hope there is a solution codesandbox.io/s/muddy-night-3gfto0?file=/src/App.js
so what i want to do in this case When I tick the 2nd or 3rd checkbox, the 1st checkbox will be automatically marked only for group2 chekboxes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.