|
| 1 | +import { type Theme, useTheme } from "@emotion/react"; |
| 2 | +import CheckOutlined from "@mui/icons-material/CheckOutlined"; |
| 3 | +import Button from "@mui/material/Button"; |
| 4 | +import MenuItem from "@mui/material/MenuItem"; |
| 5 | +import MenuList from "@mui/material/MenuList"; |
| 6 | +import type { FC, ReactNode } from "react"; |
| 7 | +import { DropdownArrow } from "components/DropdownArrow/DropdownArrow"; |
| 8 | +import { |
| 9 | + Popover, |
| 10 | + PopoverContent, |
| 11 | + PopoverTrigger, |
| 12 | +} from "components/Popover/Popover"; |
| 13 | +import { Stack } from "components/Stack/Stack"; |
| 14 | + |
| 15 | +type StatusCircleProps = { |
| 16 | + color: keyof Theme["roles"]; |
| 17 | +}; |
| 18 | + |
| 19 | +const StatusCircle: FC<StatusCircleProps> = ({ color }) => { |
| 20 | + const theme = useTheme(); |
| 21 | + |
| 22 | + return ( |
| 23 | + <div |
| 24 | + css={{ |
| 25 | + width: 8, |
| 26 | + height: 8, |
| 27 | + borderRadius: "50%", |
| 28 | + backgroundColor: theme.roles[color].fill.outline, |
| 29 | + }} |
| 30 | + /> |
| 31 | + ); |
| 32 | +}; |
| 33 | + |
| 34 | +type Option = { |
| 35 | + label: string; |
| 36 | + value: string; |
| 37 | + addon: ReactNode; |
| 38 | +}; |
| 39 | + |
| 40 | +const options: Option[] = [ |
| 41 | + { |
| 42 | + label: "Running", |
| 43 | + value: "running", |
| 44 | + addon: <StatusCircle color="success" />, |
| 45 | + }, |
| 46 | + { |
| 47 | + label: "Stopped", |
| 48 | + value: "stopped", |
| 49 | + addon: <StatusCircle color="inactive" />, |
| 50 | + }, |
| 51 | + { |
| 52 | + label: "Failed", |
| 53 | + value: "failed", |
| 54 | + addon: <StatusCircle color="error" />, |
| 55 | + }, |
| 56 | + { |
| 57 | + label: "Pending", |
| 58 | + value: "pending", |
| 59 | + addon: <StatusCircle color="info" />, |
| 60 | + }, |
| 61 | +]; |
| 62 | + |
| 63 | +type SelectLabelProps = { |
| 64 | + option: Option; |
| 65 | + selected: boolean; |
| 66 | +}; |
| 67 | + |
| 68 | +const SelectLabel: FC<SelectLabelProps> = ({ option, selected }) => { |
| 69 | + return ( |
| 70 | + <Stack |
| 71 | + direction="row" |
| 72 | + alignItems="center" |
| 73 | + spacing={2} |
| 74 | + css={{ width: "100%", lineHeight: 1 }} |
| 75 | + > |
| 76 | + <span css={{ flexShrink: 0 }} role="presentation"> |
| 77 | + {option.addon} |
| 78 | + </span> |
| 79 | + <span css={{ width: "100%" }}>{option.label}</span> |
| 80 | + <div css={{ width: 14, height: 14, flexShrink: 0 }} role="presentation"> |
| 81 | + {selected && <CheckOutlined css={{ width: 14, height: 14 }} />} |
| 82 | + </div> |
| 83 | + </Stack> |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +type StatusMenuProps = { |
| 88 | + placeholder: string; |
| 89 | + selected: string | undefined; |
| 90 | + onSelect: (value: string) => void; |
| 91 | +}; |
| 92 | + |
| 93 | +export const StatusMenu: FC<StatusMenuProps> = (props) => { |
| 94 | + const { placeholder, selected, onSelect } = props; |
| 95 | + const selectedOption = options.find((option) => option.value === selected); |
| 96 | + |
| 97 | + return ( |
| 98 | + <Popover> |
| 99 | + {({ setIsOpen }) => { |
| 100 | + return ( |
| 101 | + <> |
| 102 | + <PopoverTrigger> |
| 103 | + <Button |
| 104 | + aria-label="Select status" |
| 105 | + endIcon={<DropdownArrow />} |
| 106 | + startIcon={selectedOption?.addon} |
| 107 | + > |
| 108 | + {selectedOption ? selectedOption.label : placeholder} |
| 109 | + </Button> |
| 110 | + </PopoverTrigger> |
| 111 | + <PopoverContent> |
| 112 | + <MenuList dense> |
| 113 | + {options.map((option) => ( |
| 114 | + <MenuItem |
| 115 | + selected={option.value === selected} |
| 116 | + key={option.value} |
| 117 | + onClick={() => { |
| 118 | + setIsOpen(false); |
| 119 | + onSelect(option.value); |
| 120 | + }} |
| 121 | + > |
| 122 | + <SelectLabel |
| 123 | + option={option} |
| 124 | + selected={option.value === selected} |
| 125 | + /> |
| 126 | + </MenuItem> |
| 127 | + ))} |
| 128 | + </MenuList> |
| 129 | + </PopoverContent> |
| 130 | + </> |
| 131 | + ); |
| 132 | + }} |
| 133 | + </Popover> |
| 134 | + ); |
| 135 | +}; |
0 commit comments