新手在这里做出反应,我正在一个多格式应用程序中构建一个搜索页面,用户可以在自动完成的文本字段中搜索具有名字,姓氏或学生ID的学生。
现在,自动完成文本字段的选项已成功传递并在光标位于文本字段中时显示,但键入或实际选择一个值似乎并不影响文本字段的值。
下面的代码
<Autocomplete
id="auto-complete"
options={values.suggestions}
getOptionLabel={option => option.stuId+" "+ option.fName+" "+option.lName}
autoComplete
includeInputInList
fullWidth
renderInput={params => (
<TextField {...params} margin="normal" fullWidth />)}
/>
值通过道具传递。
预期结果是使用选定学生的stuId更新状态为'searchKeyword'的结果,并显示自动完成文本字段的结果。我可以使用onChange或默认值功能吗?
答案 0 :(得分:0)
这是您要找的吗?
<Autocomplete
...
value={this.state.value}
onChange={(event, value) => this.setState({ value })}
...
/>
答案 1 :(得分:0)
你可以在钩子的选项状态中获取值
/* eslint-disable no-use-before-define */
import React, { useState } from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
export default function ComboBox() {
const [option, setOption] = useState("");
return (
<Autocomplete
id="combo-box-demo"
options={top10Films}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
value={option}
onChange={(e, v) => {
setOption(v);
}}
renderInput={(params) => (
<TextField {...params} label="Combo box" variant="outlined" />
)}
/>
);
}
// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top10Films = [
{ title: "The Shawshank Redemption", year: 1994 },
{ title: "The Godfather", year: 1972 },
{ title: "The Godfather: Part II", year: 1974 },
{ title: "The Dark Knight", year: 2008 },
{ title: "12 Angry Men", year: 1957 },
{ title: "Schindler's List", year: 1993 },
{ title: "Pulp Fiction", year: 1994 },
{ title: "The Lord of the Rings: The Return of the King", year: 2003 },
{ title: "The Good, the Bad and the Ugly", year: 1966 },
{ title: "Fight Club", year: 1999 }
];