Skip to content

Commit 2018d25

Browse files
enricobguedesebgguidari
authored
fix(17119): combobox not displaying selected option once search query is cleared (#18498)
* fix(17119): combobox not displaying option * test: include unit testing * test: update AVT tests. * fix: changes request + avt testing fix * test: update testcases * test: import findMenuItemNode * test: remove console.log --------- Co-authored-by: ebg <ebg@ibm.com> Co-authored-by: Guilherme Datilio Ribeiro <guilhermedatilio@gmail.com>
1 parent 10ecdd3 commit 2018d25

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,15 @@
17631763
"a11y",
17641764
"review"
17651765
]
1766+
},
1767+
{
1768+
"login": "enricobguedes",
1769+
"name": "enricobguedes",
1770+
"avatar_url": "https://avatars.githubusercontent.com/u/45374536?v=4",
1771+
"profile": "https://github.com/enricobguedes",
1772+
"contributions": [
1773+
"code"
1774+
]
17661775
}
17671776
],
17681777
"commitConvention": "none"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
325325
<td align="center"><a href="https://github.com/a88zach"><img src="https://avatars.githubusercontent.com/u/1724822?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Zach Tindall</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=a88zach" title="Code">💻</a></td>
326326
<td align="center"><a href="https://github.com/vsvsv"><img src="https://avatars.githubusercontent.com/u/9214692?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vsevolod Platunov</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=vsvsv" title="Code">💻</a></td>
327327
<td align="center"><a href="https://github.com/heloiselui"><img src="https://avatars.githubusercontent.com/u/71858203?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Heloise Lui</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=heloiselui" title="Code">💻</a> <a href="https://github.com/carbon-design-system/carbon/commits?author=heloiselui" title="Documentation">📖</a> <a href="#a11y-heloiselui" title="Accessibility">️️️️♿️</a> <a href="https://github.com/carbon-design-system/carbon/pulls?q=is%3Apr+reviewed-by%3Aheloiselui" title="Reviewed Pull Requests">👀</a></td>
328+
<td align="center"><a href="https://github.com/enricobguedes"><img src="https://avatars.githubusercontent.com/u/45374536?v=4?s=100" width="100px;" alt=""/><br /><sub><b>enricobguedes</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=enricobguedes" title="Code">💻</a></td>
328329
</tr>
329330
</table>
330331

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
generateGenericItem,
1717
cognateItems,
1818
waitForPosition,
19+
findMenuItemNode,
1920
} from '../ListBox/test-helpers';
2021
import ComboBox from '../ComboBox';
2122
import { AILabel } from '../AILabel';
@@ -817,5 +818,50 @@ describe('ComboBox', () => {
817818

818819
expect(input).toHaveDisplayValue('Apple');
819820
});
821+
822+
it('should remove input and enter new conditions', async () => {
823+
const user = userEvent.setup();
824+
render(<ComboBox {...mockProps} typeahead />);
825+
826+
const input = screen.getByRole('combobox');
827+
user.click(input);
828+
829+
await user.keyboard('[Enter]');
830+
831+
expect(input).toHaveDisplayValue('');
832+
});
833+
834+
it('should open the menu and select null when Enter is pressed with no input and no highlighted item', async () => {
835+
const onInputChange = jest.fn();
836+
837+
render(<ComboBox {...mockProps} onInputChange={onInputChange} />);
838+
839+
await userEvent.type(findInputNode(), 'apple');
840+
expect(findInputNode()).toHaveDisplayValue('apple');
841+
await userEvent.keyboard('[Enter]');
842+
843+
// Delete the selected item
844+
await userEvent.keyboard('[Backspace]');
845+
await userEvent.keyboard('[Backspace]');
846+
await userEvent.keyboard('[Backspace]');
847+
await userEvent.keyboard('[Backspace]');
848+
await userEvent.keyboard('[Backspace]');
849+
// check for an empty value
850+
expect(findInputNode()).toHaveDisplayValue('');
851+
852+
// blur
853+
await userEvent.keyboard('[Tab]');
854+
assertMenuClosed(mockProps);
855+
856+
// open the menu
857+
await userEvent.click(findInputNode());
858+
assertMenuOpen(mockProps);
859+
860+
// check if the `li` item are all false
861+
for (let i = 0; i < mockProps.items.length; i++) {
862+
const item = findMenuItemNode(i);
863+
expect(item).toHaveAttribute('aria-selected', 'false');
864+
}
865+
});
820866
});
821867
});

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,9 @@ const ComboBox = forwardRef(
854854

855855
const handleFocus = (evt: FocusEvent<HTMLDivElement>) => {
856856
setIsFocused(evt.type === 'focus');
857+
if (!inputRef.current?.value && evt.type === 'blur') {
858+
selectItem(null);
859+
}
857860
};
858861

859862
const readOnlyEventHandlers = readOnly
@@ -1024,6 +1027,17 @@ const ComboBox = forwardRef(
10241027
toggleMenu();
10251028
}
10261029
}
1030+
if (
1031+
!inputValue &&
1032+
highlightedIndex == -1 &&
1033+
event.key == 'Enter'
1034+
) {
1035+
if (!isOpen) toggleMenu();
1036+
selectItem(null);
1037+
event.preventDownshiftDefault = true;
1038+
if (event.currentTarget.ariaExpanded === 'false')
1039+
openMenu();
1040+
}
10271041
if (typeahead && event.key === 'Tab') {
10281042
// event.preventDefault();
10291043
const matchingItem = items.find((item) =>

0 commit comments

Comments
 (0)