question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Numeric Values with Multi-Select option not working

See original GitHub issue

Unable to get multi select working with options that have integers as values. This would be particularly useful if trying to collect a list of Ids.

  1. Visit http://jedwatson.github.io/react-select/ down to Numeric Values

  2. Check Multi-Select from the checkboxes

  3. Select an option from the drop down

You will then see that the select component does not populate

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
paulirwincommented, Jan 20, 2017

The example @pelhage references seems to not be working because of the simpleValue attribute being added. In that example, it is not simple values that are being passed in, but rather an array of objects with numeric value properties.

I confirmed that with 1.0.0-rc2 multi-select does work with numeric property values, as long as you do not use the simpleValue option.

It does not seem like passing an array of numbers (rather than an array of objects with number values) works with the simpleValue option, so I think that option is broken. You can work around this by mapping the array of numbers to an array of objects with myArray.map(i => { return { label: i, value: i }; })

Example that works with objects with numeric values (this was scaffolded from create-react-app):

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import "react-select/dist/react-select.css";
import Select from "react-select";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      products: [
        { label: "Foo", value: 1},
        { label: "Bar", value: 2},
        { label: "Baz", value: 3},
        { label: "Fuz", value: 4},
      ],
      selectedValues: []
    }
  }

  onChange(values) {
    this.setState({
      selectedValues: values
    });
  }

  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <form>
          <div style={{ width: 300, margin: "1em"}}>
            <Select multi options={this.state.products} onChange={this.onChange.bind(this)} value={this.state.selectedValues} />
          </div>
          <ul style={{ width: 300 }}>
            {this.state.selectedValues.map(i => <li>{i.value}</li>)}
          </ul>
        </form>
      </div>
    );
  }
}

export default App;

Update: here is an example with using an array of numbers instead of an array of objects, using map:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import "react-select/dist/react-select.css";
import Select from "react-select";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      productIds: [1, 2, 3, 4],
      selectedValues: []
    }
  }

  onChange(values) {
    this.setState({
      selectedValues: values
    });
  }

  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <form>
          <div style={{ width: 300, margin: "1em"}}>
            <Select multi options={this.state.productIds.map(i => { return { label: i, value: i }; })} onChange={this.onChange.bind(this)} value={this.state.selectedValues} />
          </div>
          <ul style={{ width: 300 }}>
            {this.state.selectedValues.map(i => <li>{i.value}</li>)}
          </ul>
        </form>
      </div>
    );
  }
}

export default App;
0reactions
bladeycommented, Jun 2, 2020

Hello -

In an effort to sustain the react-select project going forward, we’re cleaning up and 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel Form:select() Multi select options not working if array ...
I want to select multiple options in select, but its only work if Array index key is string, If array index key is...
Read more >
Solved: multi select list option input do not display val...
Solved: I´ve been created a flow starting with manually trigger a flow. I set two input text, one numeric an other one as...
Read more >
St.multiselect autocomplete/match not working for numerical ...
Hi there, :wave: I was wondering if anyone has tried to enter partial data in the multiselect box and have the options narrowed...
Read more >
Kendo Multi Select Option couldn't select multi selected values
Just a quick note, because your problem may indeed be a simple one. It seems like you are mixing numbers with strings. In...
Read more >
Recode Values - Qualtrics
Not every question type has the option to recode values. Text Entry questions, for example, are infinitely variable and so cannot have a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found