Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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-disabled-result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[fix] Typeahead: a result rendered with `TypeaheadItem` and `isDisabled` is now announced as disabled, and Enter, click or tap on it picks nothing and leaves the list open, also inside a shadow root. The pointer does not highlight it; arrow keys still reach it.

@cixzhang
19 changes: 11 additions & 8 deletions docs/specs/components/Typeahead/TypeaheadItem.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ review_triggers: [public-api, theming]
verified_by:
[
packages/core/src/Typeahead/TypeaheadItem.test.tsx,
packages/core/src/Typeahead/BaseTypeahead.test.tsx,
apps/storybook/rtl-audit/targets.json,
]
modules: []
Expand All @@ -31,11 +32,11 @@ system_specs: []
| Area | Contract |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Public contract | The default Typeahead result content: `item` (required), `icon`, `description`, `isDisabled`, `group`, root inputs, and a ref. |
| Behavior | Renders an optional icon, the item's label, and an optional description under the `typeahead-item` target; dims when disabled; renders `item.element` raw instead when the item carries one. |
| Behavior | Renders an icon, the item's label, and a description under the `typeahead-item` target; dims when disabled and marks its option disabled; renders `item.element` raw when the item sets one. |
| End-user impact | People see each result's label, icon, and supporting text in one consistent row layout. |
| Builder impact | Builders reuse it inside a custom `renderItem` to keep the default layout and target. |
| Compatibility/readiness | Records shipped behavior; no API, default, or DOM change. Root inputs dropped on the `item.element` path are listed under Current-state impact. |
| Review checks | Reject a lost `typeahead-item` target, a description element when none is given, or option semantics added to this content. |
| Compatibility/readiness | A disabled item marks its option `aria-disabled` and is never picked; no API or other DOM change. Root inputs dropped on the `item.element` path are listed under Current-state impact. |
| Review checks | Reject a lost `typeahead-item` target, a description element when none is given, or option semantics added to this content beyond the disabled mark. |
| Governing rules | `architecture:public-component-api` INV5, INV6; `architecture:component-theming-surface`; `component:BaseTypeahead` for the option row around it. |

This table is a review projection; the body below is authoritative.
Expand Down Expand Up @@ -82,7 +83,7 @@ Consumer migration instructions belong in consumer docs and release notes.
| ID | Invariant | Basis |
| --- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------ |
| FR1 | Without `item.element`, the content renders `icon` before a column of the item's `label` and, when given, `description`; no description element renders without one. | Current source and tests |
| FR2 | The root carries the `typeahead-item` target; `isDisabled` dims it to half opacity. | `architecture:component-theming-surface`; current source and tests |
| FR2 | The root carries the `typeahead-item` target; `isDisabled` dims it to half opacity, marks its option `aria-disabled`, and the option is never picked; a custom `renderItem` that doesn't render TypeaheadItem has no disabled state. | `architecture:component-theming-surface`; current source and tests |
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
| FR3 | Root inputs reach the root: `className` merges with the component's classes, `style` and `xstyle` compose, other props pass through, and the ref reaches the root element. | `architecture:public-component-api`; current tests |
| FR4 | With `item.element`, that element renders raw in place of the layout. Caller root inputs MUST still reach the rendered root (`architecture:public-component-api` INV5, INV6). Implementation gap: on this path the ref, `className`, `style`, `xstyle`, and other props are dropped. | `architecture:public-component-api` INV5, INV6; current source and tests |

Expand Down Expand Up @@ -111,8 +112,10 @@ Consumer migration instructions belong in consumer docs and release notes.

## Accessibility contract

- **AR1 — Content only.** TypeaheadItem adds no role or state; the option row
around it owns them.
- **AR1 — Content, plus the disabled mark.** TypeaheadItem adds no role; the
option row around it owns its role and state. The one exception is
`isDisabled`: it marks that option `aria-disabled`, so the option is
announced as disabled, and the host reads the mark at pick time.

## Design relationships

