【发布时间】:2020-12-26 23:21:45
【问题描述】:
我对反应还很陌生,非常感谢这里的一些指示:
我有一个带有某些选项集的自动完成下拉菜单。我希望根据我的条件默认选择其中一个。
据我了解,这可以通过 getOptionSelected prop 在自动完成中完成。
我创建了一个示例来测试它,但是它没有按预期工作。你能帮忙吗?
/* eslint-disable no-use-before-define */
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
export default function ComboBox() {
return (
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option: { title: string; year: number }) => option.title}
getOptionSelected={(option) => option.year === 1994}
style={{ width: 300 }}
renderInput={(params) => (
<TextField {...params} label="Combo box" variant="outlined" />
)}
/>
);
}
// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
{ title: "The Shawshank Redemption", year: 1994 },
{ title: "The Godfather", year: 1972 },
{ title: "The Godfather: Part II", year: 1974 }
];
【问题讨论】:
标签: reactjs react-native autocomplete material-ui