// @flow import React, { useCallback, useContext, useEffect, useRef } from 'react'; import { TreeContext } from './TreeContext'; import Button from '../Button'; import ButtonIcon from '../ButtonIcon'; import Icon from '../Icon'; import styles from './SearchInput.css'; type Props = {||}; export default function SearchInput(props: Props) { const { goToNextSearchResult, goToPreviousSearchResult, searchIndex, searchResults, searchText, selectNextElementInTree, selectPreviousElementInTree, setSearchText, } = useContext(TreeContext); const inputRef = useRef(null); const handleTextChange = useCallback( ({ currentTarget }) => setSearchText(currentTarget.value), [setSearchText] ); const resetSearch = useCallback(() => { setSearchText(''); }, [setSearchText]); const handleKeyDown = useCallback( event => { // For convenience, let up/down arrow keys change Tree selection. switch (event.key) { case 'ArrowDown': selectNextElementInTree(); event.preventDefault(); break; case 'ArrowUp': selectPreviousElementInTree(); event.preventDefault(); break; default: break; } }, [selectNextElementInTree, selectPreviousElementInTree] ); const handleInputKeyPress = useCallback( ({ key }) => { if (key === 'Enter') { goToNextSearchResult(); } }, [goToNextSearchResult] ); // Auto-focus search input useEffect(() => { if (inputRef.current === null) { return () => {}; } const handleWindowKey = (event: KeyboardEvent) => { const { key, metaKey } = event; if (key === 'f' && metaKey) { if (inputRef.current !== null) { inputRef.current.focus(); event.preventDefault(); event.stopPropagation(); } } }; // It's important to listen to the ownerDocument to support the browser extension. // Here we use portals to render individual tabs (e.g. Profiler), // and the root document might belong to a different window. const ownerDocument = inputRef.current.ownerDocument; ownerDocument.addEventListener('keydown', handleWindowKey); return () => ownerDocument.removeEventListener('keydown', handleWindowKey); }, [inputRef]); return (
{!!searchText && ( {Math.min(searchIndex + 1, searchResults.length)} |{' '} {searchResults.length} )}
); }