Is it possible to open React-Select component as it mounts?

时光总嘲笑我的痴心妄想 提交于 2019-12-14 04:20:24

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/54266127/is-it-possible-to-open-react-select-component-as-it-mounts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!