2022-11-19 14:21:26 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
// headless ui
|
|
|
|
import { Transition, Combobox } from "@headlessui/react";
|
|
|
|
// common
|
|
|
|
import { classNames } from "constants/common";
|
|
|
|
// types
|
|
|
|
import type { Props } from "./types";
|
|
|
|
|
|
|
|
const SearchListbox: React.FC<Props> = ({
|
2022-11-23 15:10:19 +00:00
|
|
|
title,
|
2022-11-19 14:21:26 +00:00
|
|
|
options,
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
multiple: canSelectMultiple,
|
2022-11-23 15:10:19 +00:00
|
|
|
icon,
|
2022-11-29 14:19:39 +00:00
|
|
|
width = "sm",
|
2022-11-23 15:10:19 +00:00
|
|
|
optionsFontsize,
|
|
|
|
buttonClassName,
|
|
|
|
optionsClassName,
|
2022-11-19 14:21:26 +00:00
|
|
|
}) => {
|
|
|
|
const [query, setQuery] = useState("");
|
|
|
|
|
|
|
|
const filteredOptions =
|
|
|
|
query === ""
|
|
|
|
? options
|
2022-11-23 15:10:19 +00:00
|
|
|
: options?.filter((option) => option.display.toLowerCase().includes(query.toLowerCase()));
|
2022-11-19 14:21:26 +00:00
|
|
|
|
|
|
|
const props: any = {
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (canSelectMultiple) {
|
|
|
|
props.value = props.value ?? [];
|
|
|
|
props.onChange = (value: string[]) => {
|
|
|
|
onChange(value);
|
|
|
|
};
|
|
|
|
props.multiple = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-12-01 14:29:21 +00:00
|
|
|
<Combobox as="div" {...props} className="relative flex-shrink-0">
|
2022-11-23 15:10:19 +00:00
|
|
|
{({ open }: any) => (
|
|
|
|
<>
|
2022-12-01 14:29:21 +00:00
|
|
|
<Combobox.Label className="sr-only">{title}</Combobox.Label>
|
|
|
|
<Combobox.Button
|
|
|
|
className={`flex items-center gap-1 hover:bg-gray-100 border rounded-md shadow-sm px-2 py-1 cursor-pointer focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm duration-300 ${
|
2022-12-03 13:41:07 +00:00
|
|
|
buttonClassName || ""
|
|
|
|
}`}
|
2022-12-01 14:29:21 +00:00
|
|
|
>
|
|
|
|
{icon ?? null}
|
|
|
|
<span
|
|
|
|
className={classNames(
|
|
|
|
value === null || value === undefined ? "" : "text-gray-900",
|
2022-12-03 13:41:07 +00:00
|
|
|
"hidden truncate sm:block"
|
2022-12-01 14:29:21 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
{Array.isArray(value)
|
|
|
|
? value
|
|
|
|
.map((v) => options?.find((option) => option.value === v)?.display)
|
|
|
|
.join(", ") || title
|
|
|
|
: options?.find((option) => option.value === value)?.display || title}
|
|
|
|
</span>
|
|
|
|
</Combobox.Button>
|
|
|
|
|
|
|
|
<Transition
|
|
|
|
show={open}
|
|
|
|
as={React.Fragment}
|
|
|
|
leave="transition ease-in duration-100"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
|
|
|
<Combobox.Options
|
|
|
|
className={`absolute z-10 mt-1 bg-white shadow-lg rounded-md py-1 ring-1 ring-black ring-opacity-5 focus:outline-none max-h-32 overflow-auto ${
|
|
|
|
width === "xs"
|
|
|
|
? "w-20"
|
|
|
|
: width === "sm"
|
2022-11-23 15:10:19 +00:00
|
|
|
? "w-32"
|
|
|
|
: width === "md"
|
|
|
|
? "w-48"
|
|
|
|
: width === "lg"
|
|
|
|
? "w-64"
|
|
|
|
: width === "xl"
|
|
|
|
? "w-80"
|
|
|
|
: width === "2xl"
|
|
|
|
? "w-96"
|
|
|
|
: ""
|
2022-12-01 14:29:21 +00:00
|
|
|
}} ${
|
|
|
|
optionsFontsize === "sm"
|
|
|
|
? "text-xs"
|
|
|
|
: optionsFontsize === "md"
|
|
|
|
? "text-base"
|
|
|
|
: optionsFontsize === "lg"
|
|
|
|
? "text-lg"
|
|
|
|
: optionsFontsize === "xl"
|
|
|
|
? "text-xl"
|
|
|
|
: optionsFontsize === "2xl"
|
|
|
|
? "text-2xl"
|
|
|
|
: ""
|
|
|
|
} ${optionsClassName || ""}`}
|
2022-11-23 15:10:19 +00:00
|
|
|
>
|
2022-12-01 14:29:21 +00:00
|
|
|
<Combobox.Input
|
|
|
|
className="w-full bg-transparent border-b p-2 mb-1 focus:outline-none sm:text-sm"
|
|
|
|
onChange={(event) => setQuery(event.target.value)}
|
|
|
|
placeholder="Search"
|
|
|
|
displayValue={(assigned: any) => assigned?.name}
|
|
|
|
/>
|
|
|
|
<div className="p-1">
|
|
|
|
{filteredOptions ? (
|
|
|
|
filteredOptions.length > 0 ? (
|
|
|
|
filteredOptions.map((option) => (
|
|
|
|
<Combobox.Option
|
|
|
|
key={option.value}
|
|
|
|
className={({ active }) =>
|
|
|
|
`${
|
|
|
|
active ? "text-white bg-theme" : "text-gray-900"
|
|
|
|
} cursor-pointer select-none truncate font-medium relative p-2 rounded-md`
|
|
|
|
}
|
|
|
|
value={option.value}
|
|
|
|
>
|
|
|
|
{option.element ?? option.display}
|
|
|
|
</Combobox.Option>
|
|
|
|
))
|
2022-11-19 14:21:26 +00:00
|
|
|
) : (
|
2022-12-01 14:29:21 +00:00
|
|
|
<p className="text-sm text-gray-500">No {title.toLowerCase()} found</p>
|
|
|
|
)
|
|
|
|
) : (
|
|
|
|
<p className="text-sm text-gray-500">Loading...</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Combobox.Options>
|
|
|
|
</Transition>
|
2022-11-23 15:10:19 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Combobox>
|
2022-11-19 14:21:26 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SearchListbox;
|