Skip to content

Commit f744f65

Browse files
fix(combobox): preselect mark on initial load (#20977)
* fix(combobox): preselect mark on initial load * test(selected-item): updated
1 parent 7a12453 commit f744f65

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,29 @@ describe('ComboBox', () => {
498498
// The displayed value should still be the one from the first render.
499499
expect(findInputNode()).toHaveDisplayValue(mockProps.items[0].label);
500500
});
501+
502+
it('should mark the initially selectedItem on load when rendered', async () => {
503+
render(
504+
<ComboBox
505+
{...mockProps}
506+
initialSelectedItem={mockProps.items[0]}
507+
selectedItem={mockProps.items[0]}
508+
/>
509+
);
510+
await openMenu();
511+
512+
// Find the first menu item (which should be the initially selected item)
513+
const menuItems = screen.getAllByRole('option');
514+
const firstMenuItem = menuItems[0];
515+
516+
// Check if the initially selected item has the active class
517+
expect(firstMenuItem).toHaveClass(
518+
`${prefix}--list-box__menu-item--active`
519+
);
520+
521+
// Check if the initially selected item contains an SVG (checkmark icon)
522+
expect(firstMenuItem.querySelector('svg')).toBeInTheDocument();
523+
});
501524
});
502525

503526
describe('provided `selectedItem`', () => {

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

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,52 @@ AutocompleteWithTypeahead.argTypes = {
356356
onChange: { action: 'onChange' },
357357
};
358358

359-
export const Test = () => {
359+
export const Test = (args) => {
360+
const items = [
361+
{
362+
id: 'option-0',
363+
text: 'An example option that is really long to show what should be done to handle long text',
364+
},
365+
{
366+
id: 'option-1',
367+
text: 'Option 1',
368+
},
369+
{
370+
id: 'option-2',
371+
text: 'Option 2',
372+
},
373+
{
374+
id: 'option-3',
375+
text: 'Option 3 - a disabled item',
376+
disabled: true,
377+
},
378+
{
379+
id: 'option-4',
380+
text: 'Option 4',
381+
},
382+
{
383+
id: 'option-5',
384+
text: 'Option 5',
385+
},
386+
];
360387
return (
361388
<div style={{ width: 300 }}>
362389
<ComboBox
363-
id="compForIssue"
364-
decorator={<Locked />}
365-
items={['zero', 'one', 'two', 'three']}
366-
onChange={() => {}}
390+
id="carbon-combobox"
391+
items={items}
392+
itemToString={(item) => (item ? item.text : '')}
393+
titleText="ComboBox title"
394+
helperText="Combobox helper text"
395+
onChange={action('onChange')}
396+
initialSelectedItem={{
397+
id: 'option-1',
398+
text: 'Option 1',
399+
}}
400+
selectedItem={{
401+
id: 'option-1',
402+
text: 'Option 1',
403+
}}
404+
{...args}
367405
/>
368406
</div>
369407
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ const ComboBox = forwardRef(
11881188
return (
11891189
<ListBox.MenuItem
11901190
key={itemProps.id}
1191-
isActive={selectedItem === item}
1191+
isActive={isEqual(selectedItem, item)}
11921192
isHighlighted={highlightedIndex === index}
11931193
title={title}
11941194
disabled={disabled}
@@ -1198,7 +1198,7 @@ const ComboBox = forwardRef(
11981198
) : (
11991199
itemToString(item)
12001200
)}
1201-
{selectedItem === item && (
1201+
{isEqual(selectedItem, item) && (
12021202
<Checkmark
12031203
className={`${prefix}--list-box__menu-item__selected-icon`}
12041204
/>

0 commit comments

Comments
 (0)