Skip to content

Commit a4a17c4

Browse files
authored
refactor: use isDisabledItem throughout ComboBox (#22009)
* refactor: use isDisabledItem throughout ComboBox * refactor: update test variable name
1 parent b43ddc5 commit a4a17c4

2 files changed

Lines changed: 31 additions & 13 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,33 @@ describe('ComboBox', () => {
493493
expect(screen.getByTestId('selected-item').textContent).toBe('Item 0');
494494
});
495495

496+
it('should skip disabled matches when pressing Enter with a partial input value', async () => {
497+
const onChange = jest.fn();
498+
const items = [
499+
{ id: 'ibm-cloud', text: 'IBM Cloud', disabled: true },
500+
{ id: 'ibm-quantum', text: 'IBM Quantum' },
501+
{ id: 'ibm-z', text: 'IBM Z' },
502+
];
503+
504+
render(
505+
<ComboBox
506+
id="test-combobox"
507+
items={items}
508+
itemToString={(item) => (item ? item.text : '')}
509+
onChange={onChange}
510+
/>
511+
);
512+
513+
await userEvent.click(findInputNode());
514+
await userEvent.type(findInputNode(), 'IBM ');
515+
await userEvent.keyboard('{Enter}');
516+
517+
expect(findInputNode()).toHaveDisplayValue('IBM Quantum');
518+
expect(onChange).toHaveBeenLastCalledWith({
519+
selectedItem: items[1],
520+
});
521+
});
522+
496523
it('should restore selected item label on blur when input does not match any item and a selection exists', async () => {
497524
render(
498525
<ComboBox

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ const findHighlightedIndex = <ItemType,>(
139139

140140
for (let i = 0; i < items.length; i++) {
141141
const item = itemToString(items[i]).toLowerCase();
142-
// TODO: Use `isDisabledItem`.
143-
if (!items[i]['disabled'] && item.indexOf(searchValue) !== -1) {
142+
if (!isDisabledItem(items[i]) && item.indexOf(searchValue) !== -1) {
144143
return i;
145144
}
146145
}
@@ -679,9 +678,7 @@ const ComboBox = forwardRef(
679678
);
680679
const highlightedItem = filteredList[state.highlightedIndex];
681680

682-
// TODO: Use `isDisabledItem`.
683-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452
684-
if (highlightedItem && !(highlightedItem as any).disabled) {
681+
if (highlightedItem && !isDisabledItem(highlightedItem)) {
685682
return {
686683
...changes,
687684
selectedItem: highlightedItem,
@@ -693,9 +690,7 @@ const ComboBox = forwardRef(
693690
if (autoIndex !== -1) {
694691
const matchingItem = items[autoIndex];
695692

696-
// TODO: Use `isDisabledItem`.
697-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452
698-
if (matchingItem && !(matchingItem as any).disabled) {
693+
if (matchingItem && !isDisabledItem(matchingItem)) {
699694
return {
700695
...changes,
701696
selectedItem: matchingItem,
@@ -883,11 +878,7 @@ const ComboBox = forwardRef(
883878
initialSelectedItem: initialSelectedItem,
884879
inputId: id,
885880
stateReducer,
886-
isItemDisabled(item) {
887-
// TODO: Use `isDisabledItem`.
888-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452
889-
return (item as any)?.disabled;
890-
},
881+
isItemDisabled: isDisabledItem,
891882
...downshiftProps,
892883
onStateChange: ({ type, selectedItem: newSelectedItem }) => {
893884
downshiftProps?.onStateChange?.({

0 commit comments

Comments
 (0)