2023-11-08 15:01:46 +00:00
|
|
|
import { Fragment, useState, FC } from "react";
|
2023-11-02 10:29:43 +00:00
|
|
|
import { usePopper } from "react-popper";
|
2023-03-07 19:37:00 +00:00
|
|
|
import { Popover, Transition } from "@headlessui/react";
|
2023-10-16 14:57:22 +00:00
|
|
|
import { CalendarDays, X } from "lucide-react";
|
2023-03-07 19:37:00 +00:00
|
|
|
// react-datepicker
|
|
|
|
import DatePicker from "react-datepicker";
|
|
|
|
// import "react-datepicker/dist/react-datepicker.css";
|
2023-07-04 12:49:19 +00:00
|
|
|
import { renderDateFormat, renderShortDateWithYearFormat } from "helpers/date-time.helper";
|
2023-03-07 19:37:00 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-08-09 09:47:32 +00:00
|
|
|
label: string;
|
|
|
|
maxDate?: Date;
|
|
|
|
minDate?: Date;
|
2023-03-07 19:37:00 +00:00
|
|
|
onChange: (val: string | null) => void;
|
2023-08-09 09:47:32 +00:00
|
|
|
value: string | null;
|
2023-03-07 19:37:00 +00:00
|
|
|
};
|
|
|
|
|
2023-11-08 15:01:46 +00:00
|
|
|
export const IssueDateSelect: FC<Props> = ({ label, maxDate, minDate, onChange, value }) => {
|
|
|
|
const [referenceElement, setReferenceElement] = useState<HTMLDivElement | null>(null);
|
|
|
|
const [popperElement, setPopperElement] = useState<HTMLDivElement | null>(null);
|
2023-11-02 10:29:43 +00:00
|
|
|
|
|
|
|
const { styles, attributes } = usePopper(referenceElement, popperElement, {
|
|
|
|
placement: "bottom-start",
|
|
|
|
});
|
2023-03-07 19:37:00 +00:00
|
|
|
|
2023-11-02 10:29:43 +00:00
|
|
|
return (
|
|
|
|
<Popover className="relative flex items-center justify-center rounded-lg">
|
|
|
|
{({ close }) => (
|
|
|
|
<>
|
|
|
|
<Popover.Button>
|
|
|
|
<div
|
|
|
|
ref={setReferenceElement}
|
2023-12-10 10:18:10 +00:00
|
|
|
className="flex w-full cursor-pointer items-center justify-center gap-1 rounded border-[0.5px] border-custom-border-300 px-2 py-1 text-xs text-custom-text-200 hover:bg-custom-background-80"
|
2023-11-02 10:29:43 +00:00
|
|
|
>
|
|
|
|
{value ? (
|
|
|
|
<>
|
2023-11-08 12:21:32 +00:00
|
|
|
<CalendarDays className="h-3 w-3 flex-shrink-0" />
|
|
|
|
<span>{renderShortDateWithYearFormat(value)}</span>
|
2023-11-02 10:29:43 +00:00
|
|
|
<button onClick={() => onChange(null)}>
|
2023-11-08 12:21:32 +00:00
|
|
|
<X className="h-3 w-3 flex-shrink-0" />
|
2023-11-02 10:29:43 +00:00
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2023-11-08 12:21:32 +00:00
|
|
|
<CalendarDays className="h-3 w-3 flex-shrink-0 text-custom-text-300" />
|
|
|
|
<span className="text-custom-text-300">{label}</span>
|
2023-11-02 10:29:43 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Popover.Button>
|
2023-08-21 06:16:02 +00:00
|
|
|
|
2023-11-02 10:29:43 +00:00
|
|
|
<Transition
|
2023-11-08 15:01:46 +00:00
|
|
|
as={Fragment}
|
2023-11-02 10:29:43 +00:00
|
|
|
enter="transition ease-out duration-200"
|
|
|
|
enterFrom="opacity-0 translate-y-1"
|
|
|
|
enterTo="opacity-100 translate-y-0"
|
|
|
|
leave="transition ease-in duration-150"
|
|
|
|
leaveFrom="opacity-100 translate-y-0"
|
|
|
|
leaveTo="opacity-0 translate-y-1"
|
|
|
|
>
|
|
|
|
<Popover.Panel>
|
|
|
|
<div
|
2023-12-10 10:18:10 +00:00
|
|
|
className="absolute -left-10 top-10 z-20 transform overflow-hidden"
|
2023-11-02 10:29:43 +00:00
|
|
|
ref={setPopperElement}
|
|
|
|
style={styles.popper}
|
|
|
|
{...attributes.popper}
|
|
|
|
>
|
|
|
|
<DatePicker
|
|
|
|
selected={value ? new Date(value) : null}
|
|
|
|
onChange={(val) => {
|
|
|
|
if (!val) onChange("");
|
|
|
|
else onChange(renderDateFormat(val));
|
|
|
|
|
|
|
|
close();
|
|
|
|
}}
|
|
|
|
dateFormat="dd-MM-yyyy"
|
|
|
|
minDate={minDate}
|
|
|
|
maxDate={maxDate}
|
|
|
|
inline
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Popover.Panel>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
};
|