|
| 1 | +import * as React from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import styles from './styles.module.scss'; |
| 4 | + |
| 5 | +type Props = { |
| 6 | + className?: string; |
| 7 | + value: string; |
| 8 | + items: string[]; |
| 9 | + position?: 'left' | 'right'; |
| 10 | + onChange: (value: string) => void; |
| 11 | +}; |
| 12 | + |
| 13 | +export default function Select({ |
| 14 | + className, |
| 15 | + value, |
| 16 | + items, |
| 17 | + position = 'left', |
| 18 | + onChange, |
| 19 | +}: Props) { |
| 20 | + const dropdownRef = React.useRef<HTMLDivElement>(null); |
| 21 | + const [showDropdown, setShowDropdown] = React.useState(false); |
| 22 | + |
| 23 | + React.useEffect(() => { |
| 24 | + const handleClickOutside = ( |
| 25 | + event: MouseEvent | TouchEvent | FocusEvent, |
| 26 | + ) => { |
| 27 | + if ( |
| 28 | + !dropdownRef.current || |
| 29 | + dropdownRef.current.contains(event.target as Node) |
| 30 | + ) { |
| 31 | + return; |
| 32 | + } |
| 33 | + setShowDropdown(false); |
| 34 | + }; |
| 35 | + |
| 36 | + document.addEventListener('mousedown', handleClickOutside); |
| 37 | + document.addEventListener('touchstart', handleClickOutside); |
| 38 | + document.addEventListener('focusin', handleClickOutside); |
| 39 | + |
| 40 | + return () => { |
| 41 | + document.removeEventListener('mousedown', handleClickOutside); |
| 42 | + document.removeEventListener('touchstart', handleClickOutside); |
| 43 | + document.removeEventListener('focusin', handleClickOutside); |
| 44 | + }; |
| 45 | + }, [dropdownRef]); |
| 46 | + |
| 47 | + function handleClick() { |
| 48 | + setShowDropdown(!showDropdown); |
| 49 | + } |
| 50 | + |
| 51 | + function handleKeyDown(event: React.KeyboardEvent<HTMLDivElement>) { |
| 52 | + if (event.key === 'Enter') { |
| 53 | + event.preventDefault(); |
| 54 | + setShowDropdown(!showDropdown); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <div |
| 60 | + ref={dropdownRef} |
| 61 | + className={clsx('dropdown', className, { |
| 62 | + 'dropdown--right': position === 'right', |
| 63 | + 'dropdown--show': showDropdown, |
| 64 | + })} |
| 65 | + onClick={handleClick} |
| 66 | + onKeyDown={handleKeyDown} |
| 67 | + > |
| 68 | + <button |
| 69 | + aria-haspopup="true" |
| 70 | + aria-expanded={showDropdown} |
| 71 | + className={clsx('navbar__link', styles.button, styles.buttonBackground)} |
| 72 | + > |
| 73 | + {value} |
| 74 | + </button> |
| 75 | + |
| 76 | + <ul className="dropdown__menu"> |
| 77 | + {items.map((item, idx) => ( |
| 78 | + <li key={`${item}-${idx}`}> |
| 79 | + <button |
| 80 | + className={clsx( |
| 81 | + 'dropdown__link', |
| 82 | + styles.button, |
| 83 | + item === value |
| 84 | + ? 'dropdown__link--active' |
| 85 | + : styles.buttonBackground, |
| 86 | + )} |
| 87 | + onClick={() => onChange(item)} |
| 88 | + > |
| 89 | + {item} |
| 90 | + </button> |
| 91 | + </li> |
| 92 | + ))} |
| 93 | + </ul> |
| 94 | + </div> |
| 95 | + ); |
| 96 | +} |
0 commit comments