React-select: Pressing backspace when item is selected already

Created on 13 Sep 2016  路  4Comments  路  Source: JedWatson/react-select

Hello, i think i have found a bug. Its important to have some option selected first to reproduce the behavior!

My component:

<Select
options={datasource}
value={this.state.value}
    onChange={value=>{
        let isArray = value == null ? false : value.constructor == Array
        this.setState({value: isArray ? null : value})
    }}
/>

Scenario A:

  1. select an option ( onChange param value == { value: X, label: Y} )
  2. click clearing cross ( onChange param value == null ) - which is expected result

Scenario B:

  1. select an option ( onChange param value == { value: X, label: Y} )
  2. press backspace ( onChange param value == []) - expected value == null

Explanation:
I mean, when you click clearing cross OR press backspace when the Select has MULTI enabled, value param is the same in both cases - an empty array. Thats correct, the result should be 0-N items, an array.

When you do this with SINGLE-value Select, clicking cross sets value param to null BUT pressing backspace sets it to empty array instead of null, which seems inconsistent and a bit confusing to me. Thats why there is the isArray condition in my onChange method. The result should be null/object, not an array.

Is this a bug or not?
Thanks for your answer in advance.

Good job & keep it up!
HK

Most helpful comment

I've created a pull request to fix this issue: https://github.com/JedWatson/react-select/pull/1381

All 4 comments

+1 Same situation and solving similarly with lodash isArray method.

I've created a pull request to fix this issue: https://github.com/JedWatson/react-select/pull/1381

I am facing the same problem in v2.1.0

This seems to be the same issue as https://github.com/JedWatson/react-select/issues/2682

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonorogers picture jonorogers  路  3Comments

geraldfullam picture geraldfullam  路  3Comments

x-yuri picture x-yuri  路  3Comments

steida picture steida  路  3Comments

pablote picture pablote  路  3Comments