Skip to content

Commit 025ec46

Browse files
authored
feat(ui5-list): focus() now focuses item (#11486)
1 parent e3f69f0 commit 025ec46

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type UI5Element from "@ui5/webcomponents-base";
12
import List from "../../src/List.js";
23
import ListItemStandard from "../../src/ListItemStandard.js";
34

@@ -256,3 +257,53 @@ describe("List - Wrapping Behavior", () => {
256257
.should("not.exist");
257258
});
258259
});
260+
261+
describe("List - getFocusDomRef Method", () => {
262+
it("should return undefined when the list is empty", () => {
263+
cy.mount(<List></List>);
264+
265+
cy.get<List>("[ui5-list]")
266+
.then(($el) => {
267+
expect($el[0].getFocusDomRef()).to.be.undefined;
268+
});
269+
});
270+
271+
it("should return first item if no item was focused before", () => {
272+
cy.mount(
273+
<List>
274+
<ListItemStandard id="item1">Item 1</ListItemStandard>
275+
<ListItemStandard>Item 2</ListItemStandard>
276+
<ListItemStandard>Item 3</ListItemStandard>
277+
</List>
278+
);
279+
280+
cy.get<UI5Element>("[ui5-list], #item1")
281+
.then(($el) => {
282+
const list = $el[0];
283+
const item = $el[1];
284+
285+
expect(list.getFocusDomRef()).to.equal(item.getFocusDomRef());
286+
});
287+
});
288+
289+
it("should return last focused item in the list", () => {
290+
cy.mount(
291+
<List>
292+
<ListItemStandard>Item 1</ListItemStandard>
293+
<ListItemStandard id="item2">Item 2</ListItemStandard>
294+
<ListItemStandard>Item 3</ListItemStandard>
295+
</List>
296+
);
297+
298+
cy.get("#item2").click();
299+
cy.get("#item2").should("be.focused");
300+
301+
cy.get<UI5Element>("[ui5-list], #item2")
302+
.then(($el) => {
303+
const list = $el[0];
304+
const item = $el[1];
305+
306+
expect(list.getFocusDomRef()).to.equal(item.getFocusDomRef());
307+
});
308+
});
309+
});

packages/main/src/List.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,10 @@ class List extends UI5Element {
615615
});
616616
}
617617

618+
getFocusDomRef() {
619+
return this._itemNavigation._getCurrentItem();
620+
}
621+
618622
get shouldRenderH1() {
619623
return !this.header.length && this.headerText;
620624
}

0 commit comments

Comments
 (0)