main
js 58 lines 1.75 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import * as React from 'react';
11 import {useState, useContext, useCallback} from 'react';
12
13 import SearchInput from 'react-devtools-shared/src/devtools/views/SearchInput';
14 import {
15 TreeDispatcherContext,
16 TreeStateContext,
17 } from 'react-devtools-shared/src/devtools/views/Components/TreeContext';
18
19 export default function ComponentSearchInput(): React.Node {
20 const [localSearchQuery, setLocalSearchQuery] = useState('');
21 const {searchIndex, searchResults} = useContext(TreeStateContext);
22 const transitionDispatch = useContext(TreeDispatcherContext);
23
24 const search = useCallback(
25 (text: string) => {
26 setLocalSearchQuery(text);
27 transitionDispatch({type: 'SET_SEARCH_TEXT', payload: text});
28 },
29 [setLocalSearchQuery, transitionDispatch],
30 );
31 const goToNextResult = useCallback(
32 () => transitionDispatch({type: 'GO_TO_NEXT_SEARCH_RESULT'}),
33 [transitionDispatch],
34 );
35 const goToPreviousResult = useCallback(
36 () => transitionDispatch({type: 'GO_TO_PREVIOUS_SEARCH_RESULT'}),
37 [transitionDispatch],
38 );
39 const goToResult = useCallback(
40 (index: number) =>
41 transitionDispatch({type: 'GO_TO_SEARCH_RESULT', payload: index}),
42 [transitionDispatch],
43 );
44
45 return (
46 <SearchInput
47 goToNextResult={goToNextResult}
48 goToPreviousResult={goToPreviousResult}
49 goToResult={goToResult}
50 placeholder="Search (text or /regex/)"
51 search={search}
52 searchIndex={searchIndex}
53 searchResultsCount={searchResults.length}
54 searchText={localSearchQuery}
55 testName="ComponentSearchInput"
56 />
57 );
58 }