2023-03-04 13:40:35 +00:00
|
|
|
import React, { useState } from "react";
|
2023-05-18 13:37:24 +00:00
|
|
|
|
2023-03-04 13:40:35 +00:00
|
|
|
// headless ui
|
|
|
|
import { Combobox, Transition } from "@headlessui/react";
|
|
|
|
// icons
|
2023-05-18 13:37:24 +00:00
|
|
|
import { ChevronDownIcon } from "@heroicons/react/20/solid";
|
|
|
|
import { CheckIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline";
|
|
|
|
|
2023-03-04 13:40:35 +00:00
|
|
|
type CustomSearchSelectProps = {
|
|
|
|
value: any;
|
|
|
|
onChange: any;
|
2023-05-20 10:30:41 +00:00
|
|
|
options:
|
|
|
|
| {
|
|
|
|
value: any;
|
|
|
|
query: string;
|
|
|
|
content: JSX.Element;
|
|
|
|
}[]
|
|
|
|
| undefined;
|
2023-03-04 13:40:35 +00:00
|
|
|
label?: string | JSX.Element;
|
|
|
|
textAlignment?: "left" | "center" | "right";
|
2023-03-07 14:57:16 +00:00
|
|
|
height?: "sm" | "md" | "rg" | "lg";
|
2023-03-04 13:40:35 +00:00
|
|
|
position?: "right" | "left";
|
2023-05-10 20:45:39 +00:00
|
|
|
verticalPosition?: "top" | "bottom";
|
2023-03-04 13:40:35 +00:00
|
|
|
noChevron?: boolean;
|
|
|
|
customButton?: JSX.Element;
|
2023-07-07 08:41:33 +00:00
|
|
|
className?: string;
|
2023-03-04 13:40:35 +00:00
|
|
|
optionsClassName?: string;
|
2023-04-19 10:10:31 +00:00
|
|
|
input?: boolean;
|
2023-03-04 13:40:35 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
selfPositioned?: boolean;
|
|
|
|
multiple?: boolean;
|
|
|
|
footerOption?: JSX.Element;
|
2023-06-30 12:58:12 +00:00
|
|
|
noResultIcon?: JSX.Element;
|
2023-05-21 13:38:28 +00:00
|
|
|
dropdownWidth?: string;
|
2023-03-04 13:40:35 +00:00
|
|
|
};
|
|
|
|
export const CustomSearchSelect = ({
|
|
|
|
label,
|
|
|
|
textAlignment,
|
2023-03-07 16:51:50 +00:00
|
|
|
height = "md",
|
2023-03-04 13:40:35 +00:00
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
options,
|
|
|
|
position = "left",
|
2023-05-10 20:45:39 +00:00
|
|
|
verticalPosition = "bottom",
|
2023-03-04 13:40:35 +00:00
|
|
|
noChevron = false,
|
|
|
|
customButton,
|
2023-07-07 08:41:33 +00:00
|
|
|
className = "",
|
2023-03-04 13:40:35 +00:00
|
|
|
optionsClassName = "",
|
2023-04-19 10:10:31 +00:00
|
|
|
input = false,
|
2023-03-04 13:40:35 +00:00
|
|
|
disabled = false,
|
|
|
|
selfPositioned = false,
|
|
|
|
multiple = false,
|
2023-06-30 12:58:12 +00:00
|
|
|
noResultIcon,
|
2023-03-04 13:40:35 +00:00
|
|
|
footerOption,
|
2023-05-21 13:38:28 +00:00
|
|
|
dropdownWidth,
|
2023-03-04 13:40:35 +00:00
|
|
|
}: CustomSearchSelectProps) => {
|
|
|
|
const [query, setQuery] = useState("");
|
|
|
|
|
|
|
|
const filteredOptions =
|
|
|
|
query === ""
|
|
|
|
? options
|
|
|
|
: options?.filter((option) => option.query.toLowerCase().includes(query.toLowerCase()));
|
|
|
|
|
2023-03-15 05:29:43 +00:00
|
|
|
const props: any = {
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
disabled,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (multiple) props.multiple = true;
|
|
|
|
|
2023-03-04 13:40:35 +00:00
|
|
|
return (
|
2023-03-15 05:29:43 +00:00
|
|
|
<Combobox
|
|
|
|
as="div"
|
2023-07-07 08:41:33 +00:00
|
|
|
className={`${!selfPositioned ? "relative" : ""} flex-shrink-0 text-left ${className}`}
|
2023-03-15 05:29:43 +00:00
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{({ open }: any) => (
|
|
|
|
<>
|
|
|
|
{customButton ? (
|
|
|
|
<Combobox.Button as="div">{customButton}</Combobox.Button>
|
|
|
|
) : (
|
|
|
|
<Combobox.Button
|
2023-07-17 10:58:23 +00:00
|
|
|
className={`flex w-full border border-custom-border-200 ${
|
2023-07-10 07:17:00 +00:00
|
|
|
disabled ? "cursor-not-allowed" : "cursor-pointer hover:bg-custom-background-80"
|
2023-04-19 10:10:31 +00:00
|
|
|
} ${
|
2023-04-20 12:43:21 +00:00
|
|
|
input ? "px-3 py-2 text-sm" : "px-2.5 py-1 text-xs"
|
2023-07-12 14:25:08 +00:00
|
|
|
} items-center justify-between gap-1 rounded-md shadow-sm duration-300 focus:outline-none focus:ring-1 focus:ring-custom-border-100 ${
|
2023-03-15 05:29:43 +00:00
|
|
|
textAlignment === "right"
|
|
|
|
? "text-right"
|
|
|
|
: textAlignment === "center"
|
|
|
|
? "text-center"
|
|
|
|
: "text-left"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
{!noChevron && !disabled && (
|
|
|
|
<ChevronDownIcon className="h-3 w-3" aria-hidden="true" />
|
2023-03-04 13:40:35 +00:00
|
|
|
)}
|
2023-03-15 05:29:43 +00:00
|
|
|
</Combobox.Button>
|
2023-03-04 13:40:35 +00:00
|
|
|
)}
|
2023-03-15 05:29:43 +00:00
|
|
|
<Transition
|
|
|
|
show={open}
|
|
|
|
as={React.Fragment}
|
|
|
|
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"
|
|
|
|
>
|
|
|
|
<Combobox.Options
|
2023-07-17 10:58:23 +00:00
|
|
|
className={`${optionsClassName} absolute min-w-[10rem] border border-custom-border-200 p-2 ${
|
2023-03-15 05:29:43 +00:00
|
|
|
position === "right" ? "right-0" : "left-0"
|
2023-05-10 20:45:39 +00:00
|
|
|
} ${
|
|
|
|
verticalPosition === "top" ? "bottom-full mb-1" : "mt-1"
|
2023-07-10 07:17:00 +00:00
|
|
|
} z-10 origin-top-right rounded-md bg-custom-background-90 text-xs shadow-lg focus:outline-none ${
|
2023-05-21 13:38:28 +00:00
|
|
|
dropdownWidth ? dropdownWidth : ``
|
|
|
|
} `}
|
2023-03-15 05:29:43 +00:00
|
|
|
>
|
2023-07-17 10:58:23 +00:00
|
|
|
<div className="flex w-full items-center justify-start rounded-sm border-[0.6px] border-custom-border-200 bg-custom-background-90 px-2">
|
2023-07-10 07:17:00 +00:00
|
|
|
<MagnifyingGlassIcon className="h-3 w-3 text-custom-text-200" />
|
2023-03-15 05:29:43 +00:00
|
|
|
<Combobox.Input
|
2023-07-10 07:17:00 +00:00
|
|
|
className="w-full bg-transparent py-1 px-2 text-xs text-custom-text-200 focus:outline-none"
|
2023-03-15 05:29:43 +00:00
|
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
|
|
placeholder="Type to search..."
|
|
|
|
displayValue={(assigned: any) => assigned?.name}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={`mt-2 space-y-1 ${
|
|
|
|
height === "sm"
|
|
|
|
? "max-h-28"
|
|
|
|
: height === "md"
|
|
|
|
? "max-h-44"
|
|
|
|
: height === "rg"
|
|
|
|
? "max-h-56"
|
|
|
|
: height === "lg"
|
|
|
|
? "max-h-80"
|
|
|
|
: ""
|
|
|
|
} overflow-y-scroll`}
|
2023-03-04 13:40:35 +00:00
|
|
|
>
|
2023-03-15 05:29:43 +00:00
|
|
|
{filteredOptions ? (
|
|
|
|
filteredOptions.length > 0 ? (
|
|
|
|
filteredOptions.map((option) => (
|
|
|
|
<Combobox.Option
|
|
|
|
key={option.value}
|
|
|
|
value={option.value}
|
|
|
|
className={({ active, selected }) =>
|
2023-07-10 07:17:00 +00:00
|
|
|
`${active || selected ? "bg-custom-background-80" : ""} ${
|
2023-07-11 09:48:47 +00:00
|
|
|
selected ? "text-custom-text-100" : "text-custom-text-200"
|
|
|
|
} flex cursor-pointer select-none items-center justify-between gap-2 truncate rounded px-1 py-1.5`
|
2023-03-15 05:29:43 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
{({ active, selected }) => (
|
|
|
|
<>
|
|
|
|
{option.content}
|
2023-04-05 19:21:15 +00:00
|
|
|
{multiple ? (
|
|
|
|
<div
|
2023-07-11 09:48:47 +00:00
|
|
|
className={`flex items-center justify-center rounded border border-custom-border-400 p-0.5 ${
|
2023-04-05 19:21:15 +00:00
|
|
|
active || selected ? "opacity-100" : "opacity-0"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<CheckIcon
|
|
|
|
className={`h-3 w-3 ${selected ? "opacity-100" : "opacity-0"}`}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
2023-03-15 05:29:43 +00:00
|
|
|
<CheckIcon
|
|
|
|
className={`h-3 w-3 ${selected ? "opacity-100" : "opacity-0"}`}
|
|
|
|
/>
|
2023-04-05 19:21:15 +00:00
|
|
|
)}
|
2023-03-15 05:29:43 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Combobox.Option>
|
|
|
|
))
|
|
|
|
) : (
|
2023-06-30 12:58:12 +00:00
|
|
|
<span className="flex items-center gap-2 p-1">
|
|
|
|
{noResultIcon && noResultIcon}
|
2023-07-10 07:17:00 +00:00
|
|
|
<p className="text-left text-custom-text-200 ">No matching results</p>
|
2023-06-30 12:58:12 +00:00
|
|
|
</span>
|
2023-03-15 05:29:43 +00:00
|
|
|
)
|
|
|
|
) : (
|
2023-07-10 07:17:00 +00:00
|
|
|
<p className="text-center text-custom-text-200">Loading...</p>
|
2023-03-15 05:29:43 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
{footerOption}
|
|
|
|
</Combobox.Options>
|
|
|
|
</Transition>
|
|
|
|
</>
|
2023-03-04 13:40:35 +00:00
|
|
|
)}
|
2023-03-15 05:29:43 +00:00
|
|
|
</Combobox>
|
2023-03-04 13:40:35 +00:00
|
|
|
);
|
|
|
|
};
|