Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 363x 363x 363x 3x 3x 1x 1x 2x 363x 363x 363x 363x 363x 363x 13x 1465x 363x 13x 1102x 363x | import { CloseIcon } from "@assets/icons";
import { Text } from "@common/Text";
import { ClickAwayListener, Popper } from "@mui/material";
import { Box, BoxProps } from "@mui/material";
import { styled } from "@mui/system";
import { palette } from "@palette";
import { MouseEventHandler, useState } from "react";
import SwipeableDrawerMobile from "@components/SwipeableDrawerMobile/SwipeableDrawerMobile";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { SxProps } from "@mui/system";
export type ChipOption = {
label?: string;
value?: string | number;
StartIcon?: () => JSX.Element;
activeColor?: string;
activeBackgroundColor?: string;
};
type PopupComponentProps = {
handleClose: () => void;
handleSave: (param?: any) => void;
};
interface IChipProps extends Omit<BoxProps, "onSelect"> {
item: ChipOption;
onSelect?: (val: string | number | undefined | ChipOption) => void;
selected: boolean;
popupCallback?: (params?: any) => void;
PopupComponent?: (_: PopupComponentProps) => JSX.Element;
deleteCallback?: () => void;
allowMultiSelect?: boolean;
}
export default function Chip({
item,
onSelect,
selected,
PopupComponent,
popupCallback = () => null,
deleteCallback,
allowMultiSelect = false,
...rest
}: IChipProps) {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent<HTMLElement>) => {
Iif (PopupComponent || anchorEl) {
setAnchorEl(anchorEl ? null : event.currentTarget);
return;
}
if (allowMultiSelect) {
onSelect && onSelect(item);
return;
}
onSelect && onSelect(selected ? undefined : item.value);
};
const handleDelete: MouseEventHandler = (e) => {
e.stopPropagation();
const value = allowMultiSelect ? item : undefined;
onSelect && onSelect(value);
deleteCallback && deleteCallback();
};
const handleClosePopup = () => {
setAnchorEl(null);
};
const { isMobileView } = useCustomTheme();
const bgColor = selected
? item?.activeBackgroundColor || palette.info.light
: palette.neutral[10];
const color = selected
? item?.activeColor || palette.info.main
: palette.neutral[80];
return (
<ChipContainer
bgcolor={bgColor}
onClick={handleClick}
selected={selected}
{...rest}
>
{item?.StartIcon && item.StartIcon()}
<ChipLabel color={color} selected={selected}>
{item.label}
</ChipLabel>
{selected && (
<CloseIcon
onClick={handleDelete}
stroke={item?.activeColor || palette.info.hard}
width={18}
height={18}
/>
)}
{isMobileView ? (
<>
<SwipeableDrawerMobile
anchor="bottom"
onOpen={() => undefined}
open={open}
onClose={() => {
handleClosePopup();
}}
sx={{
"& .MuiPaper-root.MuiDrawer-paper": {
background: palette.neutral[5],
padding: "16px 12px",
},
}}
>
<Box
onClick={(e) => {
e.stopPropagation();
}}
>
{PopupComponent && (
<PopupComponent
handleClose={handleClosePopup}
handleSave={popupCallback}
/>
)}
</Box>
</SwipeableDrawerMobile>
</>
) : (
<Popper open={open} anchorEl={anchorEl} sx={{ zIndex: 1500 }}>
<ClickAwayListener onClickAway={handleClosePopup}>
<div onClick={(e) => e.stopPropagation()}>
{PopupComponent && (
<PopupComponent
handleClose={handleClosePopup}
handleSave={popupCallback}
/>
)}
</div>
</ClickAwayListener>
</Popper>
)}
</ChipContainer>
);
}
const ChipContainer = styled(Box, {
shouldForwardProp: (prop) => prop !== "selected",
})<{ selected: boolean }>(({ selected }) => ({
gap: "2px",
padding: "2px 8px",
borderRadius: "100px",
width: "fit-content",
display: "flex",
flexDirection: "row",
alignItems: "center",
height: "22px",
cursor: "pointer",
...(!selected && {
"&:hover": {
background: palette.neutral[20],
},
}),
})) as React.ComponentType<BoxProps & { selected: boolean; sx?: SxProps<any> }>;
const ChipLabel = styled(Text, {
shouldForwardProp: (prop) => prop !== "selected",
})<{ selected: boolean }>(({ selected }) => ({
fontSize: "12px",
lineHeight: "14.4px",
}));
|