Skip to content

Commit d9cc6a6

Browse files
authored
Prevent allowCustomValue ComboBox from removing selected key on blur if inputValue wasn't changed (#1541)
1 parent 87bbfd8 commit d9cc6a6

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/@react-spectrum/combobox/test/ComboBox.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,35 @@ describe('ComboBox', function () {
14631463
expect(() => getByRole('listbox')).toThrow();
14641464
});
14651465

1466+
it('retains selected key on blur if input value matches', function () {
1467+
let {getByRole} = render(
1468+
<Provider theme={theme}>
1469+
<ComboBox label="Test" allowsCustomValue selectedKey="2" onOpenChange={onOpenChange} onSelectionChange={onSelectionChange}>
1470+
<Item key="1">Bulbasaur</Item>
1471+
<Item key="2">Squirtle</Item>
1472+
<Item key="3">Charmander</Item>
1473+
</ComboBox>
1474+
</Provider>
1475+
);
1476+
1477+
let combobox = getByRole('combobox');
1478+
expect(onSelectionChange).not.toHaveBeenCalled();
1479+
1480+
act(() => {
1481+
userEvent.click(combobox);
1482+
jest.runAllTimers();
1483+
});
1484+
1485+
expect(document.activeElement).toBe(combobox);
1486+
1487+
act(() => {
1488+
combobox.blur();
1489+
jest.runAllTimers();
1490+
});
1491+
1492+
expect(onSelectionChange).not.toHaveBeenCalled();
1493+
});
1494+
14661495
it('clears the input field if value doesn\'t match a combobox option and no item is focused (menuTrigger=manual case)', function () {
14671496
let {getByRole, getAllByRole} = render(
14681497
<Provider theme={theme}>

packages/@react-stately/combobox/src/useComboBoxState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateProps<T>)
217217
open();
218218
}
219219
} else if (shouldCloseOnBlur) {
220-
if (allowsCustomValue) {
220+
let itemText = collection.getItem(selectedKey)?.textValue ?? '';
221+
if (allowsCustomValue && inputValue !== itemText) {
221222
commitCustomValue();
222223
} else {
223224
resetInputValue();

0 commit comments

Comments
 (0)