Skip to content

Commit ad6e9cc

Browse files
committed
fix: review feedback
1 parent e76e1ac commit ad6e9cc

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,4 +3299,40 @@ describe("SelectedValue API", () => {
32993299
expect(comboBox.formFormattedValue).to.equal("Germany");
33003300
});
33013301
});
3302+
3303+
it("should populate input value from selectedValue on initial render", () => {
3304+
cy.mount(
3305+
<ComboBox selectedValue="DE">
3306+
<ComboBoxItem text="Austria" value="AT"></ComboBoxItem>
3307+
<ComboBoxItem text="Germany" value="DE"></ComboBoxItem>
3308+
<ComboBoxItem text="France" value="FR"></ComboBoxItem>
3309+
</ComboBox>
3310+
);
3311+
3312+
cy.get("[ui5-combobox]")
3313+
.should("have.prop", "value", "Germany")
3314+
.should("have.prop", "selectedValue", "DE");
3315+
3316+
cy.get("[ui5-cb-item]").eq(1)
3317+
.should("have.prop", "selected", true);
3318+
});
3319+
3320+
it("should select correct item by selectedValue when multiple items have same text", () => {
3321+
cy.mount(
3322+
<ComboBox selectedValue="emp-205">
3323+
<ComboBoxItem text="John Smith" additionalText="Sales" value="emp-101"></ComboBoxItem>
3324+
<ComboBoxItem text="John Smith" additionalText="Engineering" value="emp-205"></ComboBoxItem>
3325+
<ComboBoxItem text="John Smith" additionalText="Marketing" value="emp-342"></ComboBoxItem>
3326+
</ComboBox>
3327+
);
3328+
3329+
cy.get("[ui5-combobox]")
3330+
.should("have.prop", "value", "John Smith")
3331+
.should("have.prop", "selectedValue", "emp-205");
3332+
3333+
// The second item (Engineering) should be selected, not the first
3334+
cy.get("[ui5-cb-item]").eq(0).should("have.prop", "selected", false);
3335+
cy.get("[ui5-cb-item]").eq(1).should("have.prop", "selected", true);
3336+
cy.get("[ui5-cb-item]").eq(2).should("have.prop", "selected", false);
3337+
});
33023338
});

packages/main/src/ComboBox.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class ComboBox extends UI5Element implements IFormInputElement {
477477
_lastValue: string;
478478
_selectedItemText = "";
479479
_userTypedValue = "";
480-
_useSelectedValue: boolean = false;
480+
_useSelectedValue = false;
481481
_valueStateLinks: Array<HTMLElement> = [];
482482
_composition?: InputComposition;
483483
@i18n("@ui5/webcomponents")
@@ -875,7 +875,7 @@ class ComboBox extends UI5Element implements IFormInputElement {
875875
if (this.open) {
876876
this._itemFocused = true;
877877
this.value = isGroupItem ? "" : currentItem.text!;
878-
this.selectedValue = isGroupItem ? "" : currentItem?.value;
878+
this.selectedValue = isGroupItem ? undefined : currentItem.value;
879879
this.focused = false;
880880

881881
currentItem.focused = true;
@@ -1246,20 +1246,25 @@ class ComboBox extends UI5Element implements IFormInputElement {
12461246
if (!shouldSelectionBeCleared && !itemToBeSelected) {
12471247
if (isInstanceOfComboBoxItemGroup(item)) {
12481248
if (this._useSelectedValue) {
1249-
itemToBeSelected = item.items.find(i => i.value === valueToMatch && this.value === i.text);
1249+
itemToBeSelected = item.items.find(i => i.value === valueToMatch && (this.value === "" || this.value === i.text));
12501250
} else {
12511251
itemToBeSelected = item.items?.find(i => i.text === this.value);
12521252
}
12531253
} else {
12541254
if (this._useSelectedValue) {
1255-
itemToBeSelected = this.items.find(i => i.value === valueToMatch && this.value === i.text);
1255+
itemToBeSelected = this.items.find(i => i.value === valueToMatch && (this.value === "" || this.value === i.text));
12561256
return;
12571257
}
12581258
itemToBeSelected = item.text === this.value ? item : undefined;
12591259
}
12601260
}
12611261
});
12621262

1263+
// When selectedValue matched an item but value is empty (initial render), populate value from the item's text
1264+
if (itemToBeSelected && this._initialRendering && this.value === "") {
1265+
this.value = itemToBeSelected.text || "";
1266+
}
1267+
12631268
this._filteredItems = this._filteredItems.map(item => {
12641269
if (!isInstanceOfComboBoxItemGroup(item)) {
12651270
item.selected = item === itemToBeSelected;

packages/main/test/pages/ComboBox.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
<div class="demo-section">
2929
<ui5-label id="countryLabel">Select country: </ui5-label>
30-
<ui5-combobox id="combo" class="combobox2auto" accessible-name-ref="countryLabel">
30+
<ui5-combobox id="combo" class="combobox2auto" value="Bulgaria" accessible-name-ref="countryLabel">
3131
<ui5-cb-item text="Algeria"></ui5-cb-item>
3232
<ui5-cb-item text="Argentina"></ui5-cb-item>
3333
<ui5-cb-item text="Australia"></ui5-cb-item>

0 commit comments

Comments
 (0)