|
| 1 | +import React, { HTMLAttributes, useEffect, useRef, useState } from 'react'; |
| 2 | +import { |
| 3 | + styled, |
| 4 | + Popper as MuiPopper, |
| 5 | + PopperProps, |
| 6 | + Paper as MuiPaper, |
| 7 | + ClickAwayListener, |
| 8 | +} from '@mui/material'; |
| 9 | + |
| 10 | +import ArrowDown from '../../../svg/ArrowDown'; |
| 11 | + |
| 12 | +interface ContainerProps extends HTMLAttributes<HTMLButtonElement> { |
| 13 | + color?: React.CSSProperties['color']; |
| 14 | + bgColor?: React.CSSProperties['backgroundColor']; |
| 15 | + disabled?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +const shouldForwardProp = (propName: PropertyKey) => { |
| 19 | + return propName !== 'color' && propName !== 'bgColor'; |
| 20 | +}; |
| 21 | + |
| 22 | +const Container = styled('button', { shouldForwardProp })<ContainerProps>` |
| 23 | + cursor: pointer; |
| 24 | + display: inline-flex; |
| 25 | + align-items: center; |
| 26 | + min-width: 120px; |
| 27 | + text-transform: uppercase; |
| 28 | + font-size: 1rem; |
| 29 | + line-height: 1.5; |
| 30 | + font-weight: 500; |
| 31 | + border-radius: 4px; |
| 32 | + color: ${({ color }) => color ?? '#fff'}; |
| 33 | + padding: 0 10px 0 26px; |
| 34 | + background-color: ${({ bgColor, theme }) => |
| 35 | + bgColor ?? theme.color.secondary.$60}; |
| 36 | + border: none; |
| 37 | +
|
| 38 | + &:disabled { |
| 39 | + opacity: 0.3; |
| 40 | + pointer-events: none; |
| 41 | + } |
| 42 | +`; |
| 43 | + |
| 44 | +const Popper = styled(MuiPopper)` |
| 45 | + &.MuiPopper-root { |
| 46 | + z-index: 1; |
| 47 | + } |
| 48 | +`; |
| 49 | + |
| 50 | +const Paper = styled(MuiPaper)` |
| 51 | + & > ul { |
| 52 | + list-style: none; |
| 53 | + padding: 0; |
| 54 | + margin: 0; |
| 55 | + & > li { |
| 56 | + padding: 5px; |
| 57 | + } |
| 58 | + } |
| 59 | +`; |
| 60 | + |
| 61 | +interface ItemProps { |
| 62 | + color?: React.CSSProperties['color']; |
| 63 | +} |
| 64 | + |
| 65 | +const Item = styled('div')<ItemProps>` |
| 66 | + cursor: pointer; |
| 67 | + text-transform: uppercase; |
| 68 | + color: ${({ theme }) => theme.color.secondary.$60}; |
| 69 | + padding: 5.5px 8px; |
| 70 | + border-radius: 4px; |
| 71 | +
|
| 72 | + &.selected { |
| 73 | + color: ${({ theme }) => theme.color.secondary.$100}; |
| 74 | + pointer-events: none; |
| 75 | + } |
| 76 | +
|
| 77 | + &:hover { |
| 78 | + color: ${({ theme }) => theme.color.secondary.$100}; |
| 79 | + background-color: ${({ theme }) => theme.color.box_bbg}; |
| 80 | + } |
| 81 | +
|
| 82 | + &::before { |
| 83 | + display: inline-block; |
| 84 | + content: ''; |
| 85 | + width: 14px; |
| 86 | + height: 14px; |
| 87 | + border-radius: 4px; |
| 88 | + background-color: ${({ color, theme }) => |
| 89 | + color ?? theme.color.secondary.$60}; |
| 90 | + margin-right: 17px; |
| 91 | + } |
| 92 | +`; |
| 93 | + |
| 94 | +export interface StatusDropdownProps { |
| 95 | + defaultStatus?: string; |
| 96 | + statusMap: { |
| 97 | + [status: string]: { |
| 98 | + color?: React.CSSProperties['color']; |
| 99 | + bgColor?: React.CSSProperties['backgroundColor']; |
| 100 | + displayName: string; |
| 101 | + }; |
| 102 | + }; |
| 103 | + disabled?: boolean; |
| 104 | + popperProps?: PopperProps; |
| 105 | + onSelect?: (status: string) => void; |
| 106 | +} |
| 107 | + |
| 108 | +const StatusDropdown: React.FC<StatusDropdownProps> = ({ |
| 109 | + defaultStatus, |
| 110 | + statusMap, |
| 111 | + disabled, |
| 112 | + popperProps, |
| 113 | + onSelect, |
| 114 | +}) => { |
| 115 | + const entries = Object.entries(statusMap); |
| 116 | + const [status, setStatus] = useState(defaultStatus ?? entries[0][0]); |
| 117 | + const [open, setOpen] = useState(false); |
| 118 | + const buttonRef = useRef<HTMLButtonElement | null>(null); |
| 119 | + |
| 120 | + useEffect(() => { |
| 121 | + if (defaultStatus) { |
| 122 | + setStatus(defaultStatus); |
| 123 | + } |
| 124 | + }, [defaultStatus]); |
| 125 | + |
| 126 | + return ( |
| 127 | + <> |
| 128 | + <Container |
| 129 | + ref={buttonRef} |
| 130 | + color={statusMap[status].color} |
| 131 | + bgColor={statusMap[status].bgColor} |
| 132 | + onClick={() => setOpen(!open)} |
| 133 | + disabled={disabled} |
| 134 | + > |
| 135 | + {statusMap[status].displayName} |
| 136 | + <ArrowDown /> |
| 137 | + </Container> |
| 138 | + <Popper |
| 139 | + open={open} |
| 140 | + anchorEl={buttonRef.current} |
| 141 | + placement="bottom-start" |
| 142 | + disablePortal |
| 143 | + popperOptions={{ |
| 144 | + modifiers: [ |
| 145 | + { |
| 146 | + name: 'offset', |
| 147 | + options: { |
| 148 | + offset: [0, 8], |
| 149 | + }, |
| 150 | + }, |
| 151 | + ], |
| 152 | + }} |
| 153 | + {...popperProps} |
| 154 | + > |
| 155 | + <ClickAwayListener onClickAway={() => setOpen(false)}> |
| 156 | + <Paper> |
| 157 | + <ul> |
| 158 | + {entries.map(([key, value]) => { |
| 159 | + return ( |
| 160 | + <li |
| 161 | + key={`status-item-${key}`} |
| 162 | + onClick={() => { |
| 163 | + setStatus(key); |
| 164 | + setOpen(false); |
| 165 | + onSelect?.(key); |
| 166 | + }} |
| 167 | + > |
| 168 | + <Item |
| 169 | + className={`${key === status ? 'selected' : ''}`} |
| 170 | + color={value.bgColor} |
| 171 | + > |
| 172 | + {value.displayName} |
| 173 | + </Item> |
| 174 | + </li> |
| 175 | + ); |
| 176 | + })} |
| 177 | + </ul> |
| 178 | + </Paper> |
| 179 | + </ClickAwayListener> |
| 180 | + </Popper> |
| 181 | + </> |
| 182 | + ); |
| 183 | +}; |
| 184 | + |
| 185 | +export default StatusDropdown; |
0 commit comments