我有一个material-ui-time-picker
,我想控制此输入,它很好用,但是我想编辑从键盘输入的时间,而不是单击时钟上的输入。
我的代码是:
import React, { Component } from "react";
import { TimePicker } from "material-ui-time-picker";
import { Input as Time, Dialog as Clock } from "@material-ui/core";
openDialog = () => this.setState({ isOpen: true });
closeDialog = () => this.setState({ isOpen: false });
handleDialogTimeChange = newValue => {
const hours = newValue
.getHours()
.toString()
.padStart(2, "0");
const minutes = newValue
.getMinutes()
.toString()
.padStart(2, "0");
const textValue = hours + ":" + minutes;
this.setState({ time: textValue });
};
handleKeyboardTimeChange = time => this.setState({ time });
createDateFromTextValue = value => {
const splitParts = value.split(":");
return new Date(1970, 1, 1, splitParts[0], splitParts[1]);
};
render() {
return (
<div>
<Time
value={this.state.time}
onChange={this.handleKeyboardTimeChange}
endAdornment={
<InputAdornment position="end">
<IconButton onClick={this.openDialog}>
<AccessTime />
</IconButton>
</InputAdornment>
}
//}
/>
<Clock maxWidth="xs" open={this.state.isOpen}>
<TimePicker
mode="24h"
value={this.createDateFromTextValue(this.state.time)}
onChange={this.handleDialogTimeChange}
autoOk={true}
cancelLabel=""
okLabel=""
placeholder=""
disableUnderline={true}
/>
</Clock>
</div>
);
}
我的沙盒:https://codesandbox.io/s/vm9wm19p27
运行它时,我会得到此输入,但是当我编辑他的值时,该输入将消失。
我该如何解决?
答案 0 :(得分:1)
我已经分叉了您的沙箱,并做了一些调整。尽管我尚未解决问题,但我只会向您展示当前的问题。
https://codesandbox.io/s/j24rqql9n9
我修改了两行
在您的构造函数中,我添加了
this.handleKeyboardTimeChange = this.handleKeyboardTimeChange.bind(this)
和您的handleKeyboardTimeChange:
handleKeyboardTimeChange(event) {
this.setState({ time: event.target.value });
}
这只是将状态设置为从您在那里看到的值传入的确切值。没有其他验证。