mercredi 13 mars 2019

Adding minimum alongside maximum for Checkbox selection limit function in ReactJS

I have a react app which maps JSON values into checkboxes, and the JSON includes a min/max required value, I have made a function which sets the maximum value to the checkboxes from the JSON and it works fine, but whenever I try to add the minimum to the same functions it doesn't work I tried things like (this.props.min < this.state.currentData < this.props.max ) but it didn't work

Checkboxes.js Max function

....
selectData(id, event) {
    let isSelected = event.currentTarget.checked;
    if (isSelected) {
      if (this.state.currentData < this.props.max) {
        this.setState({ currentData: this.state.currentData + 1 });
      } else {
        event.preventDefault();
        event.currentTarget.checked = false;
      }
    } else {
      this.setState({ currentData: this.state.currentData - 1 });
    }
  }

....

Full code: https://codesandbox.io/embed/wklrxy5x15?fontsize=14




Aucun commentaire:

Enregistrer un commentaire