import { DatePicker } from "antd";
import moment from "moment";
import React, { forwardRef } from "react";
//To be used as a form element only.

const dateFormat = "MM/DD/YYYY";

const FormDatePicker = (
  { value, onChange, onBlur, onlyFuture, ...restProps },
  ref
) => {
  const handleChange = (newDate) => {
    if (value !== newDate && onChange) {
      onChange(newDate);
    }
  };

  const handleKeyDown = (e) => {
    if (e.key.toLowerCase() === "t") {
      if (onChange) {
        onChange(new moment());
      }
    }
  };

  return (
    <div onKeyDown={handleKeyDown}>
      <DatePicker
        value={value ? moment(value) : null}
        onChange={handleChange}
        format={dateFormat}
        onBlur={onBlur}
        disabledTime
        {...(onlyFuture && {
          disabledDate: (d) => moment().subtract(1, "day").isAfter(d),
        })}
        {...restProps}
      />
    </div>
  );
};

export default forwardRef(FormDatePicker);
