9

I am building out a listing of checkboxes and only want the user to be able to select 2 checkboxes and then it will disable the checkboxes. I have a disabled prop which I can pass a boolean but having trouble with the logic to disable the checkbox.

<UISelectableButton
    key={i}
    block={true}
    value={workflow}
    disabled={selectedRevisions > 1 && true}
    onSelectedChange={this.onSelectedChange}
    onClick={() => this.handleRevisions(workflow)}
    type="checkbox"
/>

For the onSelectedChange I have a function that will hold the index of how many checkboxes are currently selected. I can easily disable the buttons with a ternary operator by doing selectedRevisions > 2 then anytime there are more than 2 items selected then I disable the buttons. The problem with this is that will disable all the buttons and I don't want to disable any buttons that have been selected. Is there a way to check if the checkbox has been selected and still pass disabled a boolean and not a function.

2
  • PO title is wrong Commented Aug 7, 2018 at 1:56
  • What are you disabling? Checkboxes or buttons? Commented Aug 7, 2018 at 2:00

3 Answers 3

12

You could keep an object in state that keep track of the checkbox values, and in your render method you can check if there are 2 or more checkboxes that are checked and use this to disable the others.

Example

class App extends React.Component {
  state = { checked: {} };

  onSelectedChange = index => {
    this.setState(previousState => ({
      checked: {
        ...previousState.checked,
        [index]: !previousState.checked[index]
      }
    }));
  };

  render() {
    const { checked } = this.state;
    const checkedCount = Object.keys(checked).filter(key => checked[key]).length;
    const disabled = checkedCount > 1;

    return (
      <div>
        {Array.from({ length: 5 }, (_element, index) => (
          <input
            key={index}
            onChange={() => this.onSelectedChange(index)}
            type="checkbox"
            checked={checked[index] || false}
            disabled={!checked[index] && disabled}
          />
        ))}
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root"></div>

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

Comments

1

with bootstrap it's only this

                <input
                    type="checkbox"
                    className="form-check-input"
                    id="exampleCheck1"
                    disabled={true}
                />

2 Comments

Lol. This would be the worst answer for this question.
lol. You probably didn't ven understand the question
0

you can initiate a counter variable and track & when chckebox is checked more then 2 times then add a condition at disabled on it

like i am adding based on if one checkbox is checked below

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { name, checked } = event.target;
setCheckedState({
  ...checkedState,
  [name]: checked,
});

checked ? setCheckedItem(name) : setCheckedItem("");};

<div className="flex gap-4 pt-4">
            {catagory.map((eachSource) => (
              <label key={eachSource}>
                <input
                  type="checkbox"
                  name={eachSource}
                  checked={checkedState[eachSource] || false}
                  disabled={
                    (eachSource != checkedItem && checkedItem != "") ||
                    false
                  }
                  onChange={handleChange}
                />
                {eachSource}
              </label>
            ))}
          </div>

Comments

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.