diff --git a/.changeset/fix-autocomplete-selection.md b/.changeset/fix-autocomplete-selection.md new file mode 100644 index 000000000..6abefe185 --- /dev/null +++ b/.changeset/fix-autocomplete-selection.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Fix autocomplete Enter key always selecting the first item and the first item not being highlighted on open. diff --git a/src/app/components/editor/autocomplete/AutocompleteMenu.test.tsx b/src/app/components/editor/autocomplete/AutocompleteMenu.test.tsx new file mode 100644 index 000000000..f0e2fd2f2 --- /dev/null +++ b/src/app/components/editor/autocomplete/AutocompleteMenu.test.tsx @@ -0,0 +1,193 @@ +import { type ReactNode } from 'react'; +import { describe, it, expect, vi } from 'vitest'; +import { render, act } from '@testing-library/react'; +import { + AutocompleteMenu, + AUTOCOMPLETE_NAVIGATE_EVENT, + type AutocompleteNavigateDetail, +} from './AutocompleteMenu'; + +// ── Mocks ───────────────────────────────────────────────────────────────────── + +vi.mock('focus-trap-react', () => ({ + default: ({ children }: { children: ReactNode }) => <>{children}, +})); + +vi.mock('folds', () => ({ + Header: ({ children }: { children: ReactNode }) =>
{children}
, + Menu: ({ children }: { children: ReactNode }) =>
{children}
, + Scroll: ({ children, onKeyDown }: { children: ReactNode; onKeyDown?: unknown }) => ( + // eslint-disable-next-line jsx-a11y/no-static-element-interactions +
{children}
+ ), + config: { space: { S200: '8px' } }, + Icons: {}, +})); + +vi.mock('$utils/keyboard', () => ({ + preventScrollWithArrowKey: vi.fn(), + stopPropagation: vi.fn(() => true), +})); + +vi.mock('$hooks/useAlive', () => ({ + useAlive: () => () => true, +})); + +// ── Helpers ─────────────────────────────────────────────────────────────────── + +function dispatchNavigate(container: HTMLElement, direction: 1 | -1) { + const menu = container.querySelector('[data-autocomplete-menu]'); + if (!menu) throw new Error('data-autocomplete-menu element not found'); + menu.dispatchEvent( + new CustomEvent(AUTOCOMPLETE_NAVIGATE_EVENT, { + bubbles: true, + detail: { direction }, + }) + ); +} + +function renderMenu(children: ReactNode) { + return render( + header} requestClose={() => {}}> + {children} + + ); +} + +// ── Tests ───────────────────────────────────────────────────────────────────── + +describe('AutocompleteMenu — data-selected stamping', () => { + it('stamps the first button with data-selected="true" on initial render', () => { + const { container } = renderMenu( + <> + + + + + ); + + const buttons = container.querySelectorAll('button'); + expect(buttons[0].getAttribute('data-selected')).toBe('true'); + expect(buttons[1].getAttribute('data-selected')).toBe('false'); + expect(buttons[2].getAttribute('data-selected')).toBe('false'); + }); + + it('stamps buttons when children load after initial render (async results)', async () => { + // Start with no children (empty search, buttons not yet present) + const { container, rerender } = renderMenu(null); + + // Simulate async search results arriving + await act(async () => { + rerender( + header} requestClose={() => {}}> + + + + ); + }); + + const buttons = container.querySelectorAll('button'); + expect(buttons).toHaveLength(2); + // First button must be marked selected — this was the bug: with the old + // [selectedIndex] dep array the effect didn't re-run when buttons appeared. + expect(buttons[0].getAttribute('data-selected')).toBe('true'); + expect(buttons[1].getAttribute('data-selected')).toBe('false'); + }); + + it('resets selection to the first button when the item list changes', async () => { + const { container } = renderMenu( + <> + + + + ); + + // Navigate to the second item + act(() => dispatchNavigate(container, 1)); + + const before = container.querySelectorAll('button'); + expect(before[1].getAttribute('data-selected')).toBe('true'); + + // Re-render with a completely different set of buttons (new search results) + await act(async () => { + render( + header} requestClose={() => {}}> + + + + , + { container: container.firstElementChild?.parentElement ?? document.body } + ); + }); + }); +}); + +describe('AutocompleteMenu — keyboard navigation', () => { + it('moves selection forward with direction +1', () => { + const { container } = renderMenu( + <> + + + + + ); + + act(() => dispatchNavigate(container, 1)); + + const buttons = container.querySelectorAll('button'); + expect(buttons[0].getAttribute('data-selected')).toBe('false'); + expect(buttons[1].getAttribute('data-selected')).toBe('true'); + expect(buttons[2].getAttribute('data-selected')).toBe('false'); + }); + + it('moves selection backward with direction -1', () => { + const { container } = renderMenu( + <> + + + + + ); + + // Go to last item first + act(() => dispatchNavigate(container, 1)); + act(() => dispatchNavigate(container, 1)); + + act(() => dispatchNavigate(container, -1)); + + const buttons = container.querySelectorAll('button'); + expect(buttons[0].getAttribute('data-selected')).toBe('false'); + expect(buttons[1].getAttribute('data-selected')).toBe('true'); + expect(buttons[2].getAttribute('data-selected')).toBe('false'); + }); + + it('clamps selection at the first item when navigating backward at index 0', () => { + const { container } = renderMenu( + <> + + + + ); + + act(() => dispatchNavigate(container, -1)); + + const buttons = container.querySelectorAll('button'); + expect(buttons[0].getAttribute('data-selected')).toBe('true'); + }); + + it('clamps selection at the last item when navigating forward past the end', () => { + const { container } = renderMenu( + <> + + + + ); + + act(() => dispatchNavigate(container, 1)); + act(() => dispatchNavigate(container, 1)); // attempt to go past the end + + const buttons = container.querySelectorAll('button'); + expect(buttons[0].getAttribute('data-selected')).toBe('false'); + expect(buttons[1].getAttribute('data-selected')).toBe('true'); + }); +}); diff --git a/src/app/components/editor/autocomplete/AutocompleteMenu.tsx b/src/app/components/editor/autocomplete/AutocompleteMenu.tsx index def55c2f6..9b907a8ac 100644 --- a/src/app/components/editor/autocomplete/AutocompleteMenu.tsx +++ b/src/app/components/editor/autocomplete/AutocompleteMenu.tsx @@ -31,6 +31,9 @@ export function AutocompleteMenu({ headerContent, requestClose, children }: Auto // Sync data-selected to DOM; reset to index 0 when the item list changes. // No dep array — runs after every render so newly-loaded buttons are stamped // immediately (buttons arrive async when search results load). + // setSelectedIndex is a stable React setter; the conditional call never + // triggers an infinite update chain (it only fires when button count changes). + // eslint-disable-next-line react-hooks/exhaustive-deps useLayoutEffect(() => { const buttons = Array.from( itemsRef.current?.querySelectorAll('button') ?? [] diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 840368124..41ef50a57 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -833,7 +833,7 @@ export const RoomInput = forwardRef( if (isKeyHotkey('enter', evt) && !isComposing(evt)) { const selectedItem = -autocompleteMenu.querySelector('button[data-selected="true"]') ?? + autocompleteMenu.querySelector('button[data-selected="true"]') ?? autocompleteMenu.querySelector('button'); if (selectedItem) {