An open source design system that's fully customizable and agent ready
TypeScript
fix(Tokenizer): the bottom sheet cancels pending search work, as the inline field does (#10637)
Cindy Zhang15098bc9f694
diff --git a/.changeset/tokenizer-forward-search-cancel.md b/.changeset/tokenizer-forward-search-cancel.mdindex 87787faa8e..2c3e17f81f 100644--- a/.changeset/tokenizer-forward-search-cancel.md+++ b/.changeset/tokenizer-forward-search-cancel.md@@ -2,5 +2,5 @@'@astryxdesign/core': patch----[fix] Tokenizer's inline field (`presentation="popover"`, and the default `adaptive` with a fine pointer or on a screen that is not compact) forwards `cancel` to its `searchSource`, so a newer search, closing the menu, focus leaving the field, or unmounting cancels the consumer's in-flight search (for example, to abort its request). Previously it wrapped the source to filter selected items and dropped `cancel`, so it was never called. The bottom-sheet presentation does not call `cancel` yet.+[fix] Tokenizer forwards `cancel` to its `searchSource`, so a newer search, closing the menu or the sheet, focus leaving the field, or unmounting cancels the consumer's in-flight search (for example, to abort its request). Previously the inline field wrapped the source to filter selected items and dropped `cancel`, and the bottom-sheet presentation never called it.@hurrymapleladdiff --git a/packages/core/src/Tokenizer/TokenizerBottomSheet.test.tsx b/packages/core/src/Tokenizer/TokenizerBottomSheet.test.tsxindex 9f2a7bfb1d..4551fc6529 100644--- a/packages/core/src/Tokenizer/TokenizerBottomSheet.test.tsx+++ b/packages/core/src/Tokenizer/TokenizerBottomSheet.test.tsx@@ -3367,3 +3367,91 @@ describe('Tokenizer sheet under the BottomSheet stack (component:BottomSheet FR8expect(stackSummary()).toEqual(['Value:covered:1', 'Tags:top:0']);});});++// The sheet calls the source's cancel where the inline field does: when a+// newer query replaces pending work, and when the sheet closes or unmounts+// while work is pending (spec:BaseTypeahead PR1, component:Tokenizer).+describe('Tokenizer sheet presentation: search cancellation', () => {+ function pendingSource() {+ const cancel = vi.fn();+ const pending: SearchSource<SearchableItem> = {+ search: async () => new Promise<SearchableItem[]>(() => {}),+ bootstrap: async () => new Promise<SearchableItem[]>(() => {}),+ cancel,+ };+ return {source: pending, cancel};+ }++ function renderSheet(searchSource: SearchSource<SearchableItem>) {+ return render(+ <Tokenizer+ label="Tags"+ searchSource={searchSource}+ value={[]}+ onChange={() => {}}+ debounceMs={0}+ presentation="bottom-sheet"+ />,+ );+ }++ it('cancels a pending search once when a newer query replaces it', async () => {+ const {source: pending, cancel} = pendingSource();+ renderSheet(pending);+ fireEvent.click(openTrigger());+ const search = await screen.findByLabelText('Search Tags');+ expect(cancel).not.toHaveBeenCalled();+ fireEvent.change(search, {target: {value: 'en'}});+ expect(cancel).toHaveBeenCalledTimes(1);+ });++ it('cancels pending work once when the sheet closes', async () => {+ const {source: pending, cancel} = pendingSource();+ renderSheet(pending);+ fireEvent.click(openTrigger());+ await screen.findByLabelText('Search Tags');+ fireEvent.click(screen.getByRole('button', {name: 'Save'}));+ await waitFor(() => expect(cancel).toHaveBeenCalledTimes(1));+ });++ it('cancels pending work once when the field unmounts', async () => {+ const {source: pending, cancel} = pendingSource();+ const {unmount} = renderSheet(pending);+ fireEvent.click(openTrigger());+ await screen.findByLabelText('Search Tags');+ unmount();+ expect(cancel).toHaveBeenCalledTimes(1);+ });++ it('cancels the source that started a request, not a replacement source', async () => {+ const first = pendingSource();+ const second = pendingSource();+ const {rerender} = renderSheet(first.source);+ fireEvent.click(openTrigger());+ const search = await screen.findByLabelText('Search Tags');+ rerender(+ <Tokenizer+ label="Tags"+ searchSource={second.source}+ value={[]}+ onChange={() => {}}+ debounceMs={0}+ presentation="bottom-sheet"+ />,+ );+ fireEvent.change(search, {target: {value: 'en'}});+ expect(first.cancel).toHaveBeenCalledTimes(1);+ expect(second.cancel).not.toHaveBeenCalled();+ });++ it('does not cancel when no work is pending', async () => {+ const cancel = vi.fn();+ const {unmount} = renderSheet({...source, cancel});+ fireEvent.click(openTrigger());+ const search = await screen.findByLabelText('Search Tags');+ fireEvent.change(search, {target: {value: 'en'}});+ fireEvent.click(screen.getByRole('button', {name: 'Save'}));+ unmount();+ expect(cancel).not.toHaveBeenCalled();+ });+});diff --git a/packages/core/src/Tokenizer/TokenizerBottomSheet.tsx b/packages/core/src/Tokenizer/TokenizerBottomSheet.tsxindex accbd6f9c6..8bc93f6414 100644--- a/packages/core/src/Tokenizer/TokenizerBottomSheet.tsx+++ b/packages/core/src/Tokenizer/TokenizerBottomSheet.tsx@@ -520,13 +520,18 @@ function TokenizerSheetContent<T extends SearchableItem>({// it (`component:Tokenizer` FR20). Cleanup clears the debounce timer so a// superseded query never fetches, and the sequence guard drops a response// that lands after a newer request, so the list never shows stale results- // (`architecture:react-component-runtime` INV3).+ // (`architecture:react-component-runtime` INV3). Cleanup also cancels a+ // request its source has not answered — on a newer query, on close and on+ // unmount — on the source that started it, as the inline field does.useIsomorphicLayoutEffect(() => {if (!isOpen) {+ // Retire the closed sheet's request, so its answer never lands.+ seqRef.current++;return;}const trimmed = query.trim();const seq = ++seqRef.current;+ let unanswered: SearchSource<T> | null = null;if (trimmed !== '' && trimmed.length < minQueryLength) {// Not searched; the derived `results` are already empty, and taking a// sequence number retires a still-running search so its rows cannot@@ -534,6 +539,7 @@ function TokenizerSheetContent<T extends SearchableItem>({return;}const settle = (items: T[]) => {+ unanswered = null;if (seqRef.current === seq) {setSourceResults(items.slice(0, maxMenuItems));setIsPending(false);@@ -555,17 +561,26 @@ function TokenizerSheetContent<T extends SearchableItem>({}// Anything else is awaited as a thenable — a native Promise, one from// another realm, or a library's — as the inline field's `await` does.+ unanswered = source;void Promise.resolve(answer).then(settle, () => {// A rejecting source settles to no results, as the inline field does:// the previous query's rows are not this query's answer.settle([]);});};- if (trimmed !== '' && debounceMs > 0) {- const timer = setTimeout(run, debounceMs);- return () => clearTimeout(timer);+ const timer =+ trimmed !== '' && debounceMs > 0 ? setTimeout(run, debounceMs) : null;+ if (timer == null) {+ run();}- run();+ return () => {+ if (timer != null) {+ clearTimeout(timer);+ }+ const source: SearchSource<T> | null = unanswered;+ unanswered = null;+ source?.cancel?.();+ };}, [isOpen, query, minQueryLength, maxMenuItems, debounceMs]);const visibleItems = useMemo(() => {