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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 202x 202x 4210x 4210x 4210x 4210x 4210x 4210x 4210x 559x 559x 4210x 14x 14x 14x 14x 14x 14x 4210x 36x 36x 36x 4210x 4210x 4210x 16x 16x 4340x 186008x 186008x 186008x 64806x 16x 16x 202x 202x | import { GiveInput, InputProps } from "./GiveInput";
import { MenuItem } from "@mui/material";
import {
CaretDownIcon,
CheckIcon,
Icon,
IconProps,
} from "@phosphor-icons/react";
import ContextualMenu, {
StyledGiveSearchBar,
} from "@shared/ContextualMenu/ContextualMenu";
import { ContextualMenuProps } from "@shared/ContextualMenu/ContextualMenu.types";
import GiveText from "@shared/Text/GiveText";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { styled, useAppTheme } from "@theme/v2/Provider";
import {
SyntheticEvent,
useState,
useRef,
forwardRef,
useImperativeHandle,
useCallback,
useEffect,
} from "react";
const ICON_OFFSET = 33;
export type GiveSelectOption = {
label: string;
value: string | number;
disabled?: boolean;
hidden?: boolean;
IconRight?: Icon | React.FC<IconProps>;
};
export type GiveSelectProps = InputProps & {
options: GiveSelectOption[];
value?: string | number;
useMobileDrawer?: boolean;
useContextualMenu?: boolean;
contextualMenuProps?: Partial<ContextualMenuProps>;
updateOpen?: (open: boolean) => void;
onOpen?: () => void;
onClose?: () => void;
onChange?: (e: { target: { value: number | string } }) => void;
inputValueColor?: string;
withIconOffset?: boolean;
withSearchBar?: boolean;
renderValue?: (value: any) => React.ReactNode;
renderFooter?: React.ReactNode;
};
const GiveSelect = forwardRef(
(
{
options,
onScroll,
value,
useMobileDrawer = true,
useContextualMenu,
contextualMenuProps,
updateOpen,
onOpen,
onClose,
onChange,
inputValueColor,
withIconOffset = true,
withSearchBar,
renderValue,
renderFooter,
...rest
}: GiveSelectProps,
ref,
) => {
const theme = useAppTheme();
const { isMobileView } = useCustomThemeV2();
const [menuWidth, setMenuWidth] = useState(0);
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const inputRef = useRef<HTMLDivElement | null>(null);
const [searchValue, setSearchValue] = useState("");
useEffect(() => {
Eif (inputRef.current) {
setMenuWidth(
inputRef.current.offsetWidth + (withIconOffset ? ICON_OFFSET : 0),
);
}
}, [withIconOffset]);
const handleOpenMenu = (e?: SyntheticEvent<Element, Event>) => {
Iif (rest?.disabled) return;
Iif (updateOpen) updateOpen(true);
Iif (onOpen) onOpen();
// Use inputRef.current as the anchor element directly
if (inputRef.current) {
setAnchorEl(inputRef.current);
setMenuWidth(
inputRef.current.offsetWidth + (withIconOffset ? ICON_OFFSET : 0),
);
} else Eif (e?.currentTarget) {
if (e?.currentTarget?.parentElement) {
setMenuWidth(e.currentTarget?.parentElement.offsetWidth);
}
setAnchorEl(e?.currentTarget?.parentElement as HTMLElement);
}
};
const closeMenu = () => {
setAnchorEl(null);
Iif (updateOpen) {
updateOpen(false);
}
Iif (onClose) onClose();
};
useImperativeHandle(ref, () => ({
closeMenu,
}));
const memoizedHandleChange = useCallback((val: string) => {
setSearchValue(val);
}, []);
return (
<>
<GiveInput
ref={inputRef}
select
value={value}
onChange={(e) => {
onChange?.(e);
Iif (e.target.value === "other") {
requestAnimationFrame(() => {
setAnchorEl(inputRef.current);
});
}
}}
SelectProps={{
open:
(isMobileView && useMobileDrawer) || useContextualMenu
? false
: !!anchorEl,
onClose: closeMenu,
onOpen: handleOpenMenu,
renderValue,
IconComponent: () => (
<CaretDownIcon
size={20}
color={
rest?.disabled
? theme.palette.text.disabled
: theme.palette.text.primary
}
style={{
flexShrink: 0,
cursor: rest?.disabled ? "default" : "pointer",
transition: "transform 0.2s ease",
transform: anchorEl ? "rotate(180deg)" : "rotate(0deg)",
}}
onClick={handleOpenMenu}
/>
),
MenuProps: {
autoFocus: false,
PaperProps: {
sx: {
maxHeight: "360px",
boxShadow: "none",
border: `0.5px solid ${theme.palette.border?.primary}`,
borderRadius: `${theme.customs?.radius.mediumSmall}px !important`,
marginTop: "4px",
background: `${theme.palette.surface?.["tertiary-transparent"]} !important`,
backdropFilter: "blur(8px)",
},
style: { padding: "8px" },
onScroll: onScroll,
},
},
classes: {
root: anchorEl && useContextualMenu ? "Mui-focused" : "",
},
}}
InputProps={{
sx: {
padding: `0px !important`,
"& .MuiSelect-select": {
padding: "0 !important",
},
},
}}
sx={{
"& .MuiInputBase-root": {
height: "48px",
},
...(inputValueColor && {
"& .MuiTypography-root": {
color: inputValueColor,
},
}),
}}
{...rest}
>
{withSearchBar && (
<StyledGiveSearchBar
handleChange={memoizedHandleChange}
value={searchValue}
/>
)}
{options
?.filter((o) => !o.hidden)
?.map((option, index) => {
Iif (
withSearchBar &&
!option.label
.toLocaleLowerCase()
.startsWith(searchValue.toLocaleLowerCase())
)
return null;
return (
<StyledMenuItem key={index} value={option.value}>
<GiveText variant="bodyS">{option.label}</GiveText>
</StyledMenuItem>
);
})}
{renderFooter && renderFooter}
</GiveInput>
{((isMobileView && useMobileDrawer) || useContextualMenu) && (
<ContextualMenu
anchorEl={anchorEl as any}
handleClose={closeMenu}
options={options?.map((option) => {
return {
...option,
text: option.label,
IconRight:
value === option.value ? CheckIcon : option.IconRight,
iconPlacement: "right",
disabled: option.disabled,
onClick: () => {
onChange?.({ target: { value: option.value } });
closeMenu();
},
};
})}
color="primary"
texture="solid"
menuWidth={menuWidth}
horizontalOrigin="left"
{...contextualMenuProps}
/>
)}
</>
);
},
);
GiveSelect.displayName = "GiveSelect";
export default GiveSelect;
const StyledMenuItem = styled(MenuItem)(({ theme }) => ({
background: "none !important",
padding: "8px 8px 8px 12px",
borderRadius: `${theme?.customs?.radius.extraSmall}px`,
"&:hover": {
background: `${theme?.palette?.primitive?.transparent["darken-5"]} !important`,
},
"&.Mui-selected": {
background: theme?.palette?.primitive?.transparent["darken-5"],
"&:hover": {
background: theme?.palette?.primitive?.transparent["darken-5"],
},
},
}));
|