【发布时间】:2017-12-18 22:05:00
【问题描述】:
我在一个测试项目中使用 Material UI 中的 v1.0.0-beta.24,“下拉”菜单的工作方式与以前的版本不同,所以我想做的是在 Select 组件中设置一个占位符.
在我以前版本的示例中,我有这个:
<DropDownMenu
value={this.state.DivisionState}
onChange={this.handleChangeDivision}>
<MenuItem value={''} primaryText={'Select division'} />
{this.renderDivisionOptions()}
</DropDownMenu>
所以新版本不支持primaryText属性,这是我的代码:
import React from 'react';
import Select from 'material-ui/Select';
import {MenuItem, MenuIcon} from 'material-ui/Menu';
import {DatePicker} from 'material-ui-pickers';
import { FormControl } from 'material-ui/Form';
import { InputLabel } from 'material-ui/Input';
import cr from '../styles/general.css';
export default class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
DivisionData: [],
DivisionState: '',
};
this.renderDivisionOptions = this.renderDivisionOptions.bind(this);
this.handleChangeDivision = this.handleChangeDivision.bind(this);
}
componentDidMount() {
const divisionWS = 'http://localhost:8080/services/Divisions/getAll';
fetch(divisionWS)
.then(Response => Response.json())
.then(findResponse => {
console.log(findResponse);
this.setState({
DivisionData: findResponse.divisions,
});
});
}
handleChangeDivision(event, index, value) {
this.setState({ DivisionState: event.target.value});
}
renderDivisionOptions() {
return this.state.DivisionData.map((dt, i) => {
return (
<MenuItem
key={i}
value={dt.divDeptShrtDesc}>
{dt.divDeptShrtDesc}
</MenuItem>
);
});
}
render() {
return (
<div className={cr.container}>
<div className={cr.containerOfContainers}>
<div className={cr.inputContainer}>
<div className={cr.rows}>
<div>
<div>
<FormControl>
<InputLabel htmlFor="name-multiple">Division</InputLabel>
<Select
value={this.state.DivisionState}
onChange={this.handleChangeDivision}
autoWidth={false}
>
{this.renderDivisionOptions()}
</Select>
</FormControl>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
}
所以在这方面的任何帮助都会很好。
问候。
【问题讨论】:
-
拥有 this.state.divisionState = '你的占位符文本'。或者创建一个自定义的this.state.placeholderText:'example input',如果DivisionData为空有条件使用
-
您好,感谢您的宝贵时间。问题是它不是空的,选择填充了我运行应用程序时加载的 Web 服务......
-
在您的 .map 之上,但仍在该函数内部硬编码单个菜单项以用作占位符...
标签: javascript reactjs material-ui