|
| 1 | +import { useState, useEffect, useRef } from "react"; |
| 2 | +import styled, { css } from "styled-components"; |
| 3 | +import { MdKeyboardArrowDown } from "react-icons/md"; |
| 4 | +import { getFieldTranslationByNames } from "../../services/Utility"; |
| 5 | + |
| 6 | +const DropdownContainer = styled.div` |
| 7 | + position: relative; |
| 8 | + width: 100%; |
| 9 | + font-size: 16px; |
| 10 | +white-space: nowrap; |
| 11 | +`; |
| 12 | + |
| 13 | +const DropdownButton = styled.div` |
| 14 | + padding: 10px 12px; |
| 15 | + border-radius: 5px; |
| 16 | + border: 1px solid #454545; |
| 17 | + background-color: ${({ theme }) => |
| 18 | + theme.colors.newColors.otherColors.inputBg}; |
| 19 | + color: #84858f; |
| 20 | + cursor: pointer; |
| 21 | + display: flex; |
| 22 | + justify-content: space-between; |
| 23 | + align-items: center; |
| 24 | +`; |
| 25 | + |
| 26 | +const ArrowIcon = styled(MdKeyboardArrowDown)` |
| 27 | + font-size: 20px; |
| 28 | + color: #84858f; |
| 29 | + transition: transform 0.25s ease; |
| 30 | + ${({ isOpen }) => |
| 31 | + isOpen && |
| 32 | + css` |
| 33 | + transform: rotate(180deg); |
| 34 | + `} |
| 35 | +`; |
| 36 | + |
| 37 | +const DropdownList = styled.div` |
| 38 | + position: absolute; |
| 39 | + top: 100%; |
| 40 | + left: 0; |
| 41 | + width: 100%; |
| 42 | + background-color: ${({ theme }) => |
| 43 | + theme.colors.newColors.otherColors.inputBg}; |
| 44 | + border-radius: 5px; |
| 45 | + border: 1px solid #454545; |
| 46 | + max-height: 200px; |
| 47 | + overflow-y: auto; |
| 48 | + z-index: 10; |
| 49 | +`; |
| 50 | + |
| 51 | +const DropdownItem = styled.div` |
| 52 | + padding: 10px 12px; |
| 53 | + color: #84858f; |
| 54 | + cursor: pointer; |
| 55 | +
|
| 56 | + &:hover { |
| 57 | + color: white; |
| 58 | + background-color: ${({ theme }) => theme.colors.shades[80]}; |
| 59 | + } |
| 60 | +`; |
| 61 | + |
| 62 | +const SearchInput = styled.input` |
| 63 | + width: 92%; |
| 64 | + font-size: 16px; |
| 65 | + padding: 10px 12px; |
| 66 | + border: none; |
| 67 | + border-bottom: 1px solid #454545; |
| 68 | + background-color: ${({ theme }) => |
| 69 | + theme.colors.newColors.otherColors.inputBg}; |
| 70 | + color: #84858f; |
| 71 | + outline: none; |
| 72 | +`; |
| 73 | +const Dropdown = ({ |
| 74 | + options = [], |
| 75 | + selected, |
| 76 | + onSelect, |
| 77 | + placeholder = "please select", |
| 78 | + searchable = false, |
| 79 | + disSelectOption = true, |
| 80 | +}) => { |
| 81 | + const [isOpen, setIsOpen] = useState(false); |
| 82 | + const [searchTerm, setSearchTerm] = useState(""); |
| 83 | + const dropdownRef = useRef(null); |
| 84 | + const optionsWithPlaceholder = disSelectOption |
| 85 | + ? [placeholder, ...options] |
| 86 | + : options; |
| 87 | + |
| 88 | + const filteredOptions = searchable |
| 89 | + ? optionsWithPlaceholder.filter((option) => |
| 90 | + (typeof option === "string" ? option : option.label) |
| 91 | + .toLowerCase() |
| 92 | + .includes(searchTerm.toLowerCase()) |
| 93 | + ) |
| 94 | + : optionsWithPlaceholder; |
| 95 | + |
| 96 | + const handleOptionClick = (option) => { |
| 97 | + const value = typeof option === "string" ? option : option.value; |
| 98 | + if (disSelectOption && option === placeholder) { |
| 99 | + onSelect(null); |
| 100 | + } else { |
| 101 | + onSelect(value); |
| 102 | + } |
| 103 | + setIsOpen(false); |
| 104 | + setSearchTerm(""); |
| 105 | + }; |
| 106 | + |
| 107 | + useEffect(() => { |
| 108 | + const handleClickOutside = (event) => { |
| 109 | + if (dropdownRef.current && !dropdownRef.current.contains(event.target)) { |
| 110 | + setIsOpen(false); |
| 111 | + } |
| 112 | + }; |
| 113 | + document.addEventListener("mousedown", handleClickOutside); |
| 114 | + return () => document.removeEventListener("mousedown", handleClickOutside); |
| 115 | + }, []); |
| 116 | + |
| 117 | + return ( |
| 118 | + <DropdownContainer ref={dropdownRef}> |
| 119 | + <DropdownButton onClick={() => setIsOpen(!isOpen)}> |
| 120 | + {selected || placeholder} |
| 121 | + <ArrowIcon isOpen={isOpen} /> |
| 122 | + </DropdownButton> |
| 123 | + |
| 124 | + {isOpen && ( |
| 125 | + <DropdownList> |
| 126 | + {searchable && ( |
| 127 | + <SearchInput |
| 128 | + type="text" |
| 129 | + placeholder={getFieldTranslationByNames("57")} |
| 130 | + value={searchTerm} |
| 131 | + onChange={(e) => setSearchTerm(e.target.value)} |
| 132 | + /> |
| 133 | + )} |
| 134 | + |
| 135 | + {filteredOptions.map((option, index) => ( |
| 136 | + <DropdownItem key={index} onClick={() => handleOptionClick(option)}> |
| 137 | + {typeof option === "string" ? option : option.label} |
| 138 | + </DropdownItem> |
| 139 | + ))} |
| 140 | + </DropdownList> |
| 141 | + )} |
| 142 | + </DropdownContainer> |
| 143 | + ); |
| 144 | +}; |
| 145 | + |
| 146 | +export default Dropdown; |
0 commit comments