mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-16 02:05:04 +01:00
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
import React, { useState, useRef } from 'react';
|
|
import classNames from 'classnames';
|
|
import Menu from 'components/common/Menu';
|
|
import Button from 'components/common/Button';
|
|
import useDocumentClick from 'hooks/useDocumentClick';
|
|
import styles from './MenuButton.module.css';
|
|
|
|
export default function MenuButton({
|
|
icon,
|
|
value,
|
|
options,
|
|
menuClassname,
|
|
menuPosition = 'bottom',
|
|
menuAlign = 'right',
|
|
onSelect,
|
|
renderValue,
|
|
}) {
|
|
const [showMenu, setShowMenu] = useState(false);
|
|
const ref = useRef();
|
|
const selectedOption = options.find(e => e.value === value);
|
|
|
|
function handleSelect(value) {
|
|
onSelect(value);
|
|
setShowMenu(false);
|
|
}
|
|
|
|
function toggleMenu() {
|
|
setShowMenu(state => !state);
|
|
}
|
|
|
|
useDocumentClick(e => {
|
|
if (!ref.current.contains(e.target)) {
|
|
setShowMenu(false);
|
|
}
|
|
});
|
|
|
|
return (
|
|
<div className={styles.container} ref={ref}>
|
|
<Button
|
|
icon={icon}
|
|
className={classNames(styles.button, { [styles.open]: showMenu })}
|
|
onClick={toggleMenu}
|
|
variant="light"
|
|
>
|
|
<div className={styles.text}>{renderValue ? renderValue(selectedOption) : value}</div>
|
|
</Button>
|
|
{showMenu && (
|
|
<Menu
|
|
className={menuClassname}
|
|
options={options}
|
|
selectedOption={selectedOption}
|
|
onSelect={handleSelect}
|
|
float={menuPosition}
|
|
align={menuAlign}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|