Skip to content

Commit 7107c2f

Browse files
committed
fix(ui5-combobox): set selectedValue correctly on change
1 parent 4de2083 commit 7107c2f

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,6 +3529,31 @@ describe("SelectedValue API", () => {
35293529
// Verify the second item is selected
35303530
cy.get("[ui5-cb-item]").eq(1).should("have.prop", "selected", true);
35313531
});
3532+
3533+
it("should have selectedValue available in change event on first selection", () => {
3534+
const changeSpy = cy.stub().as("changeSpy");
3535+
cy.mount(
3536+
<ComboBox onChange={changeSpy}>
3537+
<ComboBoxItem text="Cozy1" value="c1"></ComboBoxItem>
3538+
<ComboBoxItem text="Cozy2" value="c2"></ComboBoxItem>
3539+
</ComboBox>
3540+
);
3541+
3542+
// Open dropdown
3543+
cy.get("[ui5-combobox]")
3544+
.as("combo")
3545+
.shadow()
3546+
.find("[ui5-icon]")
3547+
.realClick();
3548+
3549+
// Select first item
3550+
cy.get("[ui5-cb-item]").eq(0).realClick();
3551+
3552+
// Verify change event was called with correct selectedValue
3553+
cy.get("@changeSpy").should("have.been.calledOnce");
3554+
cy.get("@combo").should("have.prop", "value", "Cozy1");
3555+
cy.get("@combo").should("have.prop", "selectedValue", "c1");
3556+
});
35323557
});
35333558

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

packages/main/src/ComboBox.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,8 +1406,12 @@ class ComboBox extends UI5Element implements IFormInputElement {
14061406
this._useSelectedValue = true;
14071407
}
14081408

1409-
if (this._useSelectedValue) {
1409+
// Always set selectedValue when the item has a value property, regardless of _useSelectedValue state
1410+
if (item.value !== undefined) {
14101411
this.selectedValue = item.value;
1412+
} else if (this._useSelectedValue) {
1413+
// Only clear selectedValue if we were using it before
1414+
this.selectedValue = undefined;
14111415
}
14121416

14131417
if (!item.selected) {

packages/main/test/pages/ComboBox.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,20 @@ <h3>ComboBox in Compact</h3>
331331
</ui5-combobox>
332332
</div>
333333

334+
<div class="demo-section">
335+
<span>ComboBox - selectedValue</span>
336+
<ui5-combobox id="selected-value-cb">
337+
<ui5-cb-item text="Item 1" value="1"></ui5-cb-item>
338+
<ui5-cb-item text="Item 2" value="2"></ui5-cb-item>
339+
<ui5-cb-item text="Item 3" value="3"></ui5-cb-item>
340+
<ui5-cb-item text="Item 4" value="4"></ui5-cb-item>
341+
<ui5-cb-item text="Item 5" value="5"></ui5-cb-item>
342+
</ui5-combobox>
343+
<br>
344+
<span>Value: <strong id="cb-value"></strong></span><br>
345+
<span>Selected Value: <strong id="cb-selected-value"></strong></span><br>
346+
</div>
347+
334348
<section>
335349
<h3>ComboBox Composition</h3>
336350

@@ -641,6 +655,12 @@ <h3>ComboBox Composition</h3>
641655
const combo = document.getElementById("toggle-loading-cb");
642656
combo.loading = !combo.loading;
643657
});
658+
659+
document.getElementById("selected-value-cb").addEventListener("ui5-change", function (event) {
660+
const combo = event.target;
661+
document.getElementById("cb-value").innerHTML = combo.value || "(empty)";
662+
document.getElementById("cb-selected-value").innerHTML = combo.selectedValue || "(none)";
663+
});
644664
</script>
645665

646666
</body>

0 commit comments

Comments
 (0)