|
| 1 | +import { useEffect, useRef, useState } from 'react' |
| 2 | +import { Text, Card } from '@nextui-org/react' |
| 3 | + |
| 4 | +// Simple hook to detect clicks outside an element |
| 5 | +const useClickAway = (onClickAway: () => void) => { |
| 6 | + const ref = useRef<HTMLDivElement>(null) |
| 7 | + |
| 8 | + useEffect(() => { |
| 9 | + const handleClickOutside = (event: MouseEvent) => { |
| 10 | + if (ref.current && !ref.current.contains(event.target as Node)) { |
| 11 | + onClickAway() |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + document.addEventListener('mousedown', handleClickOutside) |
| 16 | + return () => { |
| 17 | + document.removeEventListener('mousedown', handleClickOutside) |
| 18 | + } |
| 19 | + }, [onClickAway]) |
| 20 | + |
| 21 | + return ref |
| 22 | +} |
| 23 | +// Style variables |
| 24 | +const styles = { |
| 25 | + dropdownContainer: { |
| 26 | + position: 'relative' as const |
| 27 | + }, |
| 28 | + dropdownButton: { |
| 29 | + padding: '8px 16px', |
| 30 | + background: '$accents0', |
| 31 | + cursor: 'pointer' |
| 32 | + }, |
| 33 | + cardBody: { |
| 34 | + overflow: 'visible' as const, |
| 35 | + display: 'flex' as const, |
| 36 | + justifyContent: 'space-between' as const, |
| 37 | + alignItems: 'center' as const, |
| 38 | + padding: '4px 0' |
| 39 | + }, |
| 40 | + contentWrapper: { |
| 41 | + display: 'flex' as const, |
| 42 | + alignItems: 'center' as const, |
| 43 | + width: '100%' |
| 44 | + }, |
| 45 | + chevronContainer: { |
| 46 | + marginLeft: 'auto' |
| 47 | + }, |
| 48 | + dropdownMenu: { |
| 49 | + position: 'absolute' as const, |
| 50 | + top: 'calc(100% + 8px)', |
| 51 | + left: 0, |
| 52 | + right: 0, |
| 53 | + zIndex: 1000, |
| 54 | + padding: '4px 8px', |
| 55 | + boxShadow: '0 4px 14px 0 rgba(0, 0, 0, 0.3)', |
| 56 | + maxHeight: '300px', |
| 57 | + overflow: 'auto' as const, |
| 58 | + backgroundColor: '#2a2a2a' |
| 59 | + }, |
| 60 | + checkmark: { |
| 61 | + position: 'absolute' as const, |
| 62 | + right: '10px', |
| 63 | + top: '50%', |
| 64 | + transform: 'translateY(-50%)', |
| 65 | + width: '20px', |
| 66 | + height: '20px', |
| 67 | + borderRadius: '50%', |
| 68 | + backgroundColor: '#17c964', |
| 69 | + display: 'flex' as const, |
| 70 | + alignItems: 'center' as const, |
| 71 | + justifyContent: 'center' as const, |
| 72 | + color: 'white', |
| 73 | + boxShadow: '0 2px 8px rgba(23, 201, 100, 0.5)' |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Generic dropdown component |
| 78 | +export default function GenericDropdown({ |
| 79 | + items, |
| 80 | + selectedIndex, |
| 81 | + setSelectedIndex, |
| 82 | + renderItem, |
| 83 | + placeholder = 'Select an option', |
| 84 | + emptyMessage = 'No options available' |
| 85 | +}: { |
| 86 | + items: any[] |
| 87 | + selectedIndex: number |
| 88 | + setSelectedIndex: (index: number) => void |
| 89 | + renderItem: (item: any) => React.ReactNode |
| 90 | + placeholder?: string |
| 91 | + emptyMessage?: string |
| 92 | +}) { |
| 93 | + const [isOpen, setIsOpen] = useState(false) |
| 94 | + |
| 95 | + // Reference to detect clicks outside dropdown |
| 96 | + const dropdownRef = useClickAway(() => { |
| 97 | + setIsOpen(false) |
| 98 | + }) |
| 99 | + |
| 100 | + const handleSelect = (index: number) => { |
| 101 | + setSelectedIndex(index) |
| 102 | + setIsOpen(false) |
| 103 | + } |
| 104 | + |
| 105 | + // Get style for dropdown item |
| 106 | + const getDropdownItemStyle = (isSelected: boolean, isLast: boolean) => ({ |
| 107 | + cursor: 'pointer' as const, |
| 108 | + padding: '8px 4px', |
| 109 | + borderRadius: '8px', |
| 110 | + backgroundColor: isSelected ? 'rgba(23, 201, 100, 0.2)' : 'transparent', |
| 111 | + position: 'relative' as const, |
| 112 | + marginBottom: 0, |
| 113 | + borderBottom: isLast ? 'none' : '1px solid rgba(255, 255, 255, 0.1)', |
| 114 | + paddingBottom: '8px', |
| 115 | + marginTop: '8px', |
| 116 | + transition: 'all 0.2s ease' |
| 117 | + }) |
| 118 | + |
| 119 | + // Handle hover effects |
| 120 | + const handleMouseOver = (e: React.MouseEvent<HTMLDivElement>, isSelected: boolean) => { |
| 121 | + if (!isSelected) { |
| 122 | + e.currentTarget.style.backgroundColor = 'rgba(255, 255, 255, 0.1)' |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + const handleMouseOut = (e: React.MouseEvent<HTMLDivElement>, isSelected: boolean) => { |
| 127 | + if (!isSelected) { |
| 128 | + e.currentTarget.style.backgroundColor = 'transparent' |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Render chevron icon |
| 133 | + const renderChevron = () => ( |
| 134 | + <svg |
| 135 | + width="16" |
| 136 | + height="16" |
| 137 | + viewBox="0 0 24 24" |
| 138 | + fill="none" |
| 139 | + stroke="currentColor" |
| 140 | + strokeWidth="2" |
| 141 | + strokeLinecap="round" |
| 142 | + strokeLinejoin="round" |
| 143 | + style={{ |
| 144 | + transform: isOpen ? 'rotate(180deg)' : 'rotate(0deg)', |
| 145 | + transition: 'transform 0.2s ease' |
| 146 | + }} |
| 147 | + > |
| 148 | + <polyline points="6 9 12 15 18 9"></polyline> |
| 149 | + </svg> |
| 150 | + ) |
| 151 | + |
| 152 | + // Render checkmark icon |
| 153 | + const renderCheckmark = () => ( |
| 154 | + <div style={styles.checkmark}> |
| 155 | + <svg |
| 156 | + xmlns="http://www.w3.org/2000/svg" |
| 157 | + width="14" |
| 158 | + height="14" |
| 159 | + viewBox="0 0 24 24" |
| 160 | + fill="none" |
| 161 | + stroke="currentColor" |
| 162 | + strokeWidth="3" |
| 163 | + strokeLinecap="round" |
| 164 | + strokeLinejoin="round" |
| 165 | + > |
| 166 | + <polyline points="20 6 9 17 4 12"></polyline> |
| 167 | + </svg> |
| 168 | + </div> |
| 169 | + ) |
| 170 | + |
| 171 | + return ( |
| 172 | + <div ref={dropdownRef} style={styles.dropdownContainer}> |
| 173 | + {/* Dropdown Button */} |
| 174 | + <Card onClick={() => setIsOpen(!isOpen)} css={styles.dropdownButton}> |
| 175 | + <Card.Body css={styles.cardBody}> |
| 176 | + <div style={styles.contentWrapper}> |
| 177 | + {items.length > 0 && selectedIndex >= 0 ? ( |
| 178 | + renderItem(items[selectedIndex]) |
| 179 | + ) : ( |
| 180 | + <Text size={14} css={{ color: '#aaaaaa' }}> |
| 181 | + {placeholder} |
| 182 | + </Text> |
| 183 | + )} |
| 184 | + <div style={styles.chevronContainer}>{renderChevron()}</div> |
| 185 | + </div> |
| 186 | + </Card.Body> |
| 187 | + </Card> |
| 188 | + |
| 189 | + {/* Dropdown Menu */} |
| 190 | + {isOpen && ( |
| 191 | + <Card css={styles.dropdownMenu}> |
| 192 | + {items.length > 0 ? ( |
| 193 | + items.map((item, idx) => { |
| 194 | + const isSelected = selectedIndex === idx |
| 195 | + const isLastItem = idx === items.length - 1 |
| 196 | + |
| 197 | + return ( |
| 198 | + <div |
| 199 | + key={idx} |
| 200 | + onClick={() => handleSelect(idx)} |
| 201 | + style={getDropdownItemStyle(isSelected, isLastItem)} |
| 202 | + onMouseOver={e => handleMouseOver(e, isSelected)} |
| 203 | + onMouseOut={e => handleMouseOut(e, isSelected)} |
| 204 | + > |
| 205 | + {renderItem(item)} |
| 206 | + {isSelected && renderCheckmark()} |
| 207 | + </div> |
| 208 | + ) |
| 209 | + }) |
| 210 | + ) : ( |
| 211 | + <Text css={{ color: '$accents7', padding: '8px 4px' }}>{emptyMessage}</Text> |
| 212 | + )} |
| 213 | + </Card> |
| 214 | + )} |
| 215 | + </div> |
| 216 | + ) |
| 217 | +} |
0 commit comments