2022-12-12 13:30:34 +01:00
|
|
|
import { h } from 'preact';
|
|
|
|
import { useRef, useState } from 'preact/hooks';
|
|
|
|
import Menu from './Menu';
|
|
|
|
import { ArrowDropdown } from '../icons/ArrowDropdown';
|
|
|
|
import Heading from './Heading';
|
2022-12-18 00:54:54 +01:00
|
|
|
import Button from './Button';
|
|
|
|
import CameraIcon from '../icons/Camera';
|
2022-12-12 13:30:34 +01:00
|
|
|
|
2022-12-18 00:54:54 +01:00
|
|
|
export default function MultiSelect({ className, title, options, selection, onToggle, onShowAll, onSelectSingle }) {
|
2022-12-12 13:30:34 +01:00
|
|
|
const popupRef = useRef(null);
|
|
|
|
|
|
|
|
const [state, setState] = useState({
|
|
|
|
showMenu: false,
|
|
|
|
});
|
2023-03-04 00:45:25 +01:00
|
|
|
|
2023-04-14 14:14:28 +02:00
|
|
|
const isOptionSelected = (item) => {
|
|
|
|
return selection == 'all' || selection.split(',').indexOf(item) > -1;
|
|
|
|
};
|
2023-03-04 00:45:25 +01:00
|
|
|
|
|
|
|
const menuHeight = Math.round(window.innerHeight * 0.55);
|
|
|
|
|
2022-12-12 13:30:34 +01:00
|
|
|
return (
|
|
|
|
<div className={`${className} p-2`} ref={popupRef}>
|
2023-04-14 14:14:28 +02:00
|
|
|
<div className="flex justify-between min-w-[120px]" onClick={() => setState({ showMenu: true })}>
|
2022-12-12 13:30:34 +01:00
|
|
|
<label>{title}</label>
|
|
|
|
<ArrowDropdown className="w-6" />
|
|
|
|
</div>
|
|
|
|
{state.showMenu ? (
|
2023-04-14 14:14:28 +02:00
|
|
|
<Menu
|
|
|
|
className={`max-h-[${menuHeight}px] overflow-auto`}
|
|
|
|
relativeTo={popupRef}
|
|
|
|
onDismiss={() => setState({ showMenu: false })}
|
|
|
|
>
|
2022-12-18 00:54:54 +01:00
|
|
|
<div className="flex flex-wrap justify-between items-center">
|
2023-04-14 14:14:28 +02:00
|
|
|
<Heading className="p-4 justify-center" size="md">
|
|
|
|
{title}
|
|
|
|
</Heading>
|
|
|
|
<Button tabindex="false" className="mx-4" onClick={() => onShowAll()}>
|
2022-12-18 00:54:54 +01:00
|
|
|
Show All
|
|
|
|
</Button>
|
|
|
|
</div>
|
2022-12-12 13:30:34 +01:00
|
|
|
{options.map((item) => (
|
2022-12-18 00:54:54 +01:00
|
|
|
<div className="flex flex-grow" key={item}>
|
|
|
|
<label
|
2023-04-14 14:14:28 +02:00
|
|
|
className={`flex flex-shrink space-x-2 p-1 my-1 min-w-[176px] hover:bg-gray-200 dark:hover:bg-gray-800 dark:hover:text-white cursor-pointer capitalize text-sm`}
|
|
|
|
>
|
2022-12-18 00:54:54 +01:00
|
|
|
<input
|
|
|
|
className="mx-4 m-0 align-middle"
|
|
|
|
type="checkbox"
|
|
|
|
checked={isOptionSelected(item)}
|
2023-04-14 14:14:28 +02:00
|
|
|
onChange={() => onToggle(item)}
|
|
|
|
/>
|
|
|
|
{item.replaceAll('_', ' ')}
|
2022-12-18 00:54:54 +01:00
|
|
|
</label>
|
|
|
|
<div className="justify-right">
|
2023-04-14 14:14:28 +02:00
|
|
|
<Button
|
|
|
|
color={isOptionSelected(item) ? 'blue' : 'black'}
|
|
|
|
type="text"
|
|
|
|
className="max-h-[35px] mx-2"
|
|
|
|
onClick={() => onSelectSingle(item)}
|
|
|
|
>
|
2022-12-18 00:54:54 +01:00
|
|
|
<CameraIcon />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-12-12 13:30:34 +01:00
|
|
|
))}
|
|
|
|
</Menu>
|
2023-04-14 14:14:28 +02:00
|
|
|
) : null}
|
2022-12-12 13:30:34 +01:00
|
|
|
</div>
|
|
|
|
);
|
2022-12-18 00:54:54 +01:00
|
|
|
}
|