Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Multi select] onChange returns null when no values instead of [] #3632

Closed
nicolasletoublon opened this issue Jun 17, 2019 · 16 comments · Fixed by #4339
Closed

[Multi select] onChange returns null when no values instead of [] #3632

nicolasletoublon opened this issue Jun 17, 2019 · 16 comments · Fixed by #4339
Labels
issue/bug-unconfirmed Issues that describe a bug that hasn't been confirmed by a maintainer yet issue/reviewed Issue has recently been reviewed (mid-2020)

Comments

@nicolasletoublon
Copy link

All is in the title. We implemented a version of the Multi select. and it seems that when you clear the last value (or the only one) of the field, the onChange returns you null. I would clearly expect an empty array at this stage :)

Here is your sandbox updated with the onChange callback :)
https://5jtqt.codesandbox.io/

Thanks for looking at it!

@ragesoss
Copy link

This appears to be the intended behavor with v3: #3416

I just ran into a bug in my app because of this change as well. Curious as to how others handle it.

ragesoss added a commit to WikiEducationFoundation/WikiEduDashboard that referenced this issue Jun 17, 2019
react-select changed behavior with version 3, so that removing the last selected option from a multiselect results in value of `null` instead of `[]` as before. This was resulting in an error in the tickets selector, which assumes an array of selected filters.

Related: JedWatson/react-select#3632
@speir-wang
Copy link

And interestingly, if user uses the clear all button to remove all the values in Multi select, the value would be set to an empty array.

I was wondering why removing the last value results in null but clicking clear all button results in []? Shouldn't they be treated in the same manner?

@someden
Copy link

someden commented Jun 18, 2019

Simple example:

import React from 'react';
import Select from 'react-select';

export default () => (
  <Select
    isMulti
    options={[{ value: 'ocean', label: 'Ocean' }, { value: 'blue', label: 'Blue' }]}
    onChange={console.log}
  />
);

ScreenRecording2019-06-18at1

@someden
Copy link

someden commented Jun 18, 2019

Temporary workaround for this bug:

import React, { Component } from 'react';
import ReactSelect from 'react-select';

class Select extends Component {
  defaultProps = {
    onChange: () => {},
  };

  handleChange = selected =>
    this.props.onChange(!selected && this.props.isMulti ? [] : selected);

  render() {
    return <ReactSelect {...this.props} onChange={this.handleChange} />;
  }
};

export default Select;

@stearruda
Copy link

I was having some problems with that as well and to fix it I've redefined the value on the onChange to an empty array.

const MultiSelect = () => {
  const handleOnChange = value => {
    if (value === null) {
      value = [];
    }
  };

  return (
    <Select
      isSearchable
      isMulti
      onChange={handleOnChange}
    />
  );
}

export default MultiSelect;

@TravnikovDev
Copy link

I am still having the same Issue

@luksari
Copy link

luksari commented Nov 23, 2019

I was having the very same problem, needed to use onChange from redux-form in my component so, this is workaround for redux-form and typescript

const handleOnChange = (value: ValueType<SelectableItem>, action: ActionMeta) => {
   if (!onChange) return;
   if (value === null) {
     onChange([], action);
   } else {
     onChange(value, action);
   }
 };
<ReactSelect
     onChange={handleOnChange} ... />

@danielimmke
Copy link

Super simple inline fix for people Googling:

onChange={(value) => stateChangeMethod(value || [])}

Replace stateChangeMethod with whatever you're using to update state. In my case I was using Hooks so I had a specific function just for that bit of state.

@bladey
Copy link
Contributor

bladey commented May 28, 2020

Hello -

In an effort to sustain the react-select project going forward, we're closing old issues.

We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our efforts towards the current major version.

If you aren't using the latest version of react-select please consider upgrading to see if it resolves any issues you're having.

However, if you feel this issue is still relevant and you'd like us to review it - please leave a comment and we'll do our best to get back to you!

@bladey bladey closed this as completed May 28, 2020
@rusakovic
Copy link

@bladey Hello.

Thank you. But issue is still here.
If you remove value from input dropdown field by pressing backspace, returned value will be null but not []
react-select 3.1.0

@akhilaudable
Copy link

+1

@bladey
Copy link
Contributor

bladey commented May 30, 2020

Thanks @rusakovic!

@bladey bladey reopened this May 30, 2020
@bladey bladey added the issue/bug-unconfirmed Issues that describe a bug that hasn't been confirmed by a maintainer yet label May 30, 2020
@bladey bladey added the issue/reviewed Issue has recently been reviewed (mid-2020) label Jun 17, 2020
@kirkobyte
Copy link

Would the maintainers be happy with a PR that normalises empty values for isMulti fields to [] for initial empty values & after clearing the field? Then we could keep the normalisation #3416 intended, while also having a more useful empty state

@Methuselah96
Copy link
Collaborator

I'm definitely in favor of this. I'm working on porting react-select to TypeScript and it there a lot of places that return [] when there are no options selected and only couple of places where we actually remember to change it to null.

@bladey @JedWatson I can make a PR for this change if you guys are okay with this change. There are only a couple of places where we would need to change the behavior in order for it to be consistent.

@darrendahl
Copy link

darrendahl commented Jul 28, 2020

+1, causing some issues on my end, Basically the issue is that the isClearable "X" returns [] but removing all tags returns null

--- I know we can fix by making a wrapper and normalize to either null or [], but ideally would return same thing, since its a very similar action

@ejose19
Copy link

ejose19 commented Aug 16, 2020

I also agree the discrepancy between container "X" and element "X" is not expected, and empty multi select should be [] not null, to avoid any extra conversion by users.

I'm using (value ?? []) in the meanwhile.

@bladey bladey added issue/reviewed Issue has recently been reviewed (mid-2020) and removed issue/reviewed Issue has recently been reviewed (mid-2020) labels Aug 24, 2020
@Methuselah96 Methuselah96 added this to the 4.0 milestone Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue/bug-unconfirmed Issues that describe a bug that hasn't been confirmed by a maintainer yet issue/reviewed Issue has recently been reviewed (mid-2020)
Projects
None yet