【发布时间】:2019-12-19 18:58:45
【问题描述】:
我在一个表单上有两个自动完成组件。当第一个自动完成的值改变时,我想改变第二个自动完成的值。请参阅下面的示例代码。当第一个自动完成的值发生更改时,我将“movie2”设置为状态并将其作为值传递给第二个自动完成。但是第二个自动完成没有选择值。任何帮助将不胜感激。
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
class Demo extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<>
<Autocomplete
id="combo-box-demo"
value={this.state.movie1}
options={top100Films}
getOptionLabel={option => option.title}
style={{ width: 300 }}
renderInput={params => (
<TextField
{...params}
label="Combo box"
variant="outlined"
fullWidth
/>
)}
onChange={(event, val) => this.setState({ movie1: val, movie2: top100Films[2] })}
/>
<Autocomplete
id="combo-box-demo"
value={this.state.movie2}
options={top100Films}
getOptionLabel={option => option.title}
style={{ width: 300 }}
renderInput={params => (
<TextField
value={this.state.movie2 ? this.state.movie2.title : ""}
{...params}
label="Combo box"
variant="outlined"
fullWidth
/>
)}
/>
</>
);
}
}
export default Demo;
// 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 material-ui