Skip to content
Draft
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/typeahead-result-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[feat] `Typeahead`, and `Tokenizer` in its popover presentation, now show a result's `action`, its secondary control, such as an Edit button. Before, the control was dropped with a development warning. Once a result carries one, the list becomes a grid: each row has the result and, after it, the control. After the arrow keys reach a row, the arrow key toward the end of the row reaches the control, and Enter or a press runs it without selecting the result or closing the list. Tokenizer's bottom sheet, its presentation on compact touch screens, still shows the result without the control and warns in development. The "nothing matched" message is no longer exposed as a disabled option; screen readers still hear it.

@cixzhang
47 changes: 47 additions & 0 deletions apps/storybook/stories/Typeahead.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {expect, userEvent, waitFor, within} from 'storybook/test';
import {Typeahead, TypeaheadItem} from '@astryxdesign/core/Typeahead';
import type {SearchableItem, SearchSource} from '@astryxdesign/core/Typeahead';
import {MagnifyingGlassIcon} from '@heroicons/react/24/outline';
import {Icon} from '@astryxdesign/core/Icon';
import {IconButton} from '@astryxdesign/core/IconButton';

// Sample data
const fruits: SearchableItem[] = [
Expand Down Expand Up @@ -123,6 +125,51 @@ export const Default: Story = {
},
};

/**
* A result's secondary control (`action`). The list becomes a grid: the
* inline-end arrow reaches a row's Edit button, Enter or a press runs it, and
* the fruit stays unselected.
*/
export const ResultActions: Story = {
render: args => {
const [value, setValue] = useState<SearchableItem | null>(null);
const [lastEdited, setLastEdited] = useState<string | null>(null);
const source: SearchSource = {
search: query =>
fruits
.filter(f => f.label.toLowerCase().includes(query.toLowerCase()))
.map(fruit => ({
...fruit,
action: (
<IconButton
label={`Edit ${fruit.label}`}
icon={<Icon icon="moreHorizontal" />}
variant="ghost"
size="sm"
onClick={() => setLastEdited(fruit.label)}
/>
),
})),
bootstrap: () => [],
};
return (
<>
<Typeahead
{...args}
searchSource={source}
value={value}
onChange={setValue}
/>
<p>Last edited: {lastEdited ?? 'none'}</p>
</>
);
},
args: {
label: 'Fruit',
placeholder: 'Search fruits...',
},
};

export const WithBootstrap: Story = {
...Default,
args: {
Expand Down
22 changes: 11 additions & 11 deletions docs/specs/AST-058-option-actions/spec.md

Large diffs are not rendered by default.

89 changes: 44 additions & 45 deletions docs/specs/components/Typeahead/BaseTypeahead.spec.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/core/src/Tokenizer/TokenizerBottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,9 @@ function TokenizerSheetContent<T extends SearchableItem>({
}
}, [isOpen, isPending, renderedItems]);

// `action` is declared on `SearchableItem` for the grid-adopting hosts; the
// sheet rows have not adopted it, so a row renders the item without the
// control and says so (`spec:AST-058` FR9), as the inline panel does.
// The sheet rows have not adopted `SearchableItem.action` (the inline panel
// has), so a row renders the item without the control and says so
// (`spec:AST-058` FR9).
if (results.some(item => item.action != null)) {
warnOnce(
'tokenizer-sheet:item-action',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ for (const label of [
});
await page.clock.runFor(1000);
await expect(listbox).toBeVisible();
await expect(listbox.getByRole('option')).toHaveText('No results found');
// The message is shown but is not an option: a listbox owns only
// options, and the live region speaks the message.
await expect(listbox).toHaveText('No results found');
await expect(listbox.getByRole('option')).toHaveCount(0);
await page.clock.runFor(32);
await expect(
page.locator('[data-astryx-live-region="polite"]'),
Expand Down Expand Up @@ -116,13 +119,8 @@ for (const label of [
).toBeVisible();
}
} else {
await expect(listbox.getByRole('option')).toHaveText(
'No results found',
);
await expect(listbox.getByRole('option')).toHaveAttribute(
'aria-disabled',
'true',
);
await expect(listbox).toHaveText('No results found');
await expect(listbox.getByRole('option')).toHaveCount(0);
await expect(
page.locator('[data-astryx-live-region="polite"]'),
).toHaveText('No results found');
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/Typeahead/BaseTypeahead.incremental.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ function optionLabels() {
.map(option => option.textContent);
}

/** The "nothing matched" message: shown in the popup, never an option. */
function emptyMessage() {
return screen
.queryByRole('listbox', {hidden: true})
?.querySelector('[role="presentation"]')?.textContent;
}

function liveRegion() {
return document.querySelector('[data-astryx-live-region="polite"]');
}
Expand Down Expand Up @@ -262,7 +269,8 @@ describe('BaseTypeahead incremental results (spec:AST-092)', () => {
expect(input).toHaveAttribute('aria-busy', 'true');

await settle(latest(), []);
expect(optionLabels()).toEqual(['No results found']);
expect(optionLabels()).toEqual([]);
expect(emptyMessage()).toBe('No results found');
});

it('keeps the last non-empty list when the answer fails (FR9)', async () => {
Expand Down Expand Up @@ -330,7 +338,8 @@ describe('BaseTypeahead incremental results (spec:AST-092)', () => {
await act(async () => {
latest().reject(new Error('offline'));
});
expect(optionLabels()).toEqual(['No results found']);
expect(optionLabels()).toEqual([]);
expect(emptyMessage()).toBe('No results found');
});

it('warns in development when a later list moves shown rows, and shows it as given (FR2)', async () => {
Expand Down
Loading