简体   繁体   English

我如何在反应中使用材料 ui 库从我的自动完成框中获得选择

[英]How would i get the selected from my autocomplete box using material ui library in react

Below is the code which uses an external library called material ui and implements a search box with autocomplete.下面是使用名为 material ui 的外部库并实现具有自动完成功能的搜索框的代码。 when a value is selected a input tag gets created and has value:"selected value", how would i access the value to be able to do something with it.当一个值被选择时,一个输入标签被创建并具有值:“选定的值”,我将如何访问该值以便能够用它做某事。

import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";

class ContractsList extends React.Component {
  render() {
    const contracts = this.props.contracts;
    return (
      <div>
        <div className="searchbar">
          <h1>All Aboard</h1>
        </div>
        <div className="search-bar">
          <Autocomplete
            id="search-bar-id"
            disableClearable
            options={contracts.map(contract => contract.name)}
            renderInput={params => (
              <TextField
                {...params}
                label="Search contract"
                margin="normal"
                variant="outlined"
                InputProps={{ ...params.InputProps, type: "search" }}
              />
            )}
          />{" "}
        </div>
      </div>
    );
  }
}

export default ContractsList;

im trying to get the selected value from the input field but its not working我试图从输入字段中获取选定的值,但它不起作用

The onChange callback is being called once you change the value of the autocomplete (change of value is basically once you select/remove items from your list).一旦您更改自动完成的值,就会调用onChange回调(值的更改基本上是在您从列表中选择/删除项目后)。

onChange={(ev, value) => {
  console.log(value);
}}

Here is a working example as part of the autocomplete:这是一个作为自动完成的一部分的工作示例:

<Autocomplete
  onChange={(ev, value) => {
    console.log(value);
  }}
  id="combo-box-demo"
  options={autoCompleteItems}
  getOptionLabel={(option) => option.title}
  style={{ width: 300 }}
  renderInput={(params) => <TextField {...params} label="Combo box" variant="outlined" />}
/>

You can see a codesandbox here .你可以在这里看到一个代码框

暂无
暂无

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

相关问题 当为 Material UI Autocomplete React 组件选择任何选项时,如何获得事件? - How do I get an event when any option is selected for a Material UI Autocomplete React component? 如何覆盖材质 UI 组件中的自动完成选择选项 - How can I override autocomplete selected option in material UI components 使用 Material UI 响应自动完成 - React Autocomplete with Material UI 如何获取 material-ui 的自动完成 Select 的值? - How can I get the value of the Autocomplete Select of material-ui? React js material-ui 自动完成从 renderInput 中获取所选元素并切换到文本字段的 InputProps - React js material-ui Autocomplete take the selected element from the renderInput and switch to the InputProps of the textfield React material-ui Autocomplete 的多个选定值? - Multiple selected values of React material-ui Autocomplete? React:如何使选定的选项不显示在 Material UI Autocomplete 的输入字段中 - React: How to make selected options not to show in input field of Material UI Autocomplete 如何使用 React 在对话框 (material-ui) 中传递组件? - How do I pass in a component in a dialogue box (material-ui), using React? 如何使用 React 和 Material UI 访问列表中所选项目的索引? - How do I access the index of a selected item in list using React and Material UI? 使用 material-ui 自动完成获取所选值的“ID” - Get the "ID" of the selected value with material-ui autocomplete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM