|
| 1 | +import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown'; |
| 2 | +import { Theme } from '@mui/material'; |
| 3 | +import { MouseEvent, useState } from 'react'; |
| 4 | +import { Button, Menu, MenuItem } from '../../base'; |
| 5 | +import { iconSmall } from '../../constants/iconsSizes'; |
| 6 | +import { ALICE_BLUE, CHINESE_SILVER, NOT_FOUND, styled } from '../../theme'; |
| 7 | + |
| 8 | +interface VisibilityChipMenuProps { |
| 9 | + value: string; |
| 10 | + onChange: (value: string) => void; |
| 11 | + options: [string, React.ElementType][]; |
| 12 | + enabled: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +const StyledMenu = styled(Menu)(({ theme }) => ({ |
| 16 | + '& .MuiPaper-root': { |
| 17 | + backgroundColor: |
| 18 | + theme.palette.mode === 'light' ? ALICE_BLUE : theme.palette.background.constant?.table, |
| 19 | + color: theme.palette.text.secondary, |
| 20 | + border: `1px solid ${theme.palette.border.default}`, |
| 21 | + borderRadius: '0.25rem', |
| 22 | + padding: '0rem' |
| 23 | + }, |
| 24 | + '& .MuiMenuItem-root': { |
| 25 | + fontSize: '.9rem', |
| 26 | + padding: '0.5rem', |
| 27 | + '&:hover': { |
| 28 | + backgroundColor: theme.palette.mode === 'light' ? CHINESE_SILVER : 'rgba(0, 179, 159, 0.25)' |
| 29 | + } |
| 30 | + }, |
| 31 | + //selected |
| 32 | + '& .Mui-selected': { |
| 33 | + backgroundColor: theme.palette.mode === 'light' ? CHINESE_SILVER : 'rgba(0, 179, 159, 0.25)' |
| 34 | + }, |
| 35 | + '& .MuiList-padding': { |
| 36 | + padding: '0px' |
| 37 | + } |
| 38 | +})); |
| 39 | + |
| 40 | +const StyledButton = styled(Button)(() => ({ |
| 41 | + padding: '0px', |
| 42 | + width: '100%' |
| 43 | +})); |
| 44 | + |
| 45 | +const StyledDiv = styled('div')(({ theme, enabled }: { theme?: Theme; enabled: boolean }) => ({ |
| 46 | + paddingLeft: '0.5rem', |
| 47 | + paddingRight: enabled ? '0' : '0.5rem', |
| 48 | + borderRadius: '0.25rem', |
| 49 | + border: `1px solid ${NOT_FOUND}`, |
| 50 | + background: |
| 51 | + theme?.palette.mode === 'light' ? ALICE_BLUE : theme?.palette.background.constant?.table, |
| 52 | + |
| 53 | + textTransform: 'uppercase', |
| 54 | + color: theme?.palette.text.default, |
| 55 | + display: 'flex', |
| 56 | + justifyContent: 'center', |
| 57 | + alignItems: 'center', |
| 58 | + width: '4.5rem', |
| 59 | + fontSize: '0.75rem', |
| 60 | + fontFamily: theme?.typography.fontFamily |
| 61 | +})); |
| 62 | + |
| 63 | +const StyledMenuItem = styled(MenuItem)(({ theme }) => ({ |
| 64 | + textTransform: 'capitalize', |
| 65 | + color: theme.palette.icon.default |
| 66 | +})); |
| 67 | + |
| 68 | +const StyledIcon = styled('div')({ |
| 69 | + marginRight: '0.5rem' |
| 70 | +}); |
| 71 | + |
| 72 | +const VisibilityChipMenu: React.FC<VisibilityChipMenuProps> = ({ |
| 73 | + value, |
| 74 | + onChange, |
| 75 | + options, |
| 76 | + enabled |
| 77 | +}) => { |
| 78 | + const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); |
| 79 | + const open = Boolean(anchorEl); |
| 80 | + const close = (e: MouseEvent) => { |
| 81 | + e.stopPropagation(); |
| 82 | + setAnchorEl(null); |
| 83 | + }; |
| 84 | + const handleOpen = (e: MouseEvent<HTMLElement>) => { |
| 85 | + e.stopPropagation(); |
| 86 | + if (!enabled) return; |
| 87 | + setAnchorEl(e.currentTarget); |
| 88 | + }; |
| 89 | + const handleChange = (e: MouseEvent, value: string) => { |
| 90 | + e.stopPropagation(); |
| 91 | + onChange(value); |
| 92 | + close(e); |
| 93 | + }; |
| 94 | + return ( |
| 95 | + <> |
| 96 | + <StyledButton |
| 97 | + disabled={!enabled} |
| 98 | + onClick={handleOpen} |
| 99 | + data-testid={`design-visibility-${value.toLowerCase()}`} |
| 100 | + > |
| 101 | + <StyledDiv enabled={enabled}> |
| 102 | + <span>{value}</span> |
| 103 | + {enabled && <ArrowDropDownIcon {...iconSmall} />} |
| 104 | + </StyledDiv> |
| 105 | + </StyledButton> |
| 106 | + |
| 107 | + <StyledMenu |
| 108 | + open={open} |
| 109 | + onClose={close} |
| 110 | + anchorEl={anchorEl} |
| 111 | + anchorReference="anchorPosition" |
| 112 | + anchorPosition={{ |
| 113 | + top: (anchorEl?.getBoundingClientRect().bottom ?? 0) + 5, |
| 114 | + left: (anchorEl?.getBoundingClientRect().left ?? 0) + 5 |
| 115 | + }} |
| 116 | + > |
| 117 | + {options.map(([visibility, Icon], index) => ( |
| 118 | + <StyledMenuItem |
| 119 | + key={index} |
| 120 | + data-testid={`visibility-toggle-${visibility.toLowerCase()}`} |
| 121 | + onClick={(e) => handleChange(e, visibility)} |
| 122 | + > |
| 123 | + <StyledIcon> |
| 124 | + <Icon width={16} height={16} /> |
| 125 | + </StyledIcon> |
| 126 | + {visibility} |
| 127 | + </StyledMenuItem> |
| 128 | + ))} |
| 129 | + </StyledMenu> |
| 130 | + </> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +export default VisibilityChipMenu; |
0 commit comments