Skip to content

Commit 01d06d5

Browse files
authored
Merge pull request #845 from amitamrutiya/visiblitymenu
visibility chip menu for the public private switch
2 parents fe9d911 + 91b8473 commit 01d06d5

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import VisibilityChipMenu from './VisibilityChipMenu';
2+
3+
export { VisibilityChipMenu };

src/custom/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { TransferList } from './TransferModal/TransferList';
4747
import { TransferListProps } from './TransferModal/TransferList/TransferList';
4848
import UniversalFilter, { UniversalFilterProps } from './UniversalFilter';
4949
import { UsersTable } from './UsersTable';
50+
import { VisibilityChipMenu } from './VisibilityChipMenu';
5051
export { CatalogCard } from './CatalogCard';
5152
export { CatalogFilterSidebar } from './CatalogFilterSection';
5253
export type { FilterListType } from './CatalogFilterSection';
@@ -100,6 +101,7 @@ export {
100101
TransferList,
101102
UniversalFilter,
102103
UsersTable,
104+
VisibilityChipMenu,
103105
updateVisibleColumns,
104106
useNotificationHandler,
105107
useWindowDimensions,

0 commit comments

Comments
 (0)