Bug. To reproduce, you can use the link below. Note: This is NOT in isMulti mode. [] makes sense for isMulti.
Code Sandbox
This seems to be an issue that has happened in the past.
Let me know if I can provide any other details. The code sandbox above is simply one of your examples, but with an added console.log call from onChange, nothing else.
Also ran into this 馃檪 Solved for now with a wrapping onChangeHandler:
onChangeHandler = (value, actionMeta) => {
if (this.props.onChange) {
// fix react-select returning [] on clear
if (Array.isArray(value) && value.length === 0) {
this.props.onChange({}, actionMeta);
} else {
this.props.onChange(value, actionMeta);
}
}
};
Just ran into this one today. Definitely doesn't make sense to return an empty array if it's a single select. The item or null is what makes sense to return IMO.
single select, by default, IS NOT clearable, but backspaceRemovesValue by default is true, so single select can be cleaned by backspace, it is very confusing.
There is a PR (https://github.com/JedWatson/react-select/pull/3216) and I've put a temporary corrected version up at github:mWater/react-select#fix_2682_built for anyone else who is blocked by this bug.
Here is a workaround that works with search input and clearable select :
onSelectChange = item => {
// Prevent backspace in input (item.length === undefined)
if (item === null || item.length === undefined) {
// Write your code here
}
}
I hope it help someone
Basically if hitting backspace and nothing is actually selected, then why fire onChange in the first place?