@@ -154,17 +154,23 @@ export const Filter = ({
154
154
learnMoreLink2,
155
155
presets,
156
156
} : FilterProps ) => {
157
- const [ searchQuery , setSearchQuery ] = useState ( filter . query ) ;
157
+ // Storing local copy of the filter query so that it can be updated more
158
+ // aggressively without re-renders rippling out to the rest of the app every
159
+ // single time. Exists for performance reasons - not really a good way to
160
+ // remove this; render keys would cause the component to remount too often
161
+ const [ queryCopy , setQueryCopy ] = useState ( filter . query ) ;
158
162
const textboxInputRef = useRef < HTMLInputElement > ( null ) ;
159
163
164
+ // Conditionally re-syncs the parent and local filter queries
160
165
useEffect ( ( ) => {
161
- // We don't want to update this while the user is typing something or has
162
- // the focus in the input
163
- const hasInnerFocus =
166
+ const hasSelfOrInnerFocus =
164
167
textboxInputRef . current ?. contains ( document . activeElement ) ?? false ;
165
168
166
- if ( ! hasInnerFocus ) {
167
- setSearchQuery ( filter . query ) ;
169
+ // This doesn't address all state sync issues - namely, what happens if the
170
+ // user removes focus just after this synchronizing effect fires. Also need
171
+ // to rely on onBlur behavior as an extra safety measure
172
+ if ( ! hasSelfOrInnerFocus ) {
173
+ setQueryCopy ( filter . query ) ;
168
174
}
169
175
} , [ filter . query ] ) ;
170
176
@@ -205,12 +211,17 @@ export const Filter = ({
205
211
"aria-label" : "Filter" ,
206
212
name : "query" ,
207
213
placeholder : "Search..." ,
208
- value : searchQuery ,
214
+ value : queryCopy ,
209
215
ref : textboxInputRef ,
210
216
onChange : ( e ) => {
211
- setSearchQuery ( e . target . value ) ;
217
+ setQueryCopy ( e . target . value ) ;
212
218
filter . debounceUpdate ( e . target . value ) ;
213
219
} ,
220
+ onBlur : ( ) => {
221
+ if ( queryCopy !== filter . query ) {
222
+ setQueryCopy ( filter . query ) ;
223
+ }
224
+ } ,
214
225
sx : {
215
226
borderRadius : "6px" ,
216
227
borderTopLeftRadius : 0 ,
0 commit comments