Search code examples
reactjsreact-datetime

react-datetime + open and disableOnClickOutside prop don't work together


I have problem for closing date picker here my code. I am using this npm package

/* 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}
      />
    );
  }
}

Using, this code date picker open by default when I select then date picker would be close. but my problem is when I click outside then date picker not close so how should I close this date picker when I click outside.


Solution

  • remove the open from <Datetime /> then it will work. read the docs about open https://github.com/YouCanBookMe/react-datetime