Expand All @@ -137,10 +140,10 @@ carries no anatomy-theming map.
| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------ |
| FR1 | `TypeaheadItem.test.tsx` "renders the item label", "renders the description below the label when provided", "renders no description element when none is provided", "renders the icon before the label content" | Label, description, icon | A missing label, description, or icon, an extra description element, or the icon after the label fails. | `audit:TypeaheadItem/anatomy` |
| FR2 | `TypeaheadItem.test.tsx` "dims the row when isDisabled is set" and "does not dim the row by default" | Enabled and disabled | Missing or default dimming fails. Evidence gap: no test asserts the `typeahead-item` class. | `audit:TypeaheadItem/theming` |
| FR2 | `TypeaheadItem.test.tsx` "dims the row when isDisabled is set" and "does not dim the row by default"; `BaseTypeahead.test.tsx` "a disabled TypeaheadItem result" cases | Enabled and disabled | Missing or default dimming fails. Evidence gap: no test asserts the `typeahead-item` class. | `audit:TypeaheadItem/theming` |
| FR3 | `TypeaheadItem.test.tsx` "forwards pass-through props to the item element", "merges a caller className with its own classes", "forwards ref to the row element" | Pass-through, className, ref | A dropped prop, class, or ref fails. Evidence gap: no test asserts `style` or `xstyle`. | `audit:TypeaheadItem/api` |
| FR4, ORD1 | `TypeaheadItem.test.tsx` "renders item.element raw in place of the label layout" | `item.element` | An element that keeps the default layout fails. Evidence gap: no test asserts the root inputs on this path (the FR4 gap). | `audit:TypeaheadItem/api` |
| PR1, AR1 | Evidence gap: no test asserts the absence of lifecycle resources or roles | — | — | `audit:TypeaheadItem/behavior` |
| PR1, AR1 | PR1: evidence gap, no test of lifecycle resources. AR1: the FR2 row's `BaseTypeahead.test.tsx` cases | — | — | `audit:TypeaheadItem/behavior` |
| RTL relation (FR1) | RTL audit curated D2 target (`apps/storybook/rtl-audit/targets.json`) on `Typeahead / Parts / TypeaheadItem with icon` (results opened) | Icon, label and description in an open result; LTR and RTL | A result icon that is not at the inline start of its content fails D2. | `audit:TypeaheadItem/i18n-rtl` |

## Current-state impact
Expand Down
174 changes: 174 additions & 0 deletions packages/core/src/Typeahead/BaseTypeahead.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import * as stylex from '@stylexjs/stylex';
import {BaseTypeahead, type BaseTypeaheadHandle} from './BaseTypeahead';
import {CommittedChoicesKeyContext} from './committedChoicesKey';
import {Typeahead} from './Typeahead';
import {TypeaheadItem} from './TypeaheadItem';
import {Tokenizer} from '../Tokenizer';
import {InternationalizationProvider} from '../i18n';
import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';
Expand Down Expand Up @@ -1754,3 +1755,176 @@ describe('BaseTypeahead pending work after a dismissal', () => {
}
});
});

