Skip to content

Commit 856a70a

Browse files
authored
fix: onChange calls reduced to 1 when interacting with keyboard (#18435)
* fix: added highlightindex check on conditions * fix: removed storybook changes * Update ComboBox.stories.js
1 parent facb207 commit 856a70a

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const ControlledComboBox = ({ controlledItem }) => {
3333
const [onChangeCallCount, setOnChangeCallCount] = useState(0);
3434
const controlledOnChange = ({ selectedItem }) => {
3535
setValue(selectedItem);
36-
setOnChangeCallCount(onChangeCallCount + 1);
36+
setOnChangeCallCount((prevCount) => prevCount + 1);
3737
};
3838

3939
return (

packages/react/src/components/ComboBox/ComboBox.stories.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import React, { useState, useRef } from 'react';
99

1010
import { WithLayer } from '../../../.storybook/templates/WithLayer';
11-
1211
import ComboBox from '../ComboBox';
1312
import Button from '../Button';
1413
import { AILabel, AILabelContent, AILabelActions } from '../AILabel';

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,13 @@ const ComboBox = forwardRef(
969969
// Since `onChange` does not normally fire when the menu is closed, we should
970970
// manually fire it when `allowCustomValue` is provided, the menu is closing,
971971
// and there is a value.
972-
if (allowCustomValue && isOpen && inputValue) {
973-
onChange({ selectedItem, inputValue });
972+
if (
973+
allowCustomValue &&
974+
isOpen &&
975+
inputValue &&
976+
highlightedIndex === -1
977+
) {
978+
onChange({ selectedItem: null, inputValue });
974979
}
975980

976981
event.preventDownshiftDefault = true;

0 commit comments

Comments
 (0)