|
| 1 | +import React, { |
| 2 | + useState, |
| 3 | + ReactNode, |
| 4 | + useCallback, |
| 5 | + useMemo, |
| 6 | + useRef, |
| 7 | + SelectHTMLAttributes, |
| 8 | +} from "react"; |
| 9 | +import { useClickAway } from "react-use"; |
| 10 | +import { SelectContext } from "./select-context"; |
| 11 | +import * as S from "./select.styled"; |
| 12 | + |
| 13 | +type SelectProps = SelectHTMLAttributes<HTMLSelectElement> & { |
| 14 | + children: ReactNode | ReactNode[]; |
| 15 | + errorMessage?: string; |
| 16 | + defaultValue?: string; |
| 17 | + placeholder?: string; |
| 18 | + disabled?: boolean; |
| 19 | + iconSrc?: string; |
| 20 | + width?: string | number; |
| 21 | + label?: string; |
| 22 | + hint?: string; |
| 23 | +}; |
| 24 | + |
| 25 | +export function Select({ |
| 26 | + placeholder = "Choose an option", |
| 27 | + defaultValue = "", |
| 28 | + iconSrc = "", |
| 29 | + disabled = false, |
| 30 | + label = "", |
| 31 | + hint = "", |
| 32 | + errorMessage = "", |
| 33 | + width = "", |
| 34 | + children, |
| 35 | + ...props |
| 36 | +}: SelectProps) { |
| 37 | + const [selectedOption, setSelectedOption] = useState(defaultValue || ""); |
| 38 | + const [showDropdown, setShowDropdown] = useState(false); |
| 39 | + const ref = useRef(null); |
| 40 | + |
| 41 | + // hides dropdown when user clicks outside the select component |
| 42 | + useClickAway(ref, () => { |
| 43 | + setShowDropdown(false); |
| 44 | + }); |
| 45 | + |
| 46 | + const showDropdownHandler = useCallback( |
| 47 | + () => setShowDropdown((prevShowDropdown) => !prevShowDropdown), |
| 48 | + [] |
| 49 | + ); |
| 50 | + |
| 51 | + const updateSelectedOption = useCallback((option: string) => { |
| 52 | + setSelectedOption(option); |
| 53 | + setShowDropdown(false); |
| 54 | + }, []); |
| 55 | + |
| 56 | + const value = useMemo( |
| 57 | + () => ({ selectedOption, changeSelectedOption: updateSelectedOption }), |
| 58 | + [selectedOption, updateSelectedOption] |
| 59 | + ); |
| 60 | + |
| 61 | + return ( |
| 62 | + <SelectContext.Provider value={value}> |
| 63 | + <S.Container ref={ref} width={width} {...props}> |
| 64 | + {label && <S.Label>{label}</S.Label>} |
| 65 | + |
| 66 | + <S.SelectedOption |
| 67 | + onClick={showDropdownHandler} |
| 68 | + selectedOption={selectedOption} |
| 69 | + disabled={disabled} |
| 70 | + errorMessage={errorMessage} |
| 71 | + aria-expanded={showDropdown} |
| 72 | + width={width} |
| 73 | + > |
| 74 | + <S.LeftContainer> |
| 75 | + {iconSrc && <S.OptionalIcon src={iconSrc} />} |
| 76 | + {selectedOption || placeholder} |
| 77 | + </S.LeftContainer> |
| 78 | + |
| 79 | + <S.SelectArrowIcon |
| 80 | + src="/icons/chevron-down.svg" |
| 81 | + showDropdown={showDropdown} |
| 82 | + /> |
| 83 | + </S.SelectedOption> |
| 84 | + |
| 85 | + {hint && !showDropdown && !errorMessage && <S.Hint>{hint}</S.Hint>} |
| 86 | + |
| 87 | + {errorMessage && !showDropdown && !disabled && ( |
| 88 | + <S.ErrorMessage>{errorMessage}</S.ErrorMessage> |
| 89 | + )} |
| 90 | + |
| 91 | + <S.List showDropdown={showDropdown} role="listbox" tabIndex={-1}> |
| 92 | + {children} |
| 93 | + </S.List> |
| 94 | + </S.Container> |
| 95 | + </SelectContext.Provider> |
| 96 | + ); |
| 97 | +} |
0 commit comments