Skip to content

Commit cbcd4cf

Browse files
committed
fix(ui5-combo-box): proper first select of an item with duplicate
Fixes: #13292 First select of an item with duplicate text now works properly as long as value property is set to the item.
1 parent 00bb8fe commit cbcd4cf

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

packages/main/cypress/specs/ComboBox.cy.tsx

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3397,6 +3397,138 @@ describe("SelectedValue API", () => {
33973397
cy.get("[ui5-cb-item]").eq(1).should("have.prop", "selected", true);
33983398
cy.get("[ui5-cb-item]").eq(2).should("have.prop", "selected", false);
33993399
});
3400+
3401+
it("should properly select an item when first choosing from items with same text but different values", () => {
3402+
cy.mount(
3403+
<ComboBox id="employee-combo" placeholder="Select an employee">
3404+
<ComboBoxItem text="John Smith" additionalText="Sales" value="emp-101"></ComboBoxItem>
3405+
<ComboBoxItem text="John Smith" additionalText="Engineering" value="emp-205"></ComboBoxItem>
3406+
<ComboBoxItem text="John Smith" additionalText="Marketing" value="emp-342"></ComboBoxItem>
3407+
<ComboBoxItem text="Jane Doe" additionalText="HR" value="emp-118"></ComboBoxItem>
3408+
<ComboBoxItem text="Jane Doe" additionalText="Finance" value="emp-267"></ComboBoxItem>
3409+
</ComboBox>
3410+
);
3411+
3412+
cy.get("#employee-combo")
3413+
.as("combo")
3414+
.invoke('on', 'ui5-selection-change', cy.spy().as('selectionChangeSpy'));
3415+
3416+
// Open the picker
3417+
cy.get("@combo")
3418+
.shadow()
3419+
.find("[ui5-icon]")
3420+
.realClick();
3421+
3422+
cy.get("@combo")
3423+
.shadow()
3424+
.find("[ui5-responsive-popover]")
3425+
.should("have.attr", "open");
3426+
3427+
// Select the third item (John Smith - Marketing, emp-342)
3428+
cy.get("[ui5-cb-item]").eq(2).realClick();
3429+
3430+
// Check that selection-change event fired with the correct item
3431+
cy.get("@selectionChangeSpy").should("have.been.calledOnce");
3432+
cy.get("@selectionChangeSpy").should("have.been.calledWithMatch", Cypress.sinon.match(event => {
3433+
return event.detail.item.text === "John Smith" &&
3434+
event.detail.item.value === "emp-342" &&
3435+
event.detail.item.additionalText === "Marketing";
3436+
}));
3437+
3438+
// Verify the combo has the correct value and selectedValue
3439+
cy.get("@combo")
3440+
.should("have.prop", "value", "John Smith")
3441+
.should("have.prop", "selectedValue", "emp-342");
3442+
3443+
// Re-open the picker
3444+
cy.get("@combo")
3445+
.shadow()
3446+
.find("[ui5-icon]")
3447+
.realClick();
3448+
3449+
cy.get("@combo")
3450+
.shadow()
3451+
.find("[ui5-responsive-popover]")
3452+
.should("have.attr", "open");
3453+
3454+
// Verify the third item is selected
3455+
cy.get("[ui5-cb-item]").eq(0).should("have.prop", "selected", false);
3456+
cy.get("[ui5-cb-item]").eq(1).should("have.prop", "selected", false);
3457+
cy.get("[ui5-cb-item]").eq(2).should("have.prop", "selected", true);
3458+
cy.get("[ui5-cb-item]").eq(3).should("have.prop", "selected", false);
3459+
cy.get("[ui5-cb-item]").eq(4).should("have.prop", "selected", false);
3460+
});
3461+
3462+
it("should properly select item with same text but different values after consecutive selects", () => {
3463+
cy.mount(
3464+
<ComboBox id="employee-combo" placeholder="Select an employee">
3465+
<ComboBoxItem text="John Smith" additionalText="Sales" value="emp-101"></ComboBoxItem>
3466+
<ComboBoxItem text="John Smith" additionalText="Engineering" value="emp-205"></ComboBoxItem>
3467+
<ComboBoxItem text="John Smith" additionalText="Marketing" value="emp-342"></ComboBoxItem>
3468+
<ComboBoxItem text="Jane Doe" additionalText="HR" value="emp-118"></ComboBoxItem>
3469+
<ComboBoxItem text="Jane Doe" additionalText="Finance" value="emp-267"></ComboBoxItem>
3470+
</ComboBox>
3471+
);
3472+
3473+
cy.get("#employee-combo")
3474+
.as("combo")
3475+
.invoke('on', 'ui5-selection-change', cy.spy().as('selectionChangeSpy'));
3476+
3477+
// Open the picker
3478+
cy.get("@combo")
3479+
.shadow()
3480+
.find("[ui5-icon]")
3481+
.realClick();
3482+
3483+
cy.get("@combo")
3484+
.shadow()
3485+
.find("[ui5-responsive-popover]")
3486+
.should("have.attr", "open");
3487+
3488+
// Select the third item (John Smith - Marketing, emp-342)
3489+
cy.get("[ui5-cb-item]").eq(2).realClick();
3490+
3491+
// Verify the combo has the correct value and selectedValue
3492+
cy.get("@combo")
3493+
.should("have.prop", "value", "John Smith")
3494+
.should("have.prop", "selectedValue", "emp-342");
3495+
3496+
// Re-open the picker
3497+
cy.get("@combo")
3498+
.shadow()
3499+
.find("[ui5-icon]")
3500+
.realClick();
3501+
3502+
cy.get("@combo")
3503+
.shadow()
3504+
.find("[ui5-responsive-popover]")
3505+
.should("have.attr", "open");
3506+
3507+
// Verify the third item is selected
3508+
cy.get("[ui5-cb-item]").eq(2).should("have.prop", "selected", true);
3509+
3510+
// select the second item
3511+
cy.get("[ui5-cb-item]").eq(1).realClick();
3512+
3513+
// Verify the combo has the correct value and selectedValue
3514+
cy.get("@combo")
3515+
.should("have.prop", "value", "John Smith")
3516+
.should("have.prop", "selectedValue", "emp-205");
3517+
3518+
// Re-open the picker
3519+
cy.get("@combo")
3520+
.shadow()
3521+
.find("[ui5-icon]")
3522+
.realClick();
3523+
3524+
cy.get("@combo")
3525+
.shadow()
3526+
.find("[ui5-responsive-popover]")
3527+
.should("have.attr", "open");
3528+
3529+
// Verify the second item is selected
3530+
cy.get("[ui5-cb-item]").eq(1).should("have.prop", "selected", true);
3531+
});
34003532
});
34013533

34023534
describe("Case-Insensitive Selection", () => {

packages/main/src/ComboBox.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,12 @@ class ComboBox extends UI5Element implements IFormInputElement {
13731373
}
13741374

13751375
this.value = this._selectedItemText;
1376+
// On first item select the _useSelectedValue is still false.
1377+
// In case the item has a value property, we set the _useSelectedValue to true to start working with the value instead with the text
1378+
if (!this._useSelectedValue && item.value != undefined) {
1379+
this._useSelectedValue = true;
1380+
}
1381+
13761382
if (this._useSelectedValue) {
13771383
this.selectedValue = item.value;
13781384
}

0 commit comments

Comments
 (0)