如果我使用Material Ui创建了一个datepicker,我如何获得用户的输入值?
假设用户选择01.01.2016->我如何获取该值并将其存储为具有数据类型日期的变量?
到目前为止,我尝试做的是使用状态来传回一个JSON对象,但这似乎并没有起作用。例如:
getInitialState: function() {
return {
startDate: null,
endDate:null
};
},
render: function() {
return (
<div>
<DatePicker hintText="Start Date" value={this.state.startDate} onChange={this._handleStartInput} />
<DatePicker hintText="End Date" value={this.state.endDate} onChange={this._handleEndInput} />
</div>
);},
_handleStartInput: function(value) {
this.setState({
startDate: value
});
},
_handleEndInput: function(value) {
this.setState({
endDate: value
});
},
然后从那里我可以创建另一个拉取值的函数,但是在我选择日期之后,状态永远不会改变并显示在UI中。
答案 0 :(得分:3)
我尝试了这个并绘制了控制台值
<DatePicker container="inline" floatingLabelText="Fecha desde" onChange={(x, event) => this.setFechaDesde(x,event)} defaultDate={new Date()} />
和
setFechaDesde(x,event){
console.log(JSON.stringify(event));
console.log(JSON.stringify(x));
}
答案 1 :(得分:1)
您使用的是哪个版本的Material-UI?
通过阅读文档,您会看到
onChange 日期值更改时触发的回调函数。由于没有与更改相关的特定事件,因此第一个参数将始终为null,第二个参数将是新的Date实例。
您应该尝试在回调中添加第二个参数。
答案 2 :(得分:1)
你的onChange处理函数应该有两个参数:event和date,其中event始终为null,date是新的日期对象。
_handleStartInput: function(event, date) {
var currentState = this.state;
currentState.startDate = date;
this.setState(currentState);
},
_handleEndInput: function(event, date) {
var currentState = this.state;
currentState.endDate = date;
this.setState(currentState);
}