idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | import { |
| 19 | ListItemText, |
| 20 | ListItemTextProps, |
| 21 | MenuItem, |
| 22 | MenuItemProps, |
| 23 | MenuList, |
| 24 | MenuListProps, |
| 25 | MenuProps, |
| 26 | Stack, |
| 27 | styled, |
| 28 | SvgIconProps, |
| 29 | } from '@mui/material'; |
| 30 | import { ComponentType } from 'react'; |
| 31 | |
| 32 | export type PopoverListItemData = { |
| 33 | label: string; |
| 34 | Icon: ComponentType<SvgIconProps>; |
| 35 | onClick: () => void; |
| 36 | }; |
| 37 | |
| 38 | interface ListIconProps extends SvgIconProps { |
| 39 | Icon: ComponentType<SvgIconProps>; |
| 40 | } |
| 41 | |
| 42 | const ListIcon = styled(({ Icon, ...props }: ListIconProps) => <Icon {...props} />)(({ theme }) => ({ |
| 43 | height: '16px', |
| 44 | fontSize: '16px', |
| 45 | color: theme?.palette?.primary?.dark, |
| 46 | })); |
| 47 | |
| 48 | interface ListLabelProps extends ListItemTextProps { |
| 49 | label: string; |
| 50 | } |
| 51 | |
| 52 | const ListLabel = styled(({ label, ...props }: ListLabelProps) => ( |
| 53 | <ListItemText |
| 54 | {...props} |
| 55 | primary={label} |
| 56 | primaryTypographyProps={{ |
| 57 | fontSize: '12px', |
| 58 | lineHeight: '16px', |
| 59 | }} |
| 60 | /> |
| 61 | ))(() => ({ |
| 62 | height: '16px', |
| 63 | })); |
| 64 | |
| 65 | interface PopoverListItemProps extends MenuItemProps { |
| 66 | item: PopoverListItemData; |
| 67 | closeMenu?: MenuProps['onClose']; |
| 68 | } |
| 69 | |
| 70 | const PopoverListItem = styled(({ item, closeMenu, ...props }: PopoverListItemProps) => ( |
| 71 | <MenuItem |
| 72 | {...props} |
| 73 | onClick={() => { |
| 74 | item.onClick(); |
| 75 | closeMenu?.({}, 'backdropClick'); |
| 76 | }} |
| 77 | sx={{ |
| 78 | paddingY: '11px', |
| 79 | paddingX: '16px', |
| 80 | }} |
| 81 | > |
| 82 | <Stack direction="row" spacing="10px"> |
| 83 | <ListIcon Icon={item.Icon} /> |
| 84 | <ListLabel label={item.label} /> |
| 85 | </Stack> |
| 86 | </MenuItem> |
| 87 | ))(() => ({ |
| 88 | // Failed to modify the styles from here |
| 89 | })); |
| 90 | |
| 91 | interface PopoverListProps extends MenuListProps { |
| 92 | items: PopoverListItemData[]; |
| 93 | onClose?: MenuProps['onClose']; |
| 94 | } |
| 95 | |
| 96 | // A list intended to be used as a menu into a popover |
| 97 | const PopoverList = styled(({ items, onClose, ...props }: PopoverListProps) => ( |
| 98 | <MenuList {...props}> |
| 99 | {items.map((item, index) => ( |
| 100 | <PopoverListItem key={index} item={item} closeMenu={onClose} /> |
| 101 | ))} |
| 102 | </MenuList> |
| 103 | ))(() => ({ |
| 104 | padding: '3px 0px', |
| 105 | })); |
| 106 | |
| 107 | export default PopoverList; |