2023-10-11 06:35:53 +00:00
|
|
|
import React, { useState } from "react";
|
2023-05-18 13:37:24 +00:00
|
|
|
|
2023-10-11 06:35:53 +00:00
|
|
|
// react-popper
|
|
|
|
import { usePopper } from "react-popper";
|
2023-01-26 18:12:20 +00:00
|
|
|
// headless ui
|
2023-10-11 06:35:53 +00:00
|
|
|
import { Listbox } from "@headlessui/react";
|
2023-01-26 18:12:20 +00:00
|
|
|
// icons
|
|
|
|
import { ChevronDownIcon } from "@heroicons/react/20/solid";
|
2023-03-04 12:17:03 +00:00
|
|
|
import { CheckIcon } from "@heroicons/react/24/outline";
|
2023-07-17 12:05:47 +00:00
|
|
|
// types
|
|
|
|
import { DropdownProps } from "./types";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-07-17 12:05:47 +00:00
|
|
|
export type CustomSelectProps = DropdownProps & {
|
|
|
|
children: React.ReactNode;
|
2023-01-26 18:12:20 +00:00
|
|
|
value: any;
|
|
|
|
onChange: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
const CustomSelect = ({
|
2023-10-11 06:35:53 +00:00
|
|
|
customButtonClassName = "",
|
2023-07-17 12:05:47 +00:00
|
|
|
buttonClassName = "",
|
2023-10-11 06:35:53 +00:00
|
|
|
placement,
|
2023-01-26 18:12:20 +00:00
|
|
|
children,
|
2023-07-17 12:05:47 +00:00
|
|
|
className = "",
|
|
|
|
customButton,
|
|
|
|
disabled = false,
|
|
|
|
input = false,
|
2023-01-26 18:12:20 +00:00
|
|
|
label,
|
2023-07-17 12:05:47 +00:00
|
|
|
maxHeight = "md",
|
|
|
|
noChevron = false,
|
2023-01-26 18:12:20 +00:00
|
|
|
onChange,
|
2023-07-17 12:05:47 +00:00
|
|
|
optionsClassName = "",
|
|
|
|
value,
|
2023-01-26 18:12:20 +00:00
|
|
|
width = "auto",
|
2023-10-11 06:35:53 +00:00
|
|
|
}: CustomSelectProps) => {
|
|
|
|
const [referenceElement, setReferenceElement] = useState<HTMLButtonElement | null>(null);
|
|
|
|
const [popperElement, setPopperElement] = useState<HTMLDivElement | null>(null);
|
|
|
|
|
|
|
|
const { styles, attributes } = usePopper(referenceElement, popperElement, {
|
|
|
|
placement: placement ?? "bottom-start",
|
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-10-11 06:35:53 +00:00
|
|
|
return (
|
|
|
|
<Listbox
|
|
|
|
as="div"
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
className={`relative flex-shrink-0 text-left ${className}`}
|
|
|
|
disabled={disabled}
|
2023-01-26 18:12:20 +00:00
|
|
|
>
|
2023-10-11 06:35:53 +00:00
|
|
|
<>
|
|
|
|
{customButton ? (
|
|
|
|
<Listbox.Button as={React.Fragment}>
|
|
|
|
<button
|
|
|
|
ref={setReferenceElement}
|
|
|
|
type="button"
|
|
|
|
className={`flex items-center justify-between gap-1 w-full text-xs ${
|
|
|
|
disabled
|
|
|
|
? "cursor-not-allowed text-custom-text-200"
|
|
|
|
: "cursor-pointer hover:bg-custom-background-80"
|
|
|
|
} ${customButtonClassName}`}
|
|
|
|
>
|
|
|
|
{customButton}
|
|
|
|
</button>
|
|
|
|
</Listbox.Button>
|
|
|
|
) : (
|
|
|
|
<Listbox.Button as={React.Fragment}>
|
|
|
|
<button
|
|
|
|
ref={setReferenceElement}
|
|
|
|
type="button"
|
|
|
|
className={`flex items-center justify-between gap-1 w-full rounded-md border border-custom-border-300 shadow-sm duration-300 focus:outline-none ${
|
|
|
|
input ? "px-3 py-2 text-sm" : "px-2.5 py-1 text-xs"
|
|
|
|
} ${
|
|
|
|
disabled
|
|
|
|
? "cursor-not-allowed text-custom-text-200"
|
|
|
|
: "cursor-pointer hover:bg-custom-background-80"
|
|
|
|
} ${buttonClassName}`}
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
{!noChevron && !disabled && (
|
|
|
|
<ChevronDownIcon className="h-3 w-3" aria-hidden="true" />
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</Listbox.Button>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
<Listbox.Options>
|
|
|
|
<div
|
|
|
|
className={`z-10 border border-custom-border-300 overflow-y-auto rounded-md bg-custom-background-90 text-xs shadow-custom-shadow-rg focus:outline-none my-1 ${
|
|
|
|
maxHeight === "lg"
|
|
|
|
? "max-h-60"
|
|
|
|
: maxHeight === "md"
|
|
|
|
? "max-h-48"
|
|
|
|
: maxHeight === "rg"
|
|
|
|
? "max-h-36"
|
|
|
|
: maxHeight === "sm"
|
|
|
|
? "max-h-28"
|
|
|
|
: ""
|
|
|
|
} ${width === "auto" ? "min-w-[8rem] whitespace-nowrap" : width} ${optionsClassName}`}
|
|
|
|
ref={setPopperElement}
|
|
|
|
style={styles.popper}
|
|
|
|
{...attributes.popper}
|
|
|
|
>
|
|
|
|
<div className="space-y-1 p-2">{children}</div>
|
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
</Listbox.Options>
|
2023-10-11 06:35:53 +00:00
|
|
|
</Listbox>
|
|
|
|
);
|
|
|
|
};
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
type OptionProps = {
|
2023-07-23 16:44:26 +00:00
|
|
|
children: React.ReactNode;
|
2023-01-26 18:12:20 +00:00
|
|
|
value: any;
|
|
|
|
className?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Option: React.FC<OptionProps> = ({ children, value, className }) => (
|
|
|
|
<Listbox.Option
|
|
|
|
value={value}
|
|
|
|
className={({ active, selected }) =>
|
2023-07-17 12:05:47 +00:00
|
|
|
`cursor-pointer select-none truncate rounded px-1 py-1.5 ${
|
|
|
|
active || selected ? "bg-custom-background-80" : ""
|
|
|
|
} ${selected ? "text-custom-text-100" : "text-custom-text-200"} ${className}`
|
2023-01-26 18:12:20 +00:00
|
|
|
}
|
|
|
|
>
|
2023-03-04 12:17:03 +00:00
|
|
|
{({ selected }) => (
|
2023-03-05 14:52:01 +00:00
|
|
|
<div className="flex items-center justify-between gap-2">
|
|
|
|
<div className="flex items-center gap-2">{children}</div>
|
|
|
|
{selected && <CheckIcon className="h-4 w-4 flex-shrink-0" />}
|
|
|
|
</div>
|
2023-03-04 12:17:03 +00:00
|
|
|
)}
|
2023-01-26 18:12:20 +00:00
|
|
|
</Listbox.Option>
|
|
|
|
);
|
|
|
|
|
|
|
|
CustomSelect.Option = Option;
|
|
|
|
|
|
|
|
export { CustomSelect };
|