@@ -7,7 +7,16 @@ import {
7
7
isInterfaceType ,
8
8
isObjectType ,
9
9
} from 'graphql' ;
10
- import { FocusEventHandler , useEffect , useRef , useState } from 'react' ;
10
+ import {
11
+ FocusEventHandler ,
12
+ // eslint-disable-next-line @typescript-eslint/no-restricted-imports
13
+ useCallback ,
14
+ useEffect ,
15
+ // eslint-disable-next-line @typescript-eslint/no-restricted-imports
16
+ useMemo ,
17
+ useRef ,
18
+ useState ,
19
+ } from 'react' ;
11
20
import { Combobox } from '@headlessui/react' ;
12
21
import { MagnifyingGlassIcon } from '../../icons' ;
13
22
import { useSchemaContext } from '../../schema' ;
@@ -20,6 +29,7 @@ import { renderType } from './utils';
20
29
import { isMacOs } from '../../utility/is-macos' ;
21
30
22
31
export function Search ( ) {
32
+ 'use no memo' ; // TODO: add test https://github.com/graphql/graphiql/issues/3842
23
33
const { explorerNavStack, push } = useExplorerContext ( {
24
34
nonNull : true ,
25
35
caller : Search ,
@@ -29,10 +39,13 @@ export function Search() {
29
39
const getSearchResults = useSearchResults ( ) ;
30
40
const [ searchValue , setSearchValue ] = useState ( '' ) ;
31
41
const [ results , setResults ] = useState ( getSearchResults ( searchValue ) ) ;
32
- const [ isFocused , setIsFocused ] = useState ( false ) ;
33
- const debouncedGetSearchResults = debounce ( 200 , ( search : string ) => {
34
- setResults ( getSearchResults ( search ) ) ;
35
- } ) ;
42
+ const debouncedGetSearchResults = useMemo (
43
+ ( ) =>
44
+ debounce ( 200 , ( search : string ) => {
45
+ setResults ( getSearchResults ( search ) ) ;
46
+ } ) ,
47
+ [ getSearchResults ] ,
48
+ ) ;
36
49
useEffect ( ( ) => {
37
50
debouncedGetSearchResults ( searchValue ) ;
38
51
} , [ debouncedGetSearchResults , searchValue ] ) ;
@@ -50,16 +63,20 @@ export function Search() {
50
63
51
64
const navItem = explorerNavStack . at ( - 1 ) ! ;
52
65
53
- const onSelect = ( def : TypeMatch | FieldMatch ) => {
54
- push (
55
- 'field' in def
56
- ? { name : def . field . name , def : def . field }
57
- : { name : def . type . name , def : def . type } ,
58
- ) ;
59
- } ;
60
- const handleFocus : FocusEventHandler = e => {
61
- setIsFocused ( e . type === 'focus' ) ;
62
- } ;
66
+ const onSelect = useCallback (
67
+ ( def : TypeMatch | FieldMatch ) => {
68
+ push (
69
+ 'field' in def
70
+ ? { name : def . field . name , def : def . field }
71
+ : { name : def . type . name , def : def . type } ,
72
+ ) ;
73
+ } ,
74
+ [ push ] ,
75
+ ) ;
76
+ const isFocused = useRef ( false ) ;
77
+ const handleFocus : FocusEventHandler = useCallback ( e => {
78
+ isFocused . current = e . type === 'focus' ;
79
+ } , [ ] ) ;
63
80
64
81
const shouldSearchBoxAppear =
65
82
explorerNavStack . length === 1 ||
@@ -98,7 +115,8 @@ export function Search() {
98
115
</ div >
99
116
100
117
{ /* display on focus */ }
101
- { isFocused && (
118
+ { /* eslint-disable-next-line react-compiler/react-compiler */ }
119
+ { isFocused . current && (
102
120
< Combobox . Options data-cy = "doc-explorer-list" >
103
121
{ results . within . length +
104
122
results . types . length +
0 commit comments