2

Not just focusing, but to actually open the component and display options. I know this isn't simple on a regular select component (see Is it possible to use JS to open an HTML select to show its option list? ), but perhaps it is still possible with React-Select.

Laura
  • 8,100
  • 4
  • 40
  • 50
Ben Carp
  • 24,214
  • 9
  • 60
  • 72
  • Just to summarize, the first your select will mount you want to open it ? – Laura Jan 21 '19 at 15:56
  • @Laura, This is correct. – Ben Carp Jan 21 '19 at 18:25
  • @Laura's answer is excellent. Unfortunately, this cannot be achieved using a standard Select element. See: https://stackoverflow.com/questions/430237/is-it-possible-to-use-js-to-open-an-html-select-to-show-its-option-list – tombraider Jan 21 '19 at 19:41

1 Answers1

2

In react-select you can control the opening of the menu with menuIsOpen props. to achieve your goal I would use a combination of menuIsOpen, onInputChange and onFocus like this:

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      menuIsOpen: true
    };
  }

  onInputChange = (options, { action }) => {
    if (action === "menu-close") {
      this.setState({ menuIsOpen: false });
    }
  };

  onFocus = e => {
    this.setState({ menuIsOpen: true });
  };
  render() {
    return (
      <div className="App">
        <Select
          options={options}
          onFocus={this.onFocus}
          onInputChange={this.onInputChange}
          menuIsOpen={this.state.menuIsOpen}
        />
      </div>
    );
  }
}

onInputChange can receive the following events:

"set-value",
"input-change",
"input-blur",
"menu-close"

Depending of what kind of behaviour you're expecting I would update this live example.

Laura
  • 8,100
  • 4
  • 40
  • 50
  • It doesn't open again if an item is selected and the select `isMulti`. It only opens again if I remove the cursor from within the select and focus again. – nickornotto Jun 21 '20 at 17:12