An open source design system that's fully customizable and agent ready
TypeScript
fix(Tokenizer): forward cancel to the search source (#8777)
Adam Hull462048e861ef
diff --git a/.changeset/tokenizer-forward-search-cancel.md b/.changeset/tokenizer-forward-search-cancel.mdnew file mode 100644index 0000000000..87787faa8e--- /dev/null+++ b/.changeset/tokenizer-forward-search-cancel.md@@ -0,0 +1,6 @@+---+'@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.+@hurrymapleladdiff --git a/packages/core/src/Tokenizer/Tokenizer.test.tsx b/packages/core/src/Tokenizer/Tokenizer.test.tsxindex 74d35145d1..628a9d5b01 100644--- a/packages/core/src/Tokenizer/Tokenizer.test.tsx+++ b/packages/core/src/Tokenizer/Tokenizer.test.tsx@@ -1987,6 +1987,190 @@ describe('Tokenizer', () => {});});+describe('Tokenizer search cancellation', () => {+ // A source that records each call and answers only when the test says so.+ function pendingSource() {+ const events: string[] = [];+ const answers = new Map<string, (items: SearchableItem[]) => void>();+ const source: SearchSource = {+ search: async query => {+ events.push(`search:${query}`);+ return new Promise(resolve => answers.set(query, resolve));+ },+ bootstrap: () => [],+ cancel: () => {+ events.push('cancel');+ },+ };+ return {events, answers, source};+ }++ async function type(input: HTMLElement, value: string) {+ fireEvent.change(input, {target: {value}});+ await act(async () => {+ await new Promise(r => setTimeout(r, 0));+ });+ }++ it('cancels the previous search on the consumer source when a newer one starts', async () => {+ const {events, source} = pendingSource();+ render(+ <Tokenizer+ label="Members"+ searchSource={source}+ value={[]}+ onChange={vi.fn()}+ debounceMs={0}+ />,+ );+ const input = screen.getByRole('combobox');+ await type(input, 'a');+ await type(input, 'al');+ const first = events.indexOf('search:a');+ const second = events.indexOf('search:al');+ expect(first).toBeGreaterThanOrEqual(0);+ expect(events.slice(first + 1, second)).toContain('cancel');+ });++ it('cancels a running search on the consumer source when the menu closes with Escape', async () => {+ const {events, answers, source} = pendingSource();+ render(+ <Tokenizer+ label="Members"+ searchSource={source}+ value={[]}+ onChange={vi.fn()}+ debounceMs={0}+ />,+ );+ const input = screen.getByRole('combobox');+ await type(input, 'a');+ await act(async () => {+ answers.get('a')?.([users[0]]);+ });+ expect(input).toHaveAttribute('aria-expanded', 'true');+ await type(input, 'al');+ const before = events.filter(e => e === 'cancel').length;+ fireEvent.keyDown(input, {key: 'Escape'});+ expect(input).toHaveAttribute('aria-expanded', 'false');+ expect(events.filter(e => e === 'cancel').length).toBeGreaterThan(before);+ });++ it('cancels a running search on the consumer source when focus leaves the field', async () => {+ const {events, answers, source} = pendingSource();+ render(+ <>+ <Tokenizer+ label="Members"+ searchSource={source}+ value={[]}+ onChange={vi.fn()}+ debounceMs={0}+ />+ <button type="button">Elsewhere</button>+ </>,+ );+ const input = screen.getByRole('combobox');+ await type(input, 'a');+ await act(async () => {+ answers.get('a')?.([users[0]]);+ });+ await type(input, 'al');+ const before = events.filter(e => e === 'cancel').length;+ fireEvent.blur(input, {+ relatedTarget: screen.getByRole('button', {name: 'Elsewhere'}),+ });+ expect(input).toHaveAttribute('aria-expanded', 'false');+ expect(events.filter(e => e === 'cancel').length).toBeGreaterThan(before);+ });++ it('cancels a running search on the consumer source on unmount', async () => {+ const {events, source} = pendingSource();+ const {unmount} = render(+ <Tokenizer+ label="Members"+ searchSource={source}+ value={[]}+ onChange={vi.fn()}+ debounceMs={0}+ />,+ );+ await type(screen.getByRole('combobox'), 'a');+ const before = events.filter(e => e === 'cancel').length;+ unmount();+ expect(events.filter(e => e === 'cancel').length).toBeGreaterThan(before);+ });++ it('calls cancel with the consumer source as its receiver', async () => {+ const aborted: string[] = [];+ const source = {+ current: 'none',+ async search(query: string) {+ this.current = query;+ return new Promise<SearchableItem[]>(() => {});+ },+ bootstrap: () => [] as SearchableItem[],+ cancel() {+ aborted.push(this.current);+ },+ };+ const {unmount} = render(+ <Tokenizer+ label="Members"+ searchSource={source}+ value={[]}+ onChange={vi.fn()}+ debounceMs={0}+ />,+ );+ await type(screen.getByRole('combobox'), 'a');+ unmount();+ expect(aborted).toContain('a');+ });++ it("ignores a cancelled search's late answer when the source can't abort it", async () => {+ const {answers, source} = pendingSource();+ render(+ <Tokenizer+ label="Members"+ searchSource={source}+ value={[]}+ onChange={vi.fn()}+ debounceMs={0}+ />,+ );+ const input = screen.getByRole('combobox');+ await type(input, 'a');+ await type(input, 'b');+ await act(async () => {+ answers.get('b')?.([users[1]]);+ });+ await act(async () => {+ answers.get('a')?.([users[0]]);+ });+ expect(screen.getByText('Bob')).toBeInTheDocument();+ expect(screen.queryByText('Alice')).not.toBeInTheDocument();+ });++ it('works with a source that has no cancel', async () => {+ const {unmount} = render(+ <Tokenizer+ label="Members"+ searchSource={userSource}+ value={[]}+ onChange={vi.fn()}+ debounceMs={0}+ />,+ );+ const input = screen.getByRole('combobox');+ await type(input, 'a');+ expect(input).toHaveAttribute('aria-expanded', 'true');+ fireEvent.keyDown(input, {key: 'Escape'});+ expect(input).toHaveAttribute('aria-expanded', 'false');+ unmount();+ });+});+describe('Tokenizer required state (FormLayout FR5, AR2)', () => {it('exposes the resolved required state on the combobox', () => {const field = (props: {diff --git a/packages/core/src/Tokenizer/Tokenizer.tsx b/packages/core/src/Tokenizer/Tokenizer.tsxindex 446564f2a1..c539f4c412 100644--- a/packages/core/src/Tokenizer/Tokenizer.tsx+++ b/packages/core/src/Tokenizer/Tokenizer.tsx@@ -1231,6 +1231,9 @@ export function Tokenizer<T extends SearchableItem>({const results = await searchSource.bootstrap();return results.filter(item => !selectedIds.has(item.id));},+ // BaseTypeahead cancels this wrapper, not the caller's source. Pass the+ // call on, so the caller can stop its request.+ cancel: () => searchSource.cancel?.(),}),[searchSource, selectedIds],);