An open source design system that's fully customizable and agent ready
TypeScript
fix(core): PageUp/PageDown jump to first/last option while searching (#4008)
Baraa Hamodid2e1d3834c6c
diff --git a/.changeset/hassearch-page-jump.md b/.changeset/hassearch-page-jump.mdnew file mode 100644index 0000000000..7a79575de9--- /dev/null+++ b/.changeset/hassearch-page-jump.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Selector/MultiSelector: PageUp/PageDown now jump the active option to the first/last match while searching, complementing Home/End which stay on text-caret movement.+@bhamodidiff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsxindex 83ae53f8d0..d1fe6cf18c 100644--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx@@ -360,6 +360,30 @@ describe('MultiSelector', () => {expect(activeId).toBeTruthy();});+ it('End/Home jump the highlight to the last/first option (non-search)', async () => {+ const user = userEvent.setup();+ render(+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={[]}+ onChange={() => {}}+ />,+ );++ const trigger = screen.getByRole('combobox');+ await user.click(trigger);+ const options = screen.getAllByRole('option', h);++ await user.keyboard('{End}');+ expect(trigger).toHaveAttribute(+ 'aria-activedescendant',+ options[options.length - 1].id,+ );+ await user.keyboard('{Home}');+ expect(trigger).toHaveAttribute('aria-activedescendant', options[0].id);+ });+it('toggles item with Enter key', async () => {const user = userEvent.setup();const onChange = vi.fn();@@ -535,6 +559,65 @@ describe('MultiSelector', () => {expect(options).toHaveLength(1);});+ it('PageDown/PageUp jump the highlight to the last/first filtered option', async () => {+ const user = userEvent.setup();+ render(+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={[]}+ onChange={() => {}}+ hasSearch+ />,+ );++ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const searchInput = screen.getByRole('combobox', h);+ // Filter to Banana and Orange so "last" means last *visible* option.+ await user.type(searchInput, 'an');+ const options = screen.getAllByRole('option', h);+ expect(options).toHaveLength(2);++ await user.keyboard('{PageDown}');+ expect(searchInput).toHaveAttribute(+ 'aria-activedescendant',+ options[options.length - 1].id,+ );+ await user.keyboard('{PageUp}');+ expect(searchInput).toHaveAttribute('aria-activedescendant', options[0].id);+ });++ it('Home/End move the search caret, not the option highlight', async () => {+ const user = userEvent.setup();+ render(+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={[]}+ onChange={() => {}}+ hasSearch+ />,+ );++ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const searchInput = screen.getByRole<HTMLInputElement>('combobox', h);+ await user.type(searchInput, 'an');+ expect(searchInput.selectionStart).toBe(2);+ const activeBefore = searchInput.getAttribute('aria-activedescendant');+ // Home/End stay on the input for caret movement (APG editable combobox);+ // the option highlight must not move.+ await user.keyboard('{Home}');+ expect(searchInput.selectionStart).toBe(0);+ expect(searchInput.getAttribute('aria-activedescendant')).toBe(+ activeBefore,+ );+ await user.keyboard('{End}');+ expect(searchInput.selectionStart).toBe(2);+ expect(searchInput.getAttribute('aria-activedescendant')).toBe(+ activeBefore,+ );+ });+it('shows empty state when search has no results', async () => {const user = userEvent.setup();render(diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsxindex 1cd072c155..d324862a27 100644--- a/packages/core/src/MultiSelector/MultiSelector.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.tsx@@ -1084,10 +1084,13 @@ export function MultiSelector<T extends MultiSelectorOptionType>({onKeyDown={e => {// Arrow keys navigate options; Enter toggles; Escape/Tab close.// Space and Home/End are left to the input (type a space / move- // the caret), not forwarded to option navigation.+ // the caret) per the APG editable combobox; PageUp/PageDown are+ // the sanctioned substitute for jumping to the first/last option.if (e.key === 'ArrowDown' ||e.key === 'ArrowUp' ||+ e.key === 'PageUp' ||+ e.key === 'PageDown' ||e.key === 'Enter' ||e.key === 'Escape' ||e.key === 'Tab'diff --git a/packages/core/src/MultiSelector/hooks.ts b/packages/core/src/MultiSelector/hooks.tsindex 3dc14e402d..64977c5f1f 100644--- a/packages/core/src/MultiSelector/hooks.ts+++ b/packages/core/src/MultiSelector/hooks.ts@@ -192,6 +192,23 @@ export function useMultiCombobox({}break;+ // PageUp/PageDown mirror Home/End. In search mode Home/End stay on+ // the input for caret movement (APG editable combobox), so these are+ // the sanctioned substitute for jumping to the first/last option.+ case 'PageUp':+ e.preventDefault();+ if (isOpen && enabledIndices.length > 0) {+ setHighlightedIndex(enabledIndices[0]);+ }+ break;++ case 'PageDown':+ e.preventDefault();+ if (isOpen && enabledIndices.length > 0) {+ setHighlightedIndex(enabledIndices[enabledIndices.length - 1]);+ }+ break;+case 'Delete':case 'Backspace':// Keyboard equivalent of the clear button (comboboxes-2): clear alldiff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex a281027389..88db03bd93 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -503,6 +503,58 @@ describe('Selector', () => {expect(search).toHaveAttribute('aria-activedescendant');});+ it('PageDown/PageUp jump the highlight to the last/first filtered option', async () => {+ const user = userEvent.setup();+ render(+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Apple"+ onChange={() => {}}+ hasSearch+ />,+ );+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const search = screen.getByRole('combobox', h);+ // Filter to Apple and Banana so "last" means last *visible* option.+ await user.type(search, 'a');+ const options = screen.getAllByRole('option', h);+ expect(options).toHaveLength(2);+ await user.keyboard('{PageDown}');+ expect(search).toHaveAttribute(+ 'aria-activedescendant',+ options[options.length - 1].id,+ );+ await user.keyboard('{PageUp}');+ expect(search).toHaveAttribute('aria-activedescendant', options[0].id);+ });++ it('Home/End move the search caret, not the option highlight', async () => {+ const user = userEvent.setup();+ render(+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Apple"+ onChange={() => {}}+ hasSearch+ />,+ );+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const search = screen.getByRole<HTMLInputElement>('combobox', h);+ await user.type(search, 'an');+ expect(search.selectionStart).toBe(2);+ const activeBefore = search.getAttribute('aria-activedescendant');+ // Home/End stay on the input for caret movement (APG editable+ // combobox); the option highlight must not move.+ await user.keyboard('{Home}');+ expect(search.selectionStart).toBe(0);+ expect(search.getAttribute('aria-activedescendant')).toBe(activeBefore);+ await user.keyboard('{End}');+ expect(search.selectionStart).toBe(2);+ expect(search.getAttribute('aria-activedescendant')).toBe(activeBefore);+ });+it('does not render search input when hasSearch is false', async () => {const user = userEvent.setup();render(@@ -722,6 +774,23 @@ describe('Selector', () => {expect(trigger).toHaveAttribute('aria-expanded', 'true');});+ it('End/Home jump the highlight to the last/first option (non-search)', async () => {+ const user = userEvent.setup();+ render(<Selector label="Fruit" options={OPTIONS} />);++ const trigger = screen.getByRole('combobox');+ await user.click(trigger);+ const options = screen.getAllByRole('option', h);++ await user.keyboard('{End}');+ expect(trigger).toHaveAttribute(+ 'aria-activedescendant',+ options[options.length - 1].id,+ );+ await user.keyboard('{Home}');+ expect(trigger).toHaveAttribute('aria-activedescendant', options[0].id);+ });+it('opens and selects an option with Enter (no mouse)', async () => {const user = userEvent.setup();const onChange = vi.fn();diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex e177ce34ad..47696fe1f4 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -858,10 +858,14 @@ export function Selector<T extends SelectorOptionType>(onChange={handleSearchChange}onKeyDown={e => {// Arrow keys navigate options; Enter selects; Escape/Tab close.- // Home/End are left to the input for caret movement.+ // Home/End are left to the input for caret movement (APG editable+ // combobox); PageUp/PageDown are the sanctioned substitute for+ // jumping to the first/last option.if (e.key === 'ArrowDown' ||e.key === 'ArrowUp' ||+ e.key === 'PageUp' ||+ e.key === 'PageDown' ||e.key === 'Enter' ||e.key === 'Escape' ||e.key === 'Tab'diff --git a/packages/core/src/Selector/hooks.ts b/packages/core/src/Selector/hooks.tsindex 5cf720911f..247ba8779a 100644--- a/packages/core/src/Selector/hooks.ts+++ b/packages/core/src/Selector/hooks.ts@@ -318,6 +318,23 @@ export function useCombobox({}break;+ // PageUp/PageDown mirror Home/End. In search mode Home/End stay on+ // the input for caret movement (APG editable combobox), so these are+ // the sanctioned substitute for jumping to the first/last option.+ case 'PageUp':+ e.preventDefault();+ if (isOpen && enabledIndices.length > 0) {+ setHighlightedIndex(enabledIndices[0]);+ }+ break;++ case 'PageDown':+ e.preventDefault();+ if (isOpen && enabledIndices.length > 0) {+ setHighlightedIndex(enabledIndices[enabledIndices.length - 1]);+ }+ break;+case 'Delete':case 'Backspace':// Keyboard equivalent of the clear button (comboboxes-2): clear the