|
| 1 | +import { AppHeaderDropdown, ClearInputAdornment } from "@comet/admin"; |
| 2 | +import { Company, Search } from "@comet/admin-icons"; |
| 3 | +import { findTextMatches, MarkedMatches } from "@comet/cms-admin"; |
| 4 | +import { Box, Divider, InputAdornment, InputBase, List, ListItem, ListItemButton, ListItemIcon, ListItemText, useTheme } from "@mui/material"; |
| 5 | +import { type ReactNode, useState } from "react"; |
| 6 | +import { FormattedMessage, useIntl } from "react-intl"; |
| 7 | + |
| 8 | +type TenantOption = { |
| 9 | + id: string; |
| 10 | + name: string; |
| 11 | +}; |
| 12 | + |
| 13 | +interface Props { |
| 14 | + value: string; |
| 15 | + onChange: (value: string) => void; |
| 16 | + options: Array<TenantOption>; |
| 17 | + searchable?: boolean; |
| 18 | + icon?: ReactNode; |
| 19 | +} |
| 20 | + |
| 21 | +export function TenantScopeSelect({ value, onChange, options, searchable = true, icon = <Company /> }: Props) { |
| 22 | + const intl = useIntl(); |
| 23 | + const theme = useTheme(); |
| 24 | + const [searchValue, setSearchValue] = useState<string>(""); |
| 25 | + let filteredOptions = options; |
| 26 | + |
| 27 | + if (searchable) { |
| 28 | + filteredOptions = options.filter((option) => { |
| 29 | + return option.name.toLowerCase().includes(searchValue.toLowerCase()); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + const selectedOption = options.find((option) => option.id === value); |
| 34 | + |
| 35 | + const handleChange = (selectedId: string) => { |
| 36 | + onChange(selectedId); |
| 37 | + }; |
| 38 | + |
| 39 | + return ( |
| 40 | + <AppHeaderDropdown |
| 41 | + buttonChildren={selectedOption?.name ?? ""} |
| 42 | + startIcon={icon} |
| 43 | + slotProps={{ |
| 44 | + root: { |
| 45 | + sx: (theme) => ({ |
| 46 | + overflow: "hidden", |
| 47 | + width: "100%", |
| 48 | + |
| 49 | + [theme.breakpoints.up("md")]: { |
| 50 | + width: "auto", |
| 51 | + }, |
| 52 | + }), |
| 53 | + }, |
| 54 | + button: { |
| 55 | + slotProps: { |
| 56 | + root: { |
| 57 | + sx: (theme) => ({ |
| 58 | + width: "100%", |
| 59 | + justifyContent: "start", |
| 60 | + |
| 61 | + [theme.breakpoints.up("md")]: { |
| 62 | + justifyContent: "center", |
| 63 | + }, |
| 64 | + }), |
| 65 | + }, |
| 66 | + content: { |
| 67 | + sx: (theme) => ({ |
| 68 | + width: "100%", |
| 69 | + justifyContent: "start", |
| 70 | + |
| 71 | + [theme.breakpoints.up("md")]: { |
| 72 | + justifyContent: "center", |
| 73 | + }, |
| 74 | + }), |
| 75 | + }, |
| 76 | + endIcon: { |
| 77 | + sx: (theme) => ({ |
| 78 | + [theme.breakpoints.up("xs")]: { |
| 79 | + "&:not(:first-of-type)": { |
| 80 | + marginLeft: "auto", |
| 81 | + }, |
| 82 | + }, |
| 83 | + |
| 84 | + [theme.breakpoints.up("md")]: { |
| 85 | + "&:not(:first-of-type)": { |
| 86 | + marginLeft: theme.spacing(2), |
| 87 | + }, |
| 88 | + }, |
| 89 | + }), |
| 90 | + }, |
| 91 | + typography: { |
| 92 | + sx: { |
| 93 | + textOverflow: "ellipsis", |
| 94 | + overflow: "hidden", |
| 95 | + whiteSpace: "nowrap", |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + popover: { |
| 101 | + anchorOrigin: { vertical: "bottom", horizontal: "left" }, |
| 102 | + transformOrigin: { vertical: "top", horizontal: "left" }, |
| 103 | + PaperProps: { |
| 104 | + sx: (theme) => ({ |
| 105 | + minWidth: "350px", |
| 106 | + maxHeight: "calc(100vh - 60px)", |
| 107 | + [theme.breakpoints.down("md")]: { |
| 108 | + width: "100%", |
| 109 | + maxWidth: "none", |
| 110 | + bottom: 0, |
| 111 | + }, |
| 112 | + }), |
| 113 | + }, |
| 114 | + }, |
| 115 | + }} |
| 116 | + > |
| 117 | + {(hideDropdown) => ( |
| 118 | + <> |
| 119 | + {searchable && ( |
| 120 | + <> |
| 121 | + <Box sx={{ px: 3, py: 2 }}> |
| 122 | + <InputBase |
| 123 | + startAdornment={ |
| 124 | + <InputAdornment position="start"> |
| 125 | + <Search htmlColor={theme.palette.grey[900]} /> |
| 126 | + </InputAdornment> |
| 127 | + } |
| 128 | + placeholder={intl.formatMessage({ |
| 129 | + id: "tenantScopeSelect.searchInput.placeholder", |
| 130 | + defaultMessage: "Search...", |
| 131 | + })} |
| 132 | + value={searchValue} |
| 133 | + onChange={(event) => setSearchValue(event.currentTarget.value)} |
| 134 | + endAdornment={ |
| 135 | + <ClearInputAdornment |
| 136 | + onClick={() => setSearchValue("")} |
| 137 | + hasClearableContent={searchValue !== ""} |
| 138 | + position="end" |
| 139 | + slotProps={{ |
| 140 | + buttonBase: { sx: { fontSize: "16px" } }, |
| 141 | + }} |
| 142 | + /> |
| 143 | + } |
| 144 | + autoFocus |
| 145 | + fullWidth |
| 146 | + /> |
| 147 | + </Box> |
| 148 | + <Divider /> |
| 149 | + </> |
| 150 | + )} |
| 151 | + <List sx={{ paddingTop: 0 }}> |
| 152 | + {filteredOptions.map((option) => { |
| 153 | + const isSelected = option.id === value; |
| 154 | + |
| 155 | + return ( |
| 156 | + <ListItemButton |
| 157 | + key={option.id} |
| 158 | + onClick={() => { |
| 159 | + hideDropdown(); |
| 160 | + handleChange(option.id); |
| 161 | + setSearchValue(""); |
| 162 | + }} |
| 163 | + selected={isSelected} |
| 164 | + sx={({ spacing }) => ({ |
| 165 | + paddingX: spacing(6), |
| 166 | + })} |
| 167 | + > |
| 168 | + <ListItemIcon> |
| 169 | + <Company /> |
| 170 | + </ListItemIcon> |
| 171 | + <ListItemText |
| 172 | + slotProps={{ |
| 173 | + primary: { |
| 174 | + variant: isSelected ? "subtitle2" : "body2", |
| 175 | + }, |
| 176 | + }} |
| 177 | + sx={{ margin: 0 }} |
| 178 | + primary={<MarkedMatches text={option.name} matches={findTextMatches(option.name, searchValue)} />} |
| 179 | + /> |
| 180 | + </ListItemButton> |
| 181 | + ); |
| 182 | + })} |
| 183 | + {filteredOptions.length === 0 && ( |
| 184 | + <ListItem> |
| 185 | + <FormattedMessage id="tenantScopeSelect.list.noOptions" defaultMessage="No options" /> |
| 186 | + </ListItem> |
| 187 | + )} |
| 188 | + </List> |
| 189 | + </> |
| 190 | + )} |
| 191 | + </AppHeaderDropdown> |
| 192 | + ); |
| 193 | +} |
0 commit comments