反应日期时间 + 打开和禁用 OnClick外部道具不能一起工作



我的代码在这里关闭日期选择器时遇到问题。我正在使用这个 npm 包

/* eslint linebreak-style: ["error", "windows"] */
/* eslint-disable no-unused-vars */
import React, { Component } from 'react';
import Datetime from 'react-datetime';
const toDay = new Date();
const month = toDay.getMonth();
let date = toDay.getDate();
let hours = toDay.getHours() % 12;
const amPm = toDay.getHours() >= 12 ? 'PM' : 'AM';
hours = hours > 0 ? hours : 12;
console.log(hours);
console.log(toDay.getHours());
let minutes = toDay.getMinutes();
const obj = { readOnly: true };
if (hours < 10) {
hours = `0${hours}`;
}
if ((minutes % 5) > 0) {
minutes += (5 - (minutes % 5));
}
if (minutes > 55) {
minutes = 0;
hours += 1;
if (hours > 12) {
hours = 12;
date += 1;
}
}
if (minutes < 10) {
minutes = `0${minutes}`;
}
const timeobj = {
minutes: {
step: 5,
},
};
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec',
];
const yesterday = Datetime.moment().subtract(1, 'day');
const maxDays = Datetime.moment().add(6, 'month');
const valid = current => current.isAfter(yesterday) && current.isBefore(maxDays);
export default class DatePicker extends Component {
constructor() {
super();
this.state = {
// open: true,
};
}

render() {
return (
<Datetime
timeConstraints={timeobj}
isValidDate={valid}
defaultValue={`${monthNames[month]} ${date} at ${hours}:${minutes} ${amPm}`}
dateFormat="MMM DD [at]"
inputProps={obj}             
open
closeOnSelect
disableOnClickOutside={false}
/>
);
}
}

使用,当我选择时,此代码日期选择器默认打开,然后日期选择器将关闭。 但我的问题是当我单击外部时,日期选择器不会关闭,因此当我单击外部时,我应该如何关闭此日期选择器。

<Datetime />中删除open然后它会起作用。 阅读有关openhttps://github.com/YouCanBookMe/react-datetime 的文档

最新更新