Skip to content

Commit 83f5f87

Browse files
authored
fix: restore initial controlled ComboBox selection (#21999)
1 parent 6ff1b5f commit 83f5f87

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,24 @@ describe('ComboBox', () => {
538538
expect(findInputNode()).toHaveDisplayValue('Item 1');
539539
});
540540

541+
it('should restore controlled selected item label on blur when input does not match any item on initial load', async () => {
542+
render(
543+
<ComboBox
544+
{...mockProps}
545+
selectedItem={mockProps.items[1]}
546+
allowCustomValue={false}
547+
/>
548+
);
549+
550+
expect(findInputNode()).toHaveDisplayValue('Item 1');
551+
552+
await userEvent.clear(findInputNode());
553+
await userEvent.type(findInputNode(), 'no-match');
554+
await userEvent.keyboard('[Tab]');
555+
556+
expect(findInputNode()).toHaveDisplayValue('Item 1');
557+
});
558+
541559
it('should keep exact match input on blur when it matches an item label', async () => {
542560
render(<ComboBox {...mockProps} allowCustomValue={false} />);
543561

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,12 @@ const ComboBox = forwardRef(
656656
items.some((item) => itemToString(item) === currentInput);
657657

658658
if (!hasExactMatch) {
659+
const selectedItem =
660+
typeof selectedItemProp !== 'undefined'
661+
? selectedItemProp
662+
: state.selectedItem;
659663
const restoredInput =
660-
state.selectedItem !== null
661-
? itemToString(state.selectedItem)
662-
: '';
664+
selectedItem !== null ? itemToString(selectedItem) : '';
663665

664666
return { ...changes, inputValue: restoredInput };
665667
}
@@ -724,10 +726,12 @@ const ComboBox = forwardRef(
724726
items.some((item) => itemToString(item) === currentInput);
725727

726728
if (!hasExactMatch) {
729+
const selectedItem =
730+
typeof selectedItemProp !== 'undefined'
731+
? selectedItemProp
732+
: state.selectedItem;
727733
const restoredInput =
728-
state.selectedItem !== null
729-
? itemToString(state.selectedItem)
730-
: '';
734+
selectedItem !== null ? itemToString(selectedItem) : '';
731735

732736
return { ...changes, inputValue: restoredInput };
733737
}

0 commit comments

Comments
 (0)