2023-01-30 17:46:02 +00:00
|
|
|
// react-datepicker
|
|
|
|
import DatePicker from "react-datepicker";
|
|
|
|
import "react-datepicker/dist/react-datepicker.css";
|
2023-02-02 13:03:46 +00:00
|
|
|
// helpers
|
|
|
|
import { renderDateFormat } from "helpers/date-time.helper";
|
2023-01-30 17:46:02 +00:00
|
|
|
|
|
|
|
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;
|
2023-06-23 11:50:05 +00:00
|
|
|
noBorder?: boolean;
|
2023-01-30 17:46:02 +00:00
|
|
|
className?: string;
|
|
|
|
isClearable?: boolean;
|
2023-02-01 15:03:18 +00:00
|
|
|
disabled?: boolean;
|
2023-07-19 09:14:04 +00:00
|
|
|
minDate?: Date;
|
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,
|
2023-06-23 11:50:05 +00:00
|
|
|
noBorder = false,
|
2023-01-30 17:46:02 +00:00
|
|
|
className = "",
|
|
|
|
isClearable = true,
|
2023-02-01 15:03:18 +00:00
|
|
|
disabled = false,
|
2023-07-19 09:14:04 +00:00
|
|
|
minDate,
|
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);
|
2023-02-02 13:03:46 +00:00
|
|
|
else onChange(renderDateFormat(val));
|
2023-02-01 15:03:18 +00:00
|
|
|
}}
|
2023-04-17 19:45:10 +00:00
|
|
|
className={`${
|
2023-01-30 17:46:02 +00:00
|
|
|
renderAs === "input"
|
2023-07-04 12:49:19 +00:00
|
|
|
? "block px-2 py-2 text-sm focus:outline-none"
|
2023-01-30 17:46:02 +00:00
|
|
|
: renderAs === "button"
|
2023-07-04 12:49:19 +00:00
|
|
|
? `px-2 py-1 text-xs shadow-sm ${
|
2023-07-10 07:17:00 +00:00
|
|
|
disabled ? "" : "hover:bg-custom-background-80"
|
2023-07-11 09:48:47 +00:00
|
|
|
} 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"
|
2023-06-23 11:50:05 +00:00
|
|
|
} ${
|
2023-07-17 10:58:23 +00:00
|
|
|
noBorder ? "" : "border border-custom-border-200"
|
2023-07-11 09:48:47 +00:00
|
|
|
} w-full rounded-md caret-transparent outline-none ${className}`}
|
2023-07-04 12:49:19 +00:00
|
|
|
dateFormat="MMM dd, yyyy"
|
2023-01-30 17:46:02 +00:00
|
|
|
isClearable={isClearable}
|
2023-02-01 15:03:18 +00:00
|
|
|
disabled={disabled}
|
2023-07-19 09:14:04 +00:00
|
|
|
minDate={minDate}
|
2023-01-30 17:46:02 +00:00
|
|
|
/>
|
|
|
|
);
|