Skip to content

Commit 6d4f463

Browse files
authored
fix ComboBox async loading runtime error (#5689)
1 parent 216cb7c commit 6d4f463

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/@react-aria/selection/src/ListKeyboardDelegate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class ListKeyboardDelegate<T> implements KeyboardDelegate {
170170
let key = this.collection.getFirstKey();
171171
while (key != null) {
172172
let item = this.collection.getItem(key);
173-
if (item.type === 'item' && !this.disabledKeys.has(key)) {
173+
if (item?.type === 'item' && !this.disabledKeys.has(key)) {
174174
return key;
175175
}
176176

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {Button, ComboBox, Input, Label, ListBox, Popover} from 'react-aria-compo
1414
import {MyListBoxItem} from './utils';
1515
import React from 'react';
1616
import styles from '../example/index.css';
17+
import {useAsyncList} from 'react-stately';
1718

1819
export default {
1920
title: 'React Aria Components'
@@ -139,3 +140,46 @@ export const ComboBoxRenderPropsListBoxDynamic = () => (
139140
)}
140141
</ComboBox>
141142
);
143+
144+
export const ComboBoxAsyncLoadingExample = () => {
145+
let list = useAsyncList<ComboBoxItem>({
146+
async load({filterText}) {
147+
let json = await new Promise(resolve => {
148+
setTimeout(() => {
149+
resolve(filterText ? items.filter(item => {
150+
let name = item.name.toLowerCase();
151+
for (let filterChar of filterText.toLowerCase()) {
152+
if (!name.includes(filterChar)) {
153+
return false;
154+
}
155+
name = name.replace(filterChar, '');
156+
}
157+
return true;
158+
}) : items);
159+
}, 300);
160+
}) as ComboBoxItem[];
161+
162+
return {
163+
items: json
164+
};
165+
}
166+
});
167+
168+
return (
169+
<ComboBox items={list.items} inputValue={list.filterText} onInputChange={list.setFilterText}>
170+
<Label style={{display: 'block'}}>Test</Label>
171+
<div style={{display: 'flex'}}>
172+
<Input />
173+
<Button>
174+
<span aria-hidden="true" style={{padding: '0 2px'}}></span>
175+
</Button>
176+
</div>
177+
<Popover placement="bottom end">
178+
<ListBox<ComboBoxItem>
179+
className={styles.menu}>
180+
{item => <MyListBoxItem>{item.name}</MyListBoxItem>}
181+
</ListBox>
182+
</Popover>
183+
</ComboBox>
184+
);
185+
};

0 commit comments

Comments
 (0)