Skip to content

Commit ad9f4ec

Browse files
committed
feat: visibility chip menu for the public private switch
Signed-off-by: Amit Amrutiya <amitamrutiya2210@gmail.com>
1 parent 81cbc25 commit ad9f4ec

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

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