@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
106 lines (104 loc) • 3.58 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Button from '@mui/material/Button';
import React, { useRef, useState } from 'react';
import Menu from '@mui/material/Menu';
import { ListItemText } from '@mui/material';
import { CheckRounded, KeyboardArrowDownRounded } from '@mui/icons-material';
import { UNDEFINED } from '../../utils/constants';
import ListItemButton from '@mui/material/ListItemButton';
import ListItem from '@mui/material/ListItem';
export function DropDownMenu(props) {
const {
options,
onMenuItemClick: onMenuItemClickProp,
onClick,
closeOnSelection = true,
menuProps,
listItemProps,
listItemTextProps,
listItemButtonProps,
...buttonProps
} = props;
const buttonRef = useRef();
const [open, setOpen] = useState(false);
const onClose = () => setOpen(false);
const onMenuItemClick = (e, option) => {
closeOnSelection && onClose();
onMenuItemClickProp?.(e, option.id);
};
return React.createElement(
React.Fragment,
null,
React.createElement(Button, {
endIcon: React.createElement(KeyboardArrowDownRounded, null),
...buttonProps,
ref: buttonRef,
onClick: (e) => {
// @ts-ignore
if (onClick?.(e) !== false) {
setOpen(true);
}
}
}),
React.createElement(
Menu,
{ ...menuProps, open: open, anchorEl: buttonRef.current, onClose: onClose },
options?.map((option) =>
React.createElement(
ListItem,
{
key: option.id,
disablePadding: true,
secondaryAction: option.selected ? React.createElement(CheckRounded, null) : UNDEFINED,
...listItemProps
},
React.createElement(
ListItemButton,
{
dense: true,
...listItemButtonProps,
selected: option.selected,
disabled: option.disabled,
onClick: (e) => onMenuItemClick(e, option)
},
React.createElement(ListItemText, {
primary: option.primaryText,
secondary: option.secondaryText,
...listItemTextProps
})
)
)
)
)
);
}
export default DropDownMenu;