describe('a disabled TypeaheadItem result', () => {
const people: SearchSource<SearchableItem> = {
search: () => [
{id: 'ana', label: 'Ana'},
{id: 'ben', label: 'Ben'},
{id: 'cy', label: 'Cy'},
],
bootstrap: () => [],
};
function People({
disabledId,
onChange,
}: {
disabledId: string | null;
onChange: (item: SearchableItem | null) => void;
}) {
return (
<Typeahead
label="Add people"
searchSource={people}
value={null}
onChange={onChange}
debounceMs={0}
renderItem={item => (
<TypeaheadItem item={item} isDisabled={item.id === disabledId} />
)}
/>
);
}

it('is announced as disabled, and Enter, click and tap pick nothing and keep the list open', async () => {
const user = userEvent.setup();
const onChange = vi.fn();
render(<People disabledId="ben" onChange={onChange} />);
await user.type(screen.getByRole('combobox'), 'a');
const ben = await screen.findByRole('option', {name: 'Ben', hidden: true});
expect(ben).toHaveAttribute('aria-disabled', 'true');
expect(
screen.getByRole('option', {name: 'Ana', hidden: true}),
).not.toHaveAttribute('aria-disabled');

await user.click(ben);
fireEvent.pointerDown(ben, {pointerType: 'touch'});
fireEvent.click(ben);
expect(onChange).not.toHaveBeenCalled();
expect(screen.getByRole('combobox')).toHaveAttribute(
'aria-expanded',
'true',
);

// Arrow keys still reach it; Enter on it picks nothing.
const combobox = screen.getByRole('combobox');
for (
let step = 0;
step < 3 && combobox.getAttribute('aria-activedescendant') !== ben.id;
step++
) {
await user.keyboard('{ArrowDown}');
}
expect(combobox).toHaveAttribute('aria-activedescendant', ben.id);
await user.keyboard('{Enter}');
expect(onChange).not.toHaveBeenCalled();
expect(screen.getByRole('combobox')).toHaveAttribute(
'aria-expanded',
'true',
);

// The next result is still reached and picked.
await user.keyboard('{ArrowDown}{Enter}');
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.mock.calls[0][0]).toMatchObject({id: 'cy'});
});

it('takes no hover highlight', async () => {
const user = userEvent.setup();
render(<People disabledId="ben" onChange={() => {}} />);
const combobox = screen.getByRole('combobox');
await user.type(combobox, 'a');
const ben = await screen.findByRole('option', {name: 'Ben', hidden: true});
const cy = screen.getByRole('option', {name: 'Cy', hidden: true});
// A real pointer move releases the hover hold, then each row the pointer
// moves over takes the highlight.
const moveOver = (option: HTMLElement, y: number) => {
fireEvent.pointerMove(option, {screenX: 10, screenY: y});
fireEvent.mouseMove(option, {screenX: 10, screenY: y});
};
moveOver(cy, 10);
moveOver(cy, 20);
expect(combobox).toHaveAttribute('aria-activedescendant', cy.id);
moveOver(ben, 30);
expect(combobox).toHaveAttribute('aria-activedescendant', cy.id);
});

it('reads the mark from the row it acts on when two results share an id', async () => {
const user = userEvent.setup();
const onChange = vi.fn();
const twins: SearchSource<SearchableItem> = {
search: () => [
{id: 'ben', label: 'Ben A'},
{id: 'ben', label: 'Ben B'},
],
bootstrap: () => [],
};
render(
<Typeahead
label="Add people"
searchSource={twins}
value={null}
onChange={onChange}
debounceMs={0}
renderItem={item => (
<TypeaheadItem item={item} isDisabled={item.label === 'Ben B'} />
)}
/>,
);
await user.type(screen.getByRole('combobox'), 'b');
const second = await screen.findByRole('option', {
name: 'Ben B',
hidden: true,
});
await user.click(second);
expect(onChange).not.toHaveBeenCalled();
await user.click(screen.getByRole('option', {name: 'Ben A', hidden: true}));
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.mock.calls[0][0]).toMatchObject({label: 'Ben A'});
});

it('is not picked inside a shadow root', async () => {
const onChange = vi.fn();
const host = document.createElement('div');
document.body.append(host);
const mount = document.createElement('div');
host.attachShadow({mode: 'open'}).append(mount);
try {
render(<People disabledId="ben" onChange={onChange} />, {
container: mount,
});
const field = within(mount);
const combobox = field.getByRole('combobox');
// user-event types into the document's active element, which is the
// shadow host here, so the input event goes to the field directly.
act(() => {
combobox.focus();
});
fireEvent.change(combobox, {target: {value: 'a'}});
const ben = await field.findByRole('option', {name: 'Ben', hidden: true});
expect(ben).toHaveAttribute('aria-disabled', 'true');
fireEvent.click(ben);
expect(onChange).not.toHaveBeenCalled();
fireEvent.click(field.getByRole('option', {name: 'Ana', hidden: true}));
expect(onChange).toHaveBeenCalledTimes(1);
} finally {
host.remove();
}
});

it('is picked again once it is no longer disabled', async () => {
const user = userEvent.setup();
const onChange = vi.fn();
const {rerender} = render(<People disabledId="ben" onChange={onChange} />);
await user.type(screen.getByRole('combobox'), 'a');
const ben = await screen.findByRole('option', {name: 'Ben', hidden: true});
expect(ben).toHaveAttribute('aria-disabled', 'true');
rerender(<People disabledId={null} onChange={onChange} />);
expect(
screen.getByRole('option', {name: 'Ben', hidden: true}),
).not.toHaveAttribute('aria-disabled');
await user.click(screen.getByRole('option', {name: 'Ben', hidden: true}));
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.mock.calls[0][0]).toMatchObject({id: 'ben'});
});
});
Loading