Skip to content

Commit

Permalink
fix(ui5-cb-item): return the DOM reference of the list item (#8872)
Browse files Browse the repository at this point in the history
* fix(ui5-cb-item): return the DOM reference of the list item

fixes: #8841
  • Loading branch information
ndeshev committed May 8, 2024
1 parent 1a133b2 commit 2553213
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 7 deletions.
10 changes: 8 additions & 2 deletions packages/main/src/ComboBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ const SKIP_ITEMS_SIZE = 10;
* Interface for components that may be slotted inside a `ui5-combobox`
* @public
*/
interface IComboBoxItem {
interface IComboBoxItem extends UI5Element {
text: string,
focused: boolean,
isGroupItem: boolean,
selected?: boolean,
additionalText?: string,
stableDomRef: string,
}

type ValueStateAnnouncement = Record<Exclude<ValueState, ValueState.None>, string>;
Expand Down Expand Up @@ -412,7 +413,7 @@ class ComboBox extends UI5Element {
this._userTypedValue = "";
}

onBeforeRendering() {
async onBeforeRendering() {
const popover: Popover | undefined = this.valueStatePopover;

this.FormSupport = getFeature<typeof FormSupportT>("FormSupport");
Expand All @@ -437,6 +438,11 @@ class ComboBox extends UI5Element {
this._initialRendering = false;

this.style.setProperty(getScopedVarName("--_ui5-input-icons-count"), `${this.iconsCount}`);

const suggestionsPopover = await this._getPicker();
this.items.forEach(item => {
item._getRealDomRef = () => suggestionsPopover.querySelector(`*[data-ui5-stable=${item.stableDomRef}]`)!;
});
}

get iconsCount() {
Expand Down
4 changes: 4 additions & 0 deletions packages/main/src/ComboBoxGroupItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class ComboBoxGroupItem extends UI5Element implements IComboBoxItem {
get isGroupItem(): boolean {
return true;
}

get stableDomRef() {
return this.getAttribute("stable-dom-ref") || `${this._id}-stable-dom-ref`;
}
}

ComboBoxGroupItem.define();
Expand Down
4 changes: 4 additions & 0 deletions packages/main/src/ComboBoxItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class ComboBoxItem extends UI5Element implements IComboBoxItem {
get isGroupItem(): boolean {
return false;
}

get stableDomRef() {
return this.getAttribute("stable-dom-ref") || `${this._id}-stable-dom-ref`;
}
}

ComboBoxItem.define();
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/ComboBoxPopover.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
.mappedItem={{this}}
?selected={{this.selected}}
?focused={{this.focused}}
data-ui5-stable="{{this.stableDomRef}}"
>
{{this.text}}
</ui5-li>
Expand Down
4 changes: 0 additions & 4 deletions packages/main/src/MultiComboBoxItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ class MultiComboBoxItem extends ComboBoxItem implements IMultiComboBoxItem {
@property({ type: Boolean })
declare selected: boolean;

get stableDomRef() {
return this.getAttribute("stable-dom-ref") || `${this._id}-stable-dom-ref`;
}

get isMultiComboBoxItem() {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/main/test/pages/ComboBox.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<ui5-button id="value-set-btn">Set value</ui5-button>

<ui5-combobox id="combo2" class="combobox2auto" accessible-name="Select destination:">
<ui5-cb-item text="Algeria"></ui5-cb-item>
<ui5-cb-item text="Algeria" id="cbi"></ui5-cb-item>
<ui5-cb-item text="Argentina"></ui5-cb-item>
<ui5-cb-item text="Australia"></ui5-cb-item>
<ui5-cb-item text="Austria"></ui5-cb-item>
Expand Down
11 changes: 11 additions & 0 deletions packages/main/test/specs/ComboBox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1231,4 +1231,15 @@ describe("Keyboard navigation", async () => {

assert.ok(isInVisibleArea, "Item should be displayed in the viewport");
});

it ("Should get the physical DOM reference for the cb item", async () => {
await browser.url(`test/pages/ComboBox.html`);

const cbItemDomRef = await browser.executeAsync(done => {
return done(document.getElementById("cbi").getDomRef());
});

assert.ok(cbItemDomRef, "ComboBoxItem's DOM reference exists");
});

});

0 comments on commit 2553213

Please sign in to comment.