【发布时间】:2020-04-12 17:46:02
【问题描述】:
我有以下代码。它显示两个自动完成控件。在第一个控件中选择的值决定了第二个控件的可能值。我的问题是,当在第一个控件上单击“清除”按钮时,它也应该清除第二个控件,因为它不再有效。第一个控件中的 OnChange 事件捕获“清除”事件并确实更改了第二个控件的基础选项列表,但显示的实际值仍然存在。如何以编程方式清除它?
import React, { Component } from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
import NoSsr from '@material-ui/core/NoSsr';
export class Home extends Component {
constructor(props) {
super(props);
this.state = {
aiList: [],
ucList: [],
aiLoading: true,
ucLoading: true
};
this.handleAIChange = this.handleAIChange.bind(this);
}
componentDidMount() {
fetch('http://localhost:8080/GetLeadAI')
.then(res => res.json())
.then(
(data) => { this.setState({ aiList: data, aiLoading: false })},
(error) => { this.setState({ aiLoading: false, error })}
)
}
handleAIChange = (event, values, reason) => {
if (reason==='select-option') {
fetch(`http://localhost:8080/GetUc?AIID=${encodeURIComponent(values.id)}`)
.then(res => res.json())
.then(
(data) => { this.setState({ ucList: data, ucLoading: false })},
(error) => { this.setState({ ucLoading: false, error })}
)
}
else {
this.setState({ ucList: [], ucLoading: true })
}
}
render() {
return (
<NoSsr>
<div><br/></div>
<Autocomplete
options={this.state.aiList}
getOptionLabel={(option) => option.description}
style={{ width: 600 }}
onChange={this.handleAIChange}
noOptionsText={this.state.aiLoading ? 'Loading AIs...' : 'No AI Data'}
renderInput={(params) => <TextField {...params} label="Lead AI" variant="outlined" />}
/>
<div><br/></div>
<Autocomplete
disabled={this.state.ucLoading}
options={this.state.ucList}
getOptionLabel={(option) => option.designCode + ' - ' + option.description}
style={{ width: 600 }}
noOptionsText={this.state.aiLoading ? 'Loading UCs...' : 'No UC Data'}
renderInput={(params) => <TextField {...params} label="Unpacked Chemical" variant="outlined" />}
/>
</NoSsr>
)
}
}
【问题讨论】:
标签: reactjs material-ui