2022-07-20 21:50:56 -07:00
|
|
|
import { Listbox } from '@headlessui/react'
|
2022-09-06 21:36:35 -07:00
|
|
|
import { ChevronDownIcon } from '@heroicons/react/20/solid'
|
2022-07-20 21:50:56 -07:00
|
|
|
import { ReactNode } from 'react'
|
|
|
|
|
|
|
|
interface SelectProps {
|
|
|
|
value: string | ReactNode
|
|
|
|
onChange: (x: string) => void
|
|
|
|
children: ReactNode
|
|
|
|
className?: string
|
|
|
|
dropdownPanelClassName?: string
|
|
|
|
placeholder?: string
|
|
|
|
disabled?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
const Select = ({
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
dropdownPanelClassName,
|
|
|
|
placeholder = 'Select',
|
|
|
|
disabled = false,
|
|
|
|
}: SelectProps) => {
|
|
|
|
return (
|
|
|
|
<div className={`relative ${className}`}>
|
|
|
|
<Listbox value={value} onChange={onChange} disabled={disabled}>
|
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
<Listbox.Button
|
2022-12-07 18:34:24 -08:00
|
|
|
className={`default-transition h-full w-full rounded-md bg-th-input-bkg py-2.5 font-normal ring-1 ring-inset ring-th-input-border focus:border-th-input-border-hover focus:outline-none md:hover:ring-th-input-border-hover`}
|
2022-07-20 21:50:56 -07:00
|
|
|
>
|
|
|
|
<div
|
2022-11-30 21:21:28 -08:00
|
|
|
className={`flex items-center justify-between space-x-2 px-3 text-th-fgd-1`}
|
2022-07-20 21:50:56 -07:00
|
|
|
>
|
|
|
|
{value ? (
|
|
|
|
value
|
|
|
|
) : (
|
|
|
|
<span className="text-th-fgd-3">{placeholder}</span>
|
|
|
|
)}
|
|
|
|
<ChevronDownIcon
|
|
|
|
className={`default-transition h-5 w-5 flex-shrink-0 text-th-fgd-1 ${
|
2022-08-16 15:58:03 -07:00
|
|
|
open ? 'rotate-180' : 'rotate-360'
|
2022-07-20 21:50:56 -07:00
|
|
|
}`}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Listbox.Button>
|
|
|
|
{open ? (
|
|
|
|
<Listbox.Options
|
|
|
|
static
|
2022-12-16 03:44:21 -08:00
|
|
|
className={`thin-scroll absolute left-0 z-20 mt-1 max-h-60 w-full origin-top-left space-y-2 overflow-auto rounded-md bg-th-bkg-2 p-4 text-th-fgd-1 outline-none ${dropdownPanelClassName}`}
|
2022-07-20 21:50:56 -07:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Listbox.Options>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Listbox>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
interface OptionProps {
|
|
|
|
value: string
|
|
|
|
children: string | ReactNode
|
|
|
|
className?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const Option = ({ value, children, className }: OptionProps) => {
|
|
|
|
return (
|
|
|
|
<Listbox.Option className="mb-0" value={value}>
|
|
|
|
{({ selected }) => (
|
|
|
|
<div
|
2022-12-16 03:44:21 -08:00
|
|
|
className={`default-transition rounded text-th-fgd-2 hover:cursor-pointer md:hover:text-th-fgd-1 ${
|
2022-11-30 19:32:32 -08:00
|
|
|
selected ? 'text-th-active' : ''
|
2022-07-20 21:50:56 -07:00
|
|
|
} ${className}`}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Listbox.Option>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
Select.Option = Option
|
|
|
|
|
|
|
|
export default Select
|