umami/components/common/Menu.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-08-06 08:03:07 +02:00
import React from 'react';
2021-02-16 13:23:16 +01:00
import PropTypes from 'prop-types';
2020-08-06 08:03:07 +02:00
import classNames from 'classnames';
import styles from './Menu.module.css';
2021-02-16 13:23:16 +01:00
function Menu({
2020-08-10 00:13:38 +02:00
options = [],
selectedOption,
className,
float,
align = 'left',
optionClassName,
selectedClassName,
onSelect = () => {},
}) {
2020-08-06 08:03:07 +02:00
return (
<div
className={classNames(styles.menu, className, {
2020-08-10 00:13:38 +02:00
[styles.float]: float,
[styles.top]: float === 'top',
[styles.bottom]: float === 'bottom',
2020-08-06 08:03:07 +02:00
[styles.left]: align === 'left',
[styles.right]: align === 'right',
})}
>
{options
.filter(({ hidden }) => !hidden)
.map(option => {
const { label, value, className: customClassName, render, divider } = option;
2020-08-10 00:13:38 +02:00
return render ? (
render(option)
) : (
<div
key={value}
className={classNames(styles.option, optionClassName, customClassName, {
2020-09-21 06:31:53 +02:00
[selectedClassName]: selectedOption === option,
[styles.selected]: selectedOption === option,
[styles.divider]: divider,
})}
onClick={e => onSelect(value, e)}
>
{label}
</div>
);
})}
2020-08-06 08:03:07 +02:00
</div>
);
}
2021-02-16 13:23:16 +01:00
Menu.propTypes = {
options: PropTypes.arrayOf(
PropTypes.shape({
label: PropTypes.node,
value: PropTypes.any,
className: PropTypes.string,
render: PropTypes.func,
divider: PropTypes.bool,
}),
),
selectedOption: PropTypes.any,
className: PropTypes.string,
float: PropTypes.oneOf(['top', 'bottom']),
align: PropTypes.oneOf(['left', 'right']),
optionClassName: PropTypes.string,
selectedClassName: PropTypes.string,
onSelect: PropTypes.func,
};
export default Menu;