mango-v4-ui/components/forms/MultiSelectDropdown.tsx

85 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-09-29 20:22:55 -07:00
import { Fragment } from 'react'
import { ChevronDownIcon } from '@heroicons/react/20/solid'
import { useTranslation } from 'next-i18next'
import { Popover, Transition } from '@headlessui/react'
import Checkbox from './Checkbox'
const MultiSelectDropdown = ({
options,
selected,
toggleOption,
2023-01-04 04:10:36 -08:00
placeholder,
2022-09-29 20:22:55 -07:00
}: {
options: string[]
selected: string[]
toggleOption: (x: string) => void
2023-01-04 04:10:36 -08:00
placeholder?: string
2022-09-29 20:22:55 -07:00
}) => {
2023-01-04 04:10:36 -08:00
const { t } = useTranslation(['activity', 'common'])
2022-09-29 20:22:55 -07:00
return (
<Popover className="relative w-full min-w-[120px]">
{({ open }) => (
<div className="flex flex-col">
<Popover.Button
2022-12-07 18:34:24 -08:00
className={`default-transition h-12 rounded-md bg-th-input-bkg px-3 text-th-fgd-1 ring-1 ring-inset ring-th-input-border md:hover:ring-th-input-border-hover ${
open ? 'ring-th-input-border-hover' : 'ring-th-input-border'
2022-09-29 20:22:55 -07:00
}`}
>
<div className={`flex items-center justify-between`}>
{selected.length ? (
2023-01-12 02:19:08 -08:00
<span className="text-left">
2023-01-04 04:10:36 -08:00
{selected
.map((v) => t(v))
.toString()
.replace(/,/g, ', ')}
</span>
) : (
<span className="text-th-fgd-4">
2023-01-04 04:10:36 -08:00
{placeholder || t('common:select')}
</span>
)}
2022-09-29 20:22:55 -07:00
<ChevronDownIcon
className={`default-transition ml-0.5 h-6 w-6 ${
open ? 'rotate-180 transform' : 'rotate-360 transform'
}`}
aria-hidden="true"
/>
</div>
</Popover.Button>
<Transition
appear={true}
show={open}
as={Fragment}
enter="transition-all ease-in duration-200"
enterFrom="opacity-0 transform scale-75"
enterTo="opacity-100 transform scale-100"
leave="transition ease-out duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
2022-12-05 14:13:49 -08:00
<Popover.Panel className="absolute top-14 z-10 h-72 max-h-60 w-full overflow-y-auto rounded-md">
<div className="relative space-y-2.5 bg-th-bkg-3 p-3">
2022-09-29 20:22:55 -07:00
{options.map((option: string) => {
const isSelected = selected.includes(option)
return (
<Checkbox
labelClass="text-sm"
checked={isSelected}
key={option}
onChange={() => toggleOption(option)}
>
2023-01-04 04:10:36 -08:00
{t(option)}
2022-09-29 20:22:55 -07:00
</Checkbox>
)
})}
</div>
</Popover.Panel>
</Transition>
</div>
)}
</Popover>
2022-12-05 14:13:49 -08:00
)
2022-09-29 20:22:55 -07:00
}
export default MultiSelectDropdown