简体   繁体   English

React-Select - 如何在 OnChange 发生之前获取表单的文本值?

[英]React-Select - How to get text value of form before OnChange occurs?

So I am working on a search bar for a website of mine, and I am having a bit of trouble with one of the components I'm using, react-select .所以我正在为我的网站开发一个搜索栏,我在使用其中一个组件react-select时遇到了一些麻烦。 Below is some example code下面是一些示例代码

const baseOptions = [
      { label: 'Users', options: 
      [
      ]
      },
      { label: 'Pages', options: 
      [
      ]
      },
      { label: 'Search Google Index', value: 'search_google_index', type: "google_index" },
      { label: 'Search Bing Index', value: 'search_bing_index', disabled: 'yes' },
    ]

  handleSearchChange = selectedOption => {
    this.setState({
      selectedOption,
    }, () => {
    console.log(`Option selected:`, this.state.selectedOption);
    switch (this.state.selectedOption.type) {
      case "user":
      window.location = "/@" + this.state.selectedOption.label
        break;
      case "google_index":
      console.log("Searching google index");

            /*Here is where I need to catch the value of react-select*/

        break;
      default:
        break;
    }

    });
  };

The issue问题

I am not sure how to fetch the value of what is typed into the box before the new value is selected.选择新值之前,我不确定如何获取输入到框中的内容的值。

The Goal目标

I am aiming to search google for the term typed into the field when a user selects "Search Google Index"当用户选择“搜索 Google 索引”时,我的目标是在 google 中搜索输入到该字段中的术语

According to react-select , to get the value typed in, you can use onInputChange props as following:根据react-select ,要获取输入的值,您可以使用onInputChange道具,如下所示:

  handleInputChange = (inputValue) => {
        //get the character entered by user here in inputValue
  };

In your component calling:在您的组件调用中:

<Select
onInputChange={handleInputChange}

....

/>

Demo演示

Update:更新:

Recommendation:推荐:

You should use react-select drop-down only to show the list of users not for search options.您应该仅使用 react-select 下拉菜单来显示用户列表,而不是搜索选项。 For search options you can have two buttons.对于搜索选项,您可以有两个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM