Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - Select multiple checkboxes holding Shift key

I have a list of checkboxes in my React App. When I hold Shift key and click one - the others between current and the nearest checked one should become selected.

For now I'm tring to do somethimg like this:

<input onChange={(e)=>this.handleCheckbox(e)} value={id} checked={this.state.selected.IndexOf(id) > -1} type="checkbox" />

handleCheckbox(e){
  if(e.shiftKey){
    console.log("shiftKey is hold")
  }
  //here goes some logic to save checkboxes in the state
}

But the condition if(e.shiftKey) is never executed. What am I doing wrong?

like image 229
Anna Avatar asked Oct 19 '25 22:10

Anna


1 Answers

For future visitors:

To check if the Shift key is pressed, check the e.nativeEvent.shiftKey in the click event.

You still have to implement the logic for actually checking the boxes. If you need help with that, let me know.

Hope this helps.

like image 153
justcode Avatar answered Oct 22 '25 14:10

justcode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!