2022-02-13 18:49:03 +01:00
|
|
|
import React, {
|
|
|
|
MouseEventHandler,
|
|
|
|
SyntheticEvent,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
} from "react";
|
2022-12-13 23:50:13 +01:00
|
|
|
import { Icon, Icons } from "@/components/Icon";
|
2022-02-13 18:49:03 +01:00
|
|
|
|
2023-01-07 23:44:46 +01:00
|
|
|
import { BackdropContainer, useBackdrop } from "@/components/layout/Backdrop";
|
2022-03-06 13:43:32 +01:00
|
|
|
import { ButtonControlProps, ButtonControl } from "./ButtonControl";
|
2022-02-13 18:49:03 +01:00
|
|
|
|
2022-03-06 14:41:51 +01:00
|
|
|
export interface OptionItem {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
icon: Icons;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DropdownButtonProps extends ButtonControlProps {
|
2022-02-13 18:49:03 +01:00
|
|
|
icon: Icons;
|
|
|
|
open: boolean;
|
2022-02-13 19:29:25 +01:00
|
|
|
setOpen: (open: boolean) => void;
|
2022-02-13 18:49:03 +01:00
|
|
|
selectedItem: string;
|
2022-02-13 19:29:25 +01:00
|
|
|
setSelectedItem: (value: string) => void;
|
2022-02-13 18:49:03 +01:00
|
|
|
options: Array<OptionItem>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface OptionProps {
|
|
|
|
option: OptionItem;
|
|
|
|
onClick: MouseEventHandler<HTMLDivElement>;
|
|
|
|
tabIndex?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Option({ option, onClick, tabIndex }: OptionProps) {
|
|
|
|
return (
|
|
|
|
<div
|
2022-12-13 23:50:13 +01:00
|
|
|
className="flex h-10 cursor-pointer items-center space-x-2 px-4 py-2 text-left text-denim-700 transition-colors hover:text-white"
|
2022-02-13 18:49:03 +01:00
|
|
|
onClick={onClick}
|
|
|
|
tabIndex={tabIndex}
|
|
|
|
>
|
|
|
|
<Icon icon={option.icon} />
|
|
|
|
<input type="radio" className="hidden" id={option.id} />
|
|
|
|
<label htmlFor={option.id} className="cursor-pointer ">
|
|
|
|
<div className="item">{option.name}</div>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const DropdownButton = React.forwardRef<
|
|
|
|
HTMLDivElement,
|
|
|
|
DropdownButtonProps
|
2022-03-06 14:41:51 +01:00
|
|
|
>((props: DropdownButtonProps, ref) => {
|
2022-02-13 18:49:03 +01:00
|
|
|
const [setBackdrop, backdropProps, highlightedProps] = useBackdrop();
|
|
|
|
const [delayedSelectedId, setDelayedSelectedId] = useState(
|
|
|
|
props.selectedItem
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-01-07 21:36:18 +01:00
|
|
|
let id: ReturnType<typeof setTimeout>;
|
2022-02-13 18:49:03 +01:00
|
|
|
|
|
|
|
if (props.open) {
|
|
|
|
setDelayedSelectedId(props.selectedItem);
|
|
|
|
} else {
|
|
|
|
id = setTimeout(() => {
|
|
|
|
setDelayedSelectedId(props.selectedItem);
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
if (id) clearTimeout(id);
|
|
|
|
};
|
2022-02-13 19:29:25 +01:00
|
|
|
/* eslint-disable-next-line */
|
2022-02-13 18:49:03 +01:00
|
|
|
}, [props.open]);
|
|
|
|
|
|
|
|
const selectedItem: OptionItem = props.options.find(
|
|
|
|
(opt) => opt.id === props.selectedItem
|
|
|
|
) || { id: "movie", name: "movie", icon: Icons.ARROW_LEFT };
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setBackdrop(props.open);
|
2022-02-13 19:29:25 +01:00
|
|
|
/* eslint-disable-next-line */
|
2022-02-13 18:49:03 +01:00
|
|
|
}, [props.open]);
|
|
|
|
|
|
|
|
const onOptionClick = (e: SyntheticEvent, option: OptionItem) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
props.setSelectedItem(option.id);
|
|
|
|
props.setOpen(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-02-13 19:29:25 +01:00
|
|
|
<div className="w-full min-w-[140px] sm:w-auto">
|
2022-02-13 18:49:03 +01:00
|
|
|
<div
|
|
|
|
ref={ref}
|
|
|
|
className="relative w-full sm:w-auto"
|
|
|
|
{...highlightedProps}
|
|
|
|
>
|
2023-01-07 21:36:18 +01:00
|
|
|
<BackdropContainer
|
|
|
|
onClick={() => props.setOpen(false)}
|
|
|
|
{...backdropProps}
|
2022-02-13 18:49:03 +01:00
|
|
|
>
|
2023-01-07 21:36:18 +01:00
|
|
|
<ButtonControl
|
|
|
|
{...props}
|
|
|
|
className="sm:justify-left relative z-20 flex h-10 w-full items-center justify-center space-x-2 rounded-[20px] bg-bink-400 px-4 py-2 text-white hover:bg-bink-300"
|
|
|
|
>
|
|
|
|
<Icon icon={selectedItem.icon} />
|
|
|
|
<span className="flex-1">{selectedItem.name}</span>
|
|
|
|
<Icon
|
|
|
|
icon={Icons.CHEVRON_DOWN}
|
|
|
|
className={`transition-transform ${
|
|
|
|
props.open ? "rotate-180" : ""
|
|
|
|
}`}
|
|
|
|
/>
|
|
|
|
</ButtonControl>
|
|
|
|
<div
|
|
|
|
className={`absolute top-0 z-10 w-full rounded-[20px] bg-denim-300 pt-[40px] transition-all duration-200 ${
|
|
|
|
props.open
|
|
|
|
? "block max-h-60 opacity-100"
|
|
|
|
: "invisible max-h-0 opacity-0"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{props.options
|
|
|
|
.filter((opt) => opt.id !== delayedSelectedId)
|
|
|
|
.map((opt) => (
|
|
|
|
<Option
|
|
|
|
option={opt}
|
|
|
|
key={opt.id}
|
|
|
|
onClick={(e) => onOptionClick(e, opt)}
|
|
|
|
tabIndex={props.open ? 0 : undefined}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</BackdropContainer>
|
2022-02-13 18:49:03 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|