({ isOpen, onClose })
| 52 | }; |
| 53 | |
| 54 | const SearchDialog = ({ isOpen, onClose }) => { |
| 55 | const [inputValue, setInputValue] = useState(''); |
| 56 | const [searchValue, setSearchValue] = useState(''); |
| 57 | const [topGradientOpacity, setTopGradientOpacity] = useState(0); |
| 58 | const [bottomGradientOpacity, setBottomGradientOpacity] = useState(1); |
| 59 | const [selectedIndex, setSelectedIndex] = useState(-1); |
| 60 | const [keyboardNav, setKeyboardNav] = useState(false); |
| 61 | const resultsRef = useRef(null); |
| 62 | const navigate = useNavigate(); |
| 63 | const { toggleSearch } = useSearch(); |
| 64 | |
| 65 | useEffect(() => { |
| 66 | const t = setTimeout(() => { |
| 67 | setSearchValue(inputValue); |
| 68 | setSelectedIndex(-1); |
| 69 | }, 500); |
| 70 | return () => clearTimeout(t); |
| 71 | }, [inputValue]); |
| 72 | |
| 73 | const results = searchComponents(searchValue); |
| 74 | |
| 75 | const handleScroll = e => { |
| 76 | const { scrollTop, scrollHeight, clientHeight } = e.target; |
| 77 | setTopGradientOpacity(Math.min(scrollTop / 50, 1)); |
| 78 | const bottomDist = scrollHeight - (scrollTop + clientHeight); |
| 79 | setBottomGradientOpacity(scrollHeight <= clientHeight ? 0 : Math.min(bottomDist / 50, 1)); |
| 80 | }; |
| 81 | |
| 82 | useEffect(() => { |
| 83 | if (!resultsRef.current) return; |
| 84 | const { scrollTop, scrollHeight, clientHeight } = resultsRef.current; |
| 85 | setBottomGradientOpacity( |
| 86 | scrollHeight <= clientHeight ? 0 : Math.min((scrollHeight - (scrollTop + clientHeight)) / 50, 1) |
| 87 | ); |
| 88 | }, [results]); |
| 89 | |
| 90 | const handleSelect = useCallback( |
| 91 | result => { |
| 92 | const slug = str => str.replace(/\s+/g, '-').toLowerCase(); |
| 93 | navigate(`/${slug(result.categoryName)}/${slug(result.componentName)}`); |
| 94 | setInputValue(''); |
| 95 | setSearchValue(''); |
| 96 | setSelectedIndex(-1); |
| 97 | onClose(); |
| 98 | }, |
| 99 | [navigate, onClose] |
| 100 | ); |
| 101 | |
| 102 | useEffect(() => { |
| 103 | const onKey = e => { |
| 104 | if (!searchValue) return; |
| 105 | if (e.key === 'ArrowDown' || (e.key === 'Tab' && !e.shiftKey)) { |
| 106 | e.preventDefault(); |
| 107 | setKeyboardNav(true); |
| 108 | setSelectedIndex(p => Math.min(p + 1, results.length - 1)); |
| 109 | } else if (e.key === 'ArrowUp' || (e.key === 'Tab' && e.shiftKey)) { |
| 110 | e.preventDefault(); |
| 111 | setKeyboardNav(true); |
nothing calls this directly
no test coverage detected