onChange returns the selected value, not the complete event
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?
👍 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.
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.
Would love to see this feature implemented, you can do so much from getting the original event!
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}
/>
)
}
}
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.
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.
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.
Any chance of ever getting resolution on this issue?
@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)
@JedWatson do you have an opinion on this that you care to share here?
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.
@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.
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.
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:
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
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)}
/>}
Nice hack. Why not just use vanillajs then?
It's the method documented by the react.js team : https://reactjs.org/docs/handling-events.html#passing-arguments-to-event-handlers
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
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).