[英]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.