|
| 1 | +import { useRef, useState } from 'react'; |
| 2 | +import React from 'react'; |
| 3 | +import c from './Dropdown.module.scss'; |
| 4 | +import { DropdownOption } from './DropdownOption'; |
| 5 | +import ArrowIcon from '../../assets/icons/arrow-down-1.svg'; |
| 6 | +import clsx from 'clsx'; |
| 7 | +import { useClickOutside } from '../../hooks/useClickOutside'; |
| 8 | + |
| 9 | +type DropdownProps = { |
| 10 | + label: string; |
| 11 | + placeholder: string; |
| 12 | + options: DropdownOption[]; |
| 13 | + setOption: (option: DropdownOption) => void; |
| 14 | + selectedOption: DropdownOption | undefined; |
| 15 | + errorLabel?: string; |
| 16 | + showError?: boolean; |
| 17 | + width?: string; |
| 18 | + hasError?: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +const Dropdown = ({ |
| 22 | + label, |
| 23 | + placeholder, |
| 24 | + options, |
| 25 | + setOption, |
| 26 | + selectedOption, |
| 27 | + errorLabel, |
| 28 | + width = 'auto', |
| 29 | + hasError = false, |
| 30 | +}: DropdownProps) => { |
| 31 | + const [isOpen, setIsOpen] = useState(false); |
| 32 | + const dropdownRef = useRef(null); |
| 33 | + |
| 34 | + const toggle = () => { |
| 35 | + setIsOpen(!isOpen); |
| 36 | + }; |
| 37 | + |
| 38 | + function handleOptionSelected(option: DropdownOption) { |
| 39 | + setOption(option); |
| 40 | + setIsOpen(false); |
| 41 | + } |
| 42 | + |
| 43 | + useClickOutside(dropdownRef, () => setIsOpen(false)); |
| 44 | + |
| 45 | + const widthStyle = { width: width }; |
| 46 | + const showError = (hasError || !selectedOption?.value) && !isOpen; |
| 47 | + |
| 48 | + return ( |
| 49 | + <div className={c.wrapper} style={widthStyle} ref={dropdownRef}> |
| 50 | + {label && <label className={c.label}>{label}</label>} |
| 51 | + |
| 52 | + <button |
| 53 | + className={clsx({ |
| 54 | + [c.mainButton]: true, |
| 55 | + [c.isOpen]: isOpen, |
| 56 | + [c.isError]: showError, |
| 57 | + })} |
| 58 | + onClick={toggle}> |
| 59 | + {selectedOption?.label || placeholder} |
| 60 | + <img className={c.arrow} src={ArrowIcon} alt='arrow' /> |
| 61 | + </button> |
| 62 | + |
| 63 | + {showError && <div className={c.errorLabel}>{errorLabel}</div>} |
| 64 | + |
| 65 | + {isOpen && ( |
| 66 | + <div className={c.optionsWrapper}> |
| 67 | + <div className={c.innerContainer}> |
| 68 | + {options.map((option, i) => ( |
| 69 | + <React.Fragment key={option.value}> |
| 70 | + {i !== 0 && <div className={c.divider} key={i}></div>} |
| 71 | + <button |
| 72 | + disabled={option.value === selectedOption?.value} |
| 73 | + className={clsx({ |
| 74 | + [c.option]: true, |
| 75 | + [c.selected]: option.value === selectedOption?.value, |
| 76 | + })} |
| 77 | + key={option.value} |
| 78 | + onClick={() => handleOptionSelected(option)}> |
| 79 | + {option.label} |
| 80 | + </button> |
| 81 | + </React.Fragment> |
| 82 | + ))} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + )} |
| 86 | + </div> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +export default Dropdown; |
0 commit comments