Skip to content

fix: onChange calls reduced to 1 when interacting with keyboard #18435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react/src/components/ComboBox/ComboBox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const ControlledComboBox = ({ controlledItem }) => {
const [onChangeCallCount, setOnChangeCallCount] = useState(0);
const controlledOnChange = ({ selectedItem }) => {
setValue(selectedItem);
setOnChangeCallCount(onChangeCallCount + 1);
setOnChangeCallCount((prevCount) => prevCount + 1);
};

return (
Expand Down
30 changes: 29 additions & 1 deletion packages/react/src/components/ComboBox/ComboBox.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React, { useState, useRef } from 'react';

import { WithLayer } from '../../../.storybook/templates/WithLayer';

import { generateItems, generateGenericItem } from '../ListBox/test-helpers';
import ComboBox from '../ComboBox';
import Button from '../Button';
import { AILabel, AILabelContent, AILabelActions } from '../AILabel';
Expand Down Expand Up @@ -379,6 +379,34 @@ export const _fullyControlled = (args) => {
);
};

export const Test = ({ controlledItem }) => {
const items = generateItems(5, generateGenericItem);
const [value, setValue] = useState(controlledItem || items[0]);
const [onChangeCallCount, setOnChangeCallCount] = useState(0);
const controlledOnChange = ({ selectedItem, inputValue }) => {
console.log('selected item: ', selectedItem);
console.log(inputValue);
setValue(selectedItem);
setOnChangeCallCount((prevCount) => prevCount + 1);
};

return (
<div>
<ComboBox
id="test-combobox"
items={items}
selectedItem={value}
allowCustomValue
onChange={controlledOnChange}
placeholder="Filter..."
type="default"
/>
<div>value: {value?.label || 'none'}</div>
<div>onChangeCallCount: {onChangeCallCount}</div>
</div>
);
};

_fullyControlled.argTypes = { ...sharedArgTypes };

AutocompleteWithTypeahead.argTypes = {
Expand Down
9 changes: 7 additions & 2 deletions packages/react/src/components/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,13 @@ const ComboBox = forwardRef(
// Since `onChange` does not normally fire when the menu is closed, we should
// manually fire it when `allowCustomValue` is provided, the menu is closing,
// and there is a value.
if (allowCustomValue && isOpen && inputValue) {
onChange({ selectedItem, inputValue });
if (
allowCustomValue &&
isOpen &&
inputValue &&
highlightedIndex === -1
) {
onChange({ selectedItem: null, inputValue });
}

event.preventDownshiftDefault = true;
Expand Down
Loading