最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Conditionally disabled React Checkboxes - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Aug 7, 2018 at 1:53 James ZilchJames Zilch 1,2253 gold badges11 silver badges17 bronze badges 2
  • PO title is wrong – RIYAJ KHAN Commented Aug 7, 2018 at 1:56
  • What are you disabling? Checkboxes or buttons? – Leo Commented Aug 7, 2018 at 2:00
Add a comment  | 

3 Answers 3

Reset to default 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>

with bootstrap it's only this

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

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>
发布评论

评论列表(0)

  1. 暂无评论