Skip to content

fix: Ensure that combobox empty state is rendered when items arent directly provided to the combobox #8720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions packages/@react-spectrum/s2/src/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,13 @@ export const ComboBox = /*#__PURE__*/ (forwardRef as forwardRefType)(function Co
labelPosition = 'top',
UNSAFE_className = '',
UNSAFE_style,
loadingState,
...comboBoxProps
} = props;

return (
<AriaComboBox
{...comboBoxProps}
allowsEmptyCollection={loadingState != null}
allowsEmptyCollection
style={UNSAFE_style}
className={UNSAFE_className + style(field(), getAllowedOverrides())({
isInForm: !!formContext,
Expand Down
5 changes: 4 additions & 1 deletion packages/@react-spectrum/s2/test/Combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ describe('Combobox', () => {
comboboxTester.setInteractionType('mouse');
await comboboxTester.open();

expect(comboboxTester.options()).toHaveLength(1);
let options = comboboxTester.options();
expect(options).toHaveLength(1);
expect(comboboxTester.listbox).toBeTruthy();
expect(options[0]).toHaveTextContent('No results');
expect(within(comboboxTester.listbox!).getByTestId('loadMoreSentinel')).toBeInTheDocument();
});

Expand Down
14 changes: 9 additions & 5 deletions packages/@react-stately/list/src/ListCollection.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the ComboBox either uses defaultItems or has its items defined by the wrapped ListBox, useComboBoxState will return a ListCollection instead of a BaseCollection, resulting in a different way of calculating collection size. The below change makes sure the loaders aren't included in the keymap which should be fine since they aren't focusable. Ideally, we'd always use a BaseCollection though

Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ export class ListCollection<T> implements Collection<Node<T>> {
this.iterable = nodes;

let visit = (node: Node<T>) => {
this.keyMap.set(node.key, node);

if (node.childNodes && node.type === 'section') {
for (let child of node.childNodes) {
visit(child);
// Skip the loader node so it isn't added to the keymap and thus
// doesn't influence the size count. This should only matter for RAC and S2
if (node.type !== 'loader') {
this.keyMap.set(node.key, node);

if (node.childNodes && node.type === 'section') {
for (let child of node.childNodes) {
visit(child);
}
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/react-aria-components/docs/ComboBox.mdx
Copy link
Member Author

@LFDanLu LFDanLu Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opted to add renderEmptyState in the reusable example, but that means there is a slight item height increase when typing in the async example and it renders the emptyState since that example doesn't use MyItem. If we are fine with that example getting the red hover style I can change that too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it's fine personally

Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ interface MyComboBoxProps<T extends object> extends Omit<ComboBoxProps<T>, 'chil

function MyComboBox<T extends object>({label, description, errorMessage, children, ...props}: MyComboBoxProps<T>) {
return (
<ComboBox {...props}>
<ComboBox allowsEmptyCollection {...props}>
<Label>{label}</Label>
<div className="my-combobox-container">
<Input />
Expand All @@ -344,7 +344,7 @@ function MyComboBox<T extends object>({label, description, errorMessage, childre
{description && <Text slot="description">{description}</Text>}
<FieldError>{errorMessage}</FieldError>
<Popover>
<ListBox>
<ListBox renderEmptyState={() => <div className="my-item">No results found</div>}>
{children}
</ListBox>
</Popover>
Expand Down
4 changes: 3 additions & 1 deletion packages/react-aria-components/stories/ComboBox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ComboBoxStory = StoryFn<typeof ComboBox>;
export type ComboBoxStoryObj = StoryObj<typeof ComboBox>;

export const ComboBoxExample: ComboBoxStory = () => (
<ComboBox name="combo-box-example" data-testid="combo-box-example">
<ComboBox name="combo-box-example" data-testid="combo-box-example" allowsEmptyCollection>
<Label style={{display: 'block'}}>Test</Label>
<div style={{display: 'flex'}}>
<Input />
Expand All @@ -38,12 +38,14 @@ export const ComboBoxExample: ComboBoxStory = () => (
</div>
<Popover placement="bottom end">
<ListBox
renderEmptyState={renderEmptyState}
data-testid="combo-box-list-box"
className={styles.menu}>
<MyListBoxItem>Foo</MyListBoxItem>
<MyListBoxItem>Bar</MyListBoxItem>
<MyListBoxItem>Baz</MyListBoxItem>
<MyListBoxItem href="http://google.com">Google</MyListBoxItem>
<MyListBoxLoaderIndicator />
</ListBox>
</Popover>
</ComboBox>
Expand Down
28 changes: 26 additions & 2 deletions packages/react-aria-components/test/ComboBox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@
*/

import {act} from '@testing-library/react';
import {Button, ComboBox, ComboBoxContext, FieldError, Header, Input, Label, ListBox, ListBoxItem, ListBoxSection, ListLayout, Popover, Text, Virtualizer} from '../';
import {Button, ComboBox, ComboBoxContext, FieldError, Header, Input, Label, ListBox, ListBoxItem, ListBoxLoadMoreItem, ListBoxSection, ListLayout, Popover, Text, Virtualizer} from '../';
import {fireEvent, pointerMap, render, within} from '@react-spectrum/test-utils-internal';
import React from 'react';
import {User} from '@react-aria/test-utils';
import userEvent from '@testing-library/user-event';

let renderEmptyState = () => {
return (
<div>No results</div>
);
};

let TestComboBox = (props) => (
<ComboBox name="test-combobox" defaultInputValue="C" data-foo="bar" {...props}>
<Label>Favorite Animal</Label>
Expand All @@ -25,10 +31,13 @@ let TestComboBox = (props) => (
<Text slot="description">Description</Text>
<Text slot="errorMessage">Error</Text>
<Popover>
<ListBox>
<ListBox renderEmptyState={renderEmptyState}>
<ListBoxItem id="1">Cat</ListBoxItem>
<ListBoxItem id="2">Dog</ListBoxItem>
<ListBoxItem id="3">Kangaroo</ListBoxItem>
<ListBoxLoadMoreItem>
loading
</ListBoxLoadMoreItem>
</ListBox>
</Popover>
</ComboBox>
Expand Down Expand Up @@ -384,4 +393,19 @@ describe('ComboBox', () => {
let input = getByRole('combobox');
expect(input).toHaveAttribute('form', 'test');
});

it('should render empty state even when there is a loader provided and allowsEmptyCollection is true', async () => {
let tree = render(<TestComboBox allowsEmptyCollection />);

let comboboxTester = testUtilUser.createTester('ComboBox', {root: tree.container});
act(() => {
comboboxTester.combobox.focus();
});
await user.keyboard('p');

let options = comboboxTester.options();
expect(options).toHaveLength(1);
expect(comboboxTester.listbox).toBeTruthy();
expect(options[0]).toHaveTextContent('No results');
});
});