Skip to content
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

fix: combobox not showing correct highlight #16741

Merged
Show file tree
Hide file tree
Changes from all 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
51 changes: 48 additions & 3 deletions packages/react/src/components/ComboBox/ComboBox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React from 'react';
import { render, screen, within } from '@testing-library/react';
import { render, screen, within, fireEvent, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {
findListBoxNode,
Expand All @@ -17,8 +17,6 @@ import {
waitForPosition,
} from '../ListBox/test-helpers';
import ComboBox from '../ComboBox';
import { act } from 'react';

import { Slug } from '../Slug';

const findInputNode = () => screen.getByRole('combobox');
Expand Down Expand Up @@ -312,4 +310,51 @@ describe('ComboBox', () => {
assertMenuClosed(mockProps);
});
});

describe('Highlights', () => {
it('should highlight the matched element', async () => {
render(<ComboBox {...mockProps} allowCustomValue={false} />);
await userEvent.type(findInputNode(), '1');
expect(screen.getAllByRole('option')[1]).toHaveClass(
'cds--list-box__menu-item--highlighted'
);
});

it('should highlight the selected element', async () => {
render(<ComboBox {...mockProps} allowCustomValue={false} />);
await openMenu();
await userEvent.type(findInputNode(), 'Item 1');
await userEvent.keyboard('[Enter]');
await openMenu();
expect(screen.getAllByRole('option')[1]).toHaveClass(
'cds--list-box__menu-item--highlighted'
);
});

it('should highlight the selected element if user enter some other value click outside of combobox', async () => {
render(<ComboBox {...mockProps} allowCustomValue={false} />);
await openMenu();
await userEvent.type(findInputNode(), 'Item 1');
await userEvent.keyboard('[Enter]');
await openMenu();
expect(screen.getAllByRole('option')[1]).toHaveClass(
'cds--list-box__menu-item--highlighted'
);

await userEvent.clear(findInputNode());
await userEvent.type(findInputNode(), 'Item');
//should match the loosely match element
expect(screen.getAllByRole('option')[0]).toHaveClass(
'cds--list-box__menu-item--highlighted'
);

fireEvent.blur(findInputNode());
await openMenu();
// on blur, it should retain the selected value
expect(findInputNode()).toHaveDisplayValue('Item 1');
expect(screen.getAllByRole('option')[1]).toHaveClass(
'cds--list-box__menu-item--highlighted'
);
});
});
});
15 changes: 13 additions & 2 deletions packages/react/src/components/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,22 @@ const ComboBox = forwardRef(
if (
state.inputValue &&
highlightedIndex == '-1' &&
!allowCustomValue
changes.selectedItem
) {
return {
...changes,
inputValue: itemToString(changes.selectedItem),
};
} else if (
state.inputValue &&
highlightedIndex == '-1' &&
!allowCustomValue &&
!changes.selectedItem
) {
return { ...changes, inputValue: '' };
} else {
return changes;
}
return changes;
case InputKeyDownEnter:
if (allowCustomValue) {
setInputValue(inputValue);
Expand Down
Loading