react-select icon indicating copy to clipboard operation
react-select copied to clipboard

onChange returns the selected value, not the complete event

Open OsamaShabrez opened this issue 9 years ago • 46 comments

onChange simply returns the new value and label and not the whole event.

I understand we can use the hidden input field to fire an onChange but this does not happen automatically and while we can manually fire an onChange for hidden input field, we can also wrap an event around the Select. Here is how I am doing it following #520:

<Select
  name="form-field-name"
  value={val}
  options={options}
  onChange={(val)=> {onChangeFunc({target: { name:'revision_id', value: val.value }})}}
/>

Creating an object myself and passing it to my onChange handler. Is this the right way to do it or you can suggest a better way around it?

OsamaShabrez avatar Mar 28 '17 16:03 OsamaShabrez

👍 I've found that can cause issues with redux-form's field component, it would be great to get the original event passed as the second argument on the onChange callback.

jwarykowski avatar Apr 13 '17 00:04 jwarykowski

Currently there is no way to get the original event, at least I didn't found one and there no mention of original event in docs.

OsamaShabrez avatar Apr 14 '17 12:04 OsamaShabrez

Would love to see this feature implemented, you can do so much from getting the original event!

browne0 avatar Jun 07 '17 15:06 browne0

Another workaround: you can use this to access the name of the select (or any of its other attributes). Example:

class SomeComponent extends React.Component {

  onChangeFunc(optionSelected) {
    const name = this.name;
    const value = optionSelected.value;
    const label = optionSelected.label;
  }

  render() {
    return(
      <Select
        name="form-field-name"
        value={val}
        options={options}
        onChange={this.onChangeFunc}
      />
    )
  }

}

kellenmace avatar Jun 21 '17 13:06 kellenmace

I've run into this issue, passing a value to an onChange handler passed from one of redux-form's field component, as @jonathanchrisp mentioned, causes the string to be spread operated by this line: https://github.com/erikras/redux-form/blob/master/src/ConnectedField.js#L103

For now the only solution I could think of was writing a single edge case if statement in the container for this component.

I feel like it's kind of a standard using events in onX handlers, so I'm all in favor for introducing that standard here. To maintain compatibility with people using the onChange handler as is, a simple wrapper could be introduced, and exposed through the props? The proposed solution for adding the event as a secondary argument would probably be better.

fonsleenaars avatar Oct 02 '17 12:10 fonsleenaars

Any update on this issue?

This library really needs to conform to the expected behavior of input elements. That is, send the original event instead of an object containing label and value.

carlos0202 avatar Feb 02 '18 18:02 carlos0202

I'm also looking for this feature, because either it's impossible right now to change the validation message when the field is required or I don't know how to do it. Here is a CodeSandbox that implements this solution to change the message, which doesn't work on Select due to the missing event.

Edit: Nevermind, I found this.

GuiRitter avatar May 17 '18 14:05 GuiRitter

Any chance of ever getting resolution on this issue?

gvincentlh avatar Jun 06 '18 22:06 gvincentlh

@gvincentlh this is just a guess, but I think this function should be changed so that it saves the event https://github.com/JedWatson/react-select/blob/master/src/Select.js#L452-L461

Then the event could be passed optionally instead of the value (there is a simpleValue option already, this could be similar I think)

ashnur avatar Jun 11 '18 09:06 ashnur

@JedWatson do you have an opinion on this that you care to share here?

ashnur avatar Jun 12 '18 14:06 ashnur

I didn't realise this until now and I feel I wasted my time a bit using in my project. This is so common that don't expect it to be not here.

kvedantmahajan avatar Jul 12 '18 07:07 kvedantmahajan

@kushalmahajan it's quite an interesting fact that react-select became the only viable/usable option for a customized select input control, despite all the drawbacks.

ashnur avatar Jul 12 '18 09:07 ashnur

well, @github really needs to fix their autocomplete 😂

a good alternative which I've been following is downshift, but it's more barebones rather than a plug-and-play kind of approach.

kumarharsh avatar Jul 12 '18 13:07 kumarharsh

This is what I came up with: https://gist.github.com/cyrilf/109fc97a22cd080abcc5068a30976696 Not perfect and a bit hacky, but it solved my use-case where I really needed an event

Hope it helps :v:

cyrilf avatar Aug 30 '18 15:08 cyrilf

Has anyone taken a look at Material UI's example for React Select and their onChange function? They recommend using React Select for an auto-complete example. It helped me solve the issue of an on change event. This is their sandbox https://codesandbox.io/s/5kvmpwpo9k . For easy reference this is their onChange example and their Select component. I hope it gets someone else started on a solution that works in their project.

handleChange = name => value => {
   this.setState({
      [name]: value,
    });
  };

  <Select
       classes={classes}
       styles={selectStyles}
       options={suggestions}
       components={components}
       value={this.state.single}
       onChange={this.handleChange('single')}
       placeholder="Search a country (start with a)"
       />

I wasn't familiar with multiple arrows in a function. This is a curried function and this had a nice explanation https://stackoverflow.com/questions/32782922/what-do-multiple-arrow-functions-mean-in-javascript

danyonsatterlee avatar Sep 05 '18 19:09 danyonsatterlee

I made it work by encapsulting the onChange function with another function that pass the Select name :

 onOptionChange = (selectName,selectedOption) => {
         //do something with selectName and selectedOption
    };
 <Select options=...
              name={selectName}
               value=...
               onChange={e => this.onOptionChange(selectName.id,e)}
   />}

bflorat avatar Sep 20 '18 13:09 bflorat

Nice hack. Why not just use vanillajs then?

ashnur avatar Sep 20 '18 16:09 ashnur

It's the method documented by the react.js team : https://reactjs.org/docs/handling-events.html#passing-arguments-to-event-handlers

bflorat avatar Sep 20 '18 21:09 bflorat

onOptionChange = (selectName,selectedOption) => {
    //do something with selectName and selectedOption
};
 <Select options=...
              name={selectName}
               value=...
               onChange={e => this.onOptionChange(selectName.id,e)}
   />}

no, I mean, the variable that seems to be a string, selectName, is actually an object with an .id property.. sure, I could just pass the whole state there, but that doesn't mean I get my original event object unless I somehow recreate a live reference to some dom element, which you seem to be doing here

ashnur avatar Sep 20 '18 21:09 ashnur

The method mentioned by bflorat returns the mouse click event on the react-select rendered div if doing a selection. It definitely would be nice to see this implemented, but can work around it for now. (edited to clarify).

escott88 avatar Nov 15 '18 17:11 escott88