An open source design system that's fully customizable and agent ready
TypeScript
refactor(Typeahead): invalidate a replaced searchSource during render, not in an effect (#6370)
nynexman4464af5510c873d0
diff --git a/.changeset/powersearch-stale-value-menu.md b/.changeset/powersearch-stale-value-menu.mdindex a2aa9b2873..89f2ee1eca 100644--- a/.changeset/powersearch-stale-value-menu.md+++ b/.changeset/powersearch-stale-value-menu.md@@ -2,6 +2,6 @@'@astryxdesign/core': patch----[fix] PowerSearch: switching the field or operator while a value menu is open now replaces the menu's contents instead of leaving the previous field's options showing. The value editor is keyed on the field and operator it edits, so editors for two fields sharing a value type no longer share one instance; nested sub-filter rows get the same treatment. BaseTypeahead also invalidates in-flight searches when its `searchSource` is replaced, so a late response from the old source can no longer populate the menu.+[fix] PowerSearch: switching the field or operator while the value menu is open now shows the new field's options instead of the old ones.@nynexman4464diff --git a/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx b/packages/core/src/PowerSearch/PowerSearchEditPopover.tsxindex 794c1300c8..e535b8f088 100644--- a/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx+++ b/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx@@ -614,26 +614,14 @@ function NestedEditor({// Value editor cell// =============================================================================-/**- * The value editor's identity is the (field, operator) pair it edits. Keying- * on it remounts the editor when either changes, so nothing an editor keeps- * privately — a Tokenizer's open menu, its query text, results fetched from- * the previous field's source — survives into the next field. Without the- * key, two fields sharing a value type (two `enum_list` fields, say) reuse- * one editor instance and the open menu keeps showing the old field's- * options.- */+// A field switch must replace the editor, not update it in place: a reused+// editor keeps its open menu and old results.function valueEditorKey(filter: {field: string; operator?: string}): string {return `${filter.field}\u0000${filter.operator ?? ''}`;}-/**- * Wraps the value editor and moves focus into it once it mounts. Because the- * cell is keyed on the filter's field and operator, "mount" means both the- * popover opening and the user picking a different field or operator — the- * same focus handoff either way, so the freshly mounted editor can bootstrap- * its own suggestions.- */+// Keyed on field + operator, so this also mounts on a field switch, not just+// when the popover opens; the focus handoff below runs for both.function ValueEditorCell(props: PowerSearchValueEditorProps) {const ref = useRef<HTMLDivElement>(null);diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsxindex 4bff959e07..5ad677664f 100644--- a/packages/core/src/Typeahead/BaseTypeahead.tsx+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx@@ -543,6 +543,15 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({// be re-shown.const resultsGenRef = useRef(0);+ // Results still arriving from a replaced source must not land in the new+ // one's menu.+ const prevSearchSourceRef = useRef(searchSource);+ if (prevSearchSourceRef.current !== searchSource) {+ prevSearchSourceRef.current.cancel?.();+ prevSearchSourceRef.current = searchSource;+ searchGenRef.current++;+ }+// Layer for dropdownconst handleLayerShow = useCallback(() => {onOpenChange?.(true);@@ -987,20 +996,15 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({const selectedKey =value == null ? null : getKey(value.id, () => results.indexOf(value));- // A replaced or unmounted source ends its search lifetime: drop the pending- // debounce, cancel in-flight work where the source supports it, and advance- // the generation so a late response from the old source can no longer- // commit results into a typeahead that is now reading a different source.+ // Unmount: clear the pending debounce and cancel in-flight work.useEffect(() => {return () => {if (searchTimeoutRef.current) {clearTimeout(searchTimeoutRef.current);}- // eslint-disable-next-line @eslint-react/exhaustive-deps -- a counter, not a node: the cleanup must advance the live generation- searchGenRef.current++;- searchSource.cancel?.();+ prevSearchSourceRef.current.cancel?.();};- }, [searchSource]);+ }, []);return (<>