Skip to content

Commit 11c06e1

Browse files
committed
fix: Add onBlur behavior for state syncs
1 parent 2d4d285 commit 11c06e1

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

site/src/components/Filter/filter.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,23 @@ export const Filter = ({
154154
learnMoreLink2,
155155
presets,
156156
}: 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);
158162
const textboxInputRef = useRef<HTMLInputElement>(null);
159163

164+
// Conditionally re-syncs the parent and local filter queries
160165
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 =
164167
textboxInputRef.current?.contains(document.activeElement) ?? false;
165168

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);
168174
}
169175
}, [filter.query]);
170176

@@ -205,12 +211,17 @@ export const Filter = ({
205211
"aria-label": "Filter",
206212
name: "query",
207213
placeholder: "Search...",
208-
value: searchQuery,
214+
value: queryCopy,
209215
ref: textboxInputRef,
210216
onChange: (e) => {
211-
setSearchQuery(e.target.value);
217+
setQueryCopy(e.target.value);
212218
filter.debounceUpdate(e.target.value);
213219
},
220+
onBlur: () => {
221+
if (queryCopy !== filter.query) {
222+
setQueryCopy(filter.query);
223+
}
224+
},
214225
sx: {
215226
borderRadius: "6px",
216227
borderTopLeftRadius: 0,

0 commit comments

Comments
 (0)