Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(useTypeahead): clear the pending buffer reset on unmount (#7938)

Christopher Chedeauf32572e75dd8
diff --git a/.changeset/typeahead-clears-reset-on-unmount.md b/.changeset/typeahead-clears-reset-on-unmount.md
new file mode 100644
index 0000000000..f88d06fb50
--- /dev/null
+++ b/.changeset/typeahead-clears-reset-on-unmount.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] useTypeahead clears its pending buffer reset when its component unmounts or an `<Activity>` boundary hides it, so a menu, list or selector removed within 750 ms of a typed character leaves no timer running behind it, and a hidden one comes back with an empty buffer
+@vjeux
diff --git a/packages/core/src/hooks/useTypeahead.test.tsx b/packages/core/src/hooks/useTypeahead.test.tsx
index 348eaa384c..176ad60735 100644
--- a/packages/core/src/hooks/useTypeahead.test.tsx
+++ b/packages/core/src/hooks/useTypeahead.test.tsx
@@ -10,7 +10,8 @@
*/
import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
-import {renderHook} from '@testing-library/react';
+import {Activity, type ReactNode} from 'react';
+import {render, renderHook} from '@testing-library/react';
import {useTypeahead} from './useTypeahead';
const LABELS = ['Apple', 'Apricot', 'Banana', 'Cherry'] as const;
@@ -86,6 +87,46 @@ describe('useTypeahead', () => {
expect(onMatch).toHaveBeenLastCalledWith(2);
});
+ it('clears the pending buffer reset when the component unmounts', () => {
+ const {result, unmount} = renderHook(() =>
+ useTypeahead({getItemLabels: () => LABELS, onMatch: vi.fn()}),
+ );
+ result.current.onKeyDown(key('a'));
+ expect(vi.getTimerCount()).toBe(1);
+ unmount();
+ expect(vi.getTimerCount()).toBe(0);
+ });
+
+ it('empties the buffer when an Activity boundary hides the collection', () => {
+ const latest: {api: ReturnType<typeof useTypeahead> | null} = {api: null};
+ function Probe(): ReactNode {
+ latest.api = useTypeahead({
+ getItemLabels: () => LABELS,
+ onMatch: vi.fn(),
+ });
+ return null;
+ }
+ const {rerender} = render(
+ <Activity mode="visible">
+ <Probe />
+ </Activity>,
+ );
+ latest.api?.onKeyDown(key('a'));
+ rerender(
+ <Activity mode="hidden">
+ <Probe />
+ </Activity>,
+ );
+ expect(vi.getTimerCount()).toBe(0);
+ rerender(
+ <Activity mode="visible">
+ <Probe />
+ </Activity>,
+ );
+ // The typed prefix went with the hide, so a bare Space is not typeahead.
+ expect(latest.api?.onKeyDown(key(' '))).toBe(false);
+ });
+
it('skips disabled items', () => {
const {onMatch, api} = setup({disabledIndices: [0]}); // Apple disabled
api.onKeyDown(key('a'));
diff --git a/packages/core/src/hooks/useTypeahead.ts b/packages/core/src/hooks/useTypeahead.ts
index ee523bcf12..414fc686f2 100644
--- a/packages/core/src/hooks/useTypeahead.ts
+++ b/packages/core/src/hooks/useTypeahead.ts
@@ -4,7 +4,7 @@
/**
* @file useTypeahead.ts
- * @input Uses React useCallback/useRef
+ * @input Uses React useCallback/useEffect/useRef
* @output Exports useTypeahead hook for first-character (type-to-focus) search
* @position Core hook; adds APG typeahead to menus, listboxes, and other
* collections. Composes with useListFocus/useGridFocus (or any collection)
@@ -14,7 +14,7 @@
* - /packages/core/src/hooks/index.ts
*/
-import {useCallback, useRef} from 'react';
+import {useCallback, useEffect, useRef} from 'react';
export interface UseTypeaheadOptions {
/**
@@ -135,6 +135,11 @@ export function useTypeahead(options: UseTypeaheadOptions): UseTypeaheadReturn {
}
}, []);
+ // A collection that unmounts, or that an `<Activity>` boundary hides,
+ // mid-typing leaves no reset timer running and comes back with an empty
+ // buffer, as the reset would have left it.
+ useEffect(() => reset, [reset]);
+
const scheduleReset = useCallback(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);