Skip to content

Commit 1cbf47d

Browse files
fix: ensure contains is used as default filter in ComboBox (#9234)
* fix: ensure contains is used as default filter in ComboBox * add test --------- Co-authored-by: Robert Snow <[email protected]> Co-authored-by: Robert Snow <[email protected]>
1 parent 8f18b4b commit 1cbf47d

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ function ComboBoxInner<T extends object>({props, collection, comboBoxRef: ref}:
133133
let validationBehavior = props.validationBehavior ?? formValidationBehavior ?? 'native';
134134
let {contains} = useFilter({sensitivity: 'base'});
135135
let state = useComboBoxState({
136-
defaultFilter: props.defaultFilter || contains,
137136
...props,
137+
defaultFilter: props.defaultFilter || contains,
138138
// If props.items isn't provided, rely on collection filtering (aka listbox.items is provided or defaultItems provided to Combobox)
139139
items: props.items,
140140
children: undefined,

packages/react-aria-components/test/ComboBox.test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,44 @@ describe('ComboBox', () => {
147147
expect(options).toHaveLength(1);
148148
});
149149

150+
it('should support undefined defaultFilter', async () => {
151+
let tree = render(
152+
<ComboBox defaultFilter={undefined}>
153+
<Label>Preferred fruit or vegetable</Label>
154+
<Input />
155+
<Button />
156+
<Popover>
157+
<ListBox>
158+
<ListBoxSection>
159+
<Header>Fruit</Header>
160+
<ListBoxItem id="Apple">Apple</ListBoxItem>
161+
<ListBoxItem id="Banana">Banana</ListBoxItem>
162+
</ListBoxSection>
163+
<ListBoxSection>
164+
<Header>Vegetable</Header>
165+
<ListBoxItem id="Cabbage">Cabbage</ListBoxItem>
166+
<ListBoxItem id="Broccoli">Broccoli</ListBoxItem>
167+
</ListBoxSection>
168+
</ListBox>
169+
</Popover>
170+
</ComboBox>
171+
);
172+
173+
let comboboxTester = testUtilUser.createTester('ComboBox', {root: tree.container});
174+
act(() => {
175+
comboboxTester.combobox.focus();
176+
});
177+
await user.keyboard('p');
178+
179+
let groups = comboboxTester.sections;
180+
expect(groups).toHaveLength(1);
181+
expect(groups[0]).toHaveAttribute('aria-labelledby');
182+
expect(document.getElementById(groups[0].getAttribute('aria-labelledby'))).toHaveTextContent('Fruit');
183+
184+
let options = within(groups[0]).getAllByRole('option');
185+
expect(options).toHaveLength(1);
186+
});
187+
150188
it('should support dynamic collections', async () => {
151189
let defaultItems = [
152190
{id: 1, name: 'Cat'},
@@ -413,7 +451,7 @@ describe('ComboBox', () => {
413451
let onAction = jest.fn();
414452
function WithCreateOption() {
415453
let [inputValue, setInputValue] = useState('');
416-
454+
417455
return (
418456
<ComboBox
419457
allowsEmptyCollection
@@ -464,7 +502,7 @@ describe('ComboBox', () => {
464502
}
465503
expect(onAction).toHaveBeenCalledTimes(1);
466504
expect(comboboxTester.combobox).toHaveValue('');
467-
505+
468506
// Repeat with an option selected.
469507
await comboboxTester.selectOption({option: 'Cat'});
470508

0 commit comments

Comments
 (0)