2023-01-30 17:46:02 +00:00
|
|
|
// react-datepicker
|
|
|
|
import DatePicker from "react-datepicker";
|
|
|
|
import "react-datepicker/dist/react-datepicker.css";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
renderAs?: "input" | "button";
|
|
|
|
value: Date | string | null | undefined;
|
2023-02-01 15:03:18 +00:00
|
|
|
onChange: (val: string | null) => void;
|
2023-01-30 17:46:02 +00:00
|
|
|
placeholder?: string;
|
|
|
|
displayShortForm?: boolean;
|
|
|
|
error?: boolean;
|
|
|
|
className?: string;
|
|
|
|
isClearable?: boolean;
|
2023-02-01 15:03:18 +00:00
|
|
|
disabled?: boolean;
|
2023-01-30 17:46:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const CustomDatePicker: React.FC<Props> = ({
|
|
|
|
renderAs = "button",
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
placeholder = "Select date",
|
|
|
|
displayShortForm = false,
|
|
|
|
error = false,
|
|
|
|
className = "",
|
|
|
|
isClearable = true,
|
2023-02-01 15:03:18 +00:00
|
|
|
disabled = false,
|
2023-01-30 17:46:02 +00:00
|
|
|
}) => (
|
|
|
|
<DatePicker
|
|
|
|
placeholderText={placeholder}
|
|
|
|
selected={value ? new Date(value) : null}
|
2023-02-01 15:03:18 +00:00
|
|
|
onChange={(val) => {
|
|
|
|
if (!val) onChange(null);
|
|
|
|
else {
|
|
|
|
const year = val.getFullYear();
|
|
|
|
let month: number | string = val.getMonth() + 1;
|
|
|
|
let date: number | string = val.getDate();
|
|
|
|
|
|
|
|
if (date < 10) date = `0${date}`;
|
|
|
|
if (month < 10) month = `0${month}`;
|
|
|
|
|
|
|
|
onChange(`${year}-${month}-${date}`);
|
|
|
|
}
|
|
|
|
}}
|
2023-01-30 17:46:02 +00:00
|
|
|
className={`${className} ${
|
|
|
|
renderAs === "input"
|
2023-02-01 15:03:18 +00:00
|
|
|
? "block bg-transparent text-sm focus:outline-none border-gray-300 px-3 py-2"
|
2023-01-30 17:46:02 +00:00
|
|
|
: renderAs === "button"
|
2023-02-01 15:03:18 +00:00
|
|
|
? `px-2 py-1 text-xs shadow-sm ${
|
|
|
|
disabled ? "" : "hover:bg-gray-100"
|
|
|
|
} focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 duration-300`
|
2023-01-30 17:46:02 +00:00
|
|
|
: ""
|
2023-02-01 15:03:18 +00:00
|
|
|
} ${error ? "border-red-500 bg-red-100" : ""} ${
|
|
|
|
disabled ? "cursor-not-allowed" : "cursor-pointer"
|
|
|
|
} w-full rounded-md bg-transparent border caret-transparent`}
|
|
|
|
dateFormat="dd-MM-yyyy"
|
2023-01-30 17:46:02 +00:00
|
|
|
isClearable={isClearable}
|
2023-02-01 15:03:18 +00:00
|
|
|
disabled={disabled}
|
2023-01-30 17:46:02 +00:00
|
|
|
/>
|
|
|
|
);
|