jakezatecky / react-dual-listbox

A feature-rich dual listbox for React.
https://jakezatecky.github.io/react-dual-listbox/
MIT License
109 stars 58 forks source link

Need a function to handle the left and right click. #117

Closed droska closed 3 years ago

droska commented 3 years ago

Hello, great work! i'm using your awesome library, and i'm trying to call a POST request when an item is selected to go right and call a different POST request when an item is selected to go left.

There is any function that can help me to do this?

Thank you very much!

jakezatecky commented 3 years ago

While my response is very delayed, I thought I would include a solution here for anyone who needs this type of functionality.

You can essentially watch the onChange function and if the length of the new selected values is greater than the previous state, then you know that items have move to the right. Otherwise, they have moved to the left:

onChange(newSelected, targetItems) {
  const { selected: oldSelected } = this.state;
  const movedRight = newSelected.length > oldSelected.length;

  if (movedRight) {
    // Do something
  } else {
    // Do something else
  }
}