rkit / react-select2-wrapper

Wrapper for Select2
MIT License
163 stars 97 forks source link

how to get the new value in the callback handler? #61

Closed smancang closed 6 years ago

smancang commented 7 years ago

There is only one parameter in the callback function, it is like an event object.But how to get the new value when changing selection.The parameter has a data property, but its value is always null.Should it be getten by the '.target.value' ? what if I set it multiple?what's more, when I select one option the dropdown doesn't fold automatically.

sznowicki commented 7 years ago

It's e.target.val;

hakunin commented 7 years ago

Proper way of getting the value is $(e.target).select2().val() IMO, as that will give you whatever you passed into the element / or was selected by UI instead of the string representation from DOM.

siddhanttambe commented 7 years ago

onChange callback is not firing in case of any text change in the select2 input box. Is there another callback for this?

sznowicki commented 7 years ago

@siddhanttambe sadly, as far as I remember you need to bind to jquery event on real element. Not sure if "ref" works here, I guess it's not and you need to use ReactDOM.getdomnode.

siddhanttambe commented 7 years ago

That is exactly what I was trying to avoid, @sznowicki . Thanks all the same!

sznowicki commented 7 years ago

@siddhanttambe "Leben is kein Ponyhof". We just used it as it is since we consider it legacy anyway, but maybe you could fix this issue within this repo.

rkit commented 7 years ago

Please, try this way:

<Select2 
  ref={(e) => { this.tags = e; }}
  onChange={() => {
    console.log(this.tag.el.val());
  }}
  // …
/>
Dumbear commented 7 years ago

@rkit Should be this.tag = e instead of this.tags = e. And it works, thanks.