2022-03-13 14:34:32 +01:00
|
|
|
import React, { Fragment } from "react";
|
2022-03-06 18:31:05 +01:00
|
|
|
|
2022-03-13 14:34:32 +01:00
|
|
|
import { Listbox, Transition } from "@headlessui/react";
|
2022-12-13 23:50:13 +01:00
|
|
|
import { Icon, Icons } from "@/components/Icon";
|
2022-03-06 18:31:05 +01:00
|
|
|
|
|
|
|
export interface OptionItem {
|
2022-03-13 16:55:59 +01:00
|
|
|
id: string;
|
2022-03-06 18:31:05 +01:00
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DropdownProps {
|
2022-03-13 14:34:32 +01:00
|
|
|
selectedItem: OptionItem;
|
|
|
|
setSelectedItem: (value: OptionItem) => void;
|
2022-03-06 18:31:05 +01:00
|
|
|
options: Array<OptionItem>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Dropdown = React.forwardRef<HTMLDivElement, DropdownProps>(
|
2022-03-13 14:34:32 +01:00
|
|
|
(props: DropdownProps) => (
|
2022-03-13 17:26:46 +01:00
|
|
|
<div className="relative my-4 max-w-[18rem]">
|
2022-03-13 14:34:32 +01:00
|
|
|
<Listbox value={props.selectedItem} onChange={props.setSelectedItem}>
|
|
|
|
{({ open }) => (
|
|
|
|
<>
|
2022-12-13 23:50:13 +01:00
|
|
|
<Listbox.Button className="relative w-full cursor-default rounded-lg bg-denim-500 py-2 pl-3 pr-10 text-left text-white shadow-md focus:outline-none focus-visible:border-indigo-500 focus-visible:ring-2 focus-visible:ring-bink-500 focus-visible:ring-opacity-75 focus-visible:ring-offset-2 focus-visible:ring-offset-bink-300 sm:text-sm">
|
2022-03-13 14:34:32 +01:00
|
|
|
<span className="block truncate">{props.selectedItem.name}</span>
|
|
|
|
<span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
|
|
|
|
<Icon
|
|
|
|
icon={Icons.CHEVRON_DOWN}
|
|
|
|
className={`transform transition-transform ${
|
|
|
|
open ? "rotate-180" : ""
|
|
|
|
}`}
|
2022-03-06 18:31:05 +01:00
|
|
|
/>
|
2022-03-13 14:34:32 +01:00
|
|
|
</span>
|
|
|
|
</Listbox.Button>
|
|
|
|
<Transition
|
|
|
|
as={Fragment}
|
|
|
|
leave="transition ease-in duration-100"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
2022-12-13 23:50:13 +01:00
|
|
|
<Listbox.Options className="absolute bottom-11 left-0 right-0 z-10 mt-1 max-h-60 overflow-auto rounded-md bg-denim-500 py-1 text-white shadow-lg ring-1 ring-black ring-opacity-5 scrollbar-thin scrollbar-track-denim-400 scrollbar-thumb-denim-200 focus:outline-none sm:bottom-10 sm:text-sm">
|
2022-03-13 14:34:32 +01:00
|
|
|
{props.options.map((opt) => (
|
|
|
|
<Listbox.Option
|
|
|
|
className={({ active }) =>
|
|
|
|
`relative cursor-default select-none py-2 pl-10 pr-4 ${
|
|
|
|
active ? "bg-denim-400 text-bink-700" : "text-white"
|
|
|
|
}`
|
|
|
|
}
|
|
|
|
key={opt.id}
|
|
|
|
value={opt}
|
|
|
|
>
|
|
|
|
{opt.name}
|
|
|
|
</Listbox.Option>
|
|
|
|
))}
|
|
|
|
</Listbox.Options>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Listbox>
|
|
|
|
</div>
|
|
|
|
)
|
2022-03-06 18:31:05 +01:00
|
|
|
);
|