Skip to content

Commit d8e0045

Browse files
fix: cannot clear initial combobox value in fully controlled component (#17324)
* fix: initial state issue * fix: added test case * fix: test case * fix: test case * fix: ci checks fail * fix: merge conflict issue --------- Co-authored-by: Taylor Jones <[email protected]>
1 parent 06844e9 commit d8e0045

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ describe('ComboBox', () => {
9090
});
9191
});
9292

93+
it('should call `onChange` on a fully controlled component', async () => {
94+
render(<ComboBox {...mockProps} selectedItem={mockProps.items[0]} />);
95+
await userEvent.click(screen.getAllByRole('button')[0]);
96+
expect(mockProps.onChange).toHaveBeenCalled();
97+
});
98+
9399
it('should select the correct item from the filtered list after text input on click', async () => {
94100
const user = userEvent.setup();
95101

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const {
5757
InputKeyDownArrowUp,
5858
InputKeyDownArrowDown,
5959
MenuMouseLeave,
60+
FunctionSelectItem,
6061
} = useCombobox.stateChangeTypes;
6162

6263
const defaultItemToString = <ItemType,>(item: ItemType | null) => {
@@ -521,6 +522,16 @@ const ComboBox = forwardRef(
521522
}
522523
return changes;
523524
}
525+
526+
case FunctionSelectItem:
527+
if (onChange) {
528+
onChange({
529+
selectedItem: changes.selectedItem,
530+
inputValue: changes.inputValue,
531+
});
532+
}
533+
return changes;
534+
524535
case InputKeyDownEnter:
525536
if (allowCustomValue) {
526537
setInputValue(inputValue);

0 commit comments

Comments
 (0)