解决方案是在 customEditor 中添加事件侦听器虽然左右仍然可以在日历上导航,但为其设置状态并继续跟踪。
constructor(props) {
super(props);
this.state = {
dateEditor: props.value,
selectedDate: props.value
};
this.handleChange = this.handleChange.bind(this);
}
componentDidMount() {
document.addEventListener('keydown', this.handleKeyDown, true);
}
componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyDown, true);
}
handleKeyDown = (e) => {
if (e.keyCode === 37) {
// Arrow left subtract one day
this.setState({ selectedDate: moment(this.state.selectedDate).subtract(1, 'days').format('L')});
}
if (e.keyCode === 39) {
// Arrow right add one day
this.setState({ selectedDate: moment(this.state.selectedDate).add(1, 'days').format('L')});
}
if (e.keyCode === 13) {
// Enter will commit selected date
let value = this.state.selectedDate;
this.setState({ ["dateEditor"]: value }, () => this.props.onCommit());
}
};