rkit / react-select2-wrapper

Wrapper for Select2
MIT License
163 stars 97 forks source link

using the onSelect event, you cannot find the previous select value #23

Closed sferoze closed 8 years ago

sferoze commented 8 years ago

I am logged out the event from the onSelect and onChange functions.

I looked at the object in console and nowhere to be found is the previous value. It only gives data on the new value selected. But how can I get access to the previous value?

rkit commented 8 years ago

Maybe via https://github.com/rkit/react-select2-wrapper#access-to-select2 ? What is your case?

afgarcia86 commented 8 years ago

This is what I am using for a single select

changeSelect(select) {
  var newState = {}
  newState[select.currentTarget.name] = select.currentTarget.value
  this.setState(newState)
}

This is what I am using for a multi select

changeSelect(select) {
    var newState = {}
    newState[select.currentTarget.name] = helpers.multiSelector(select.currentTarget)
    this.setState(newState)
}

and in my helpers.js I have

multiSelector : function(select) {
  var selectedValues = [];
  for (var i = 0; i < select.length; i++) {
    if (select.options[i].selected) selectedValues.push(select.options[i].value);
  }
  return selectedValues;
}
rkit commented 8 years ago

I closed. If you have any problems, let me know.