Skip to content

Commit 9bf119b

Browse files
fix: Combobox VoiceOver announcement not respecting aria-label of ListBoxItem (adobe#8908)
* add ComboBox Story for ListBoxItem with aria-label This can be used to check whether the screen-reader announcement for option selections reads the correct label for each option, especially on mac, where there is a special codepath for this. * fix `CollectionBuilder` to handle `aria-label` correctly This commit ensures the aria-label prop from the ListItem is read into the corresponding attribute ElementNode * only add key if it exists --------- Co-authored-by: Robert Snow <rsnow@adobe.com> Co-authored-by: Robert Snow <snowystinger@gmail.com>
1 parent de7e167 commit 9bf119b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

packages/@react-aria/collections/src/Document.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ export class ElementNode<T> extends BaseNode<T> {
344344
node.rendered = rendered;
345345
node.render = render;
346346
node.value = value;
347+
if (obj['aria-label']) {
348+
node['aria-label'] = obj['aria-label'];
349+
}
347350
node.textValue = textValue || (typeof props.children === 'string' ? props.children : '') || obj['aria-label'] || '';
348351
if (id != null && id !== node.key) {
349352
throw new Error('Cannot change the id of an item');

packages/react-aria-components/stories/ComboBox.stories.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,25 @@ export function WithCreateOption() {
367367
</ComboBox>
368368
);
369369
}
370+
371+
export const ComboBoxListBoxItemWithAriaLabel: ComboBoxStory = () => (
372+
<ComboBox name="combo-box-example" data-testid="combo-box-example" allowsEmptyCollection>
373+
<Label style={{display: 'block'}}>Test</Label>
374+
<div style={{display: 'flex'}}>
375+
<Input />
376+
<Button>
377+
<span aria-hidden="true" style={{padding: '0 2px'}}></span>
378+
</Button>
379+
</div>
380+
<Popover placement="bottom end">
381+
<ListBox
382+
renderEmptyState={renderEmptyState}
383+
data-testid="combo-box-list-box"
384+
className={styles.menu}>
385+
<MyListBoxItem aria-label="Item Foo" textValue="Foo">Item <b>Foo</b></MyListBoxItem>
386+
<MyListBoxItem aria-label="Item Bar" textValue="Bar">Item <b>Bar</b></MyListBoxItem>
387+
<MyListBoxItem aria-label="Item Baz" textValue="Baz">Item <b>Baz</b></MyListBoxItem>
388+
</ListBox>
389+
</Popover>
390+
</ComboBox>
391+
);

0 commit comments

Comments
 (0)