Skip to content

feat(ui5-list): focus() now focuses item #11486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/main/cypress/specs/List.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type UI5Element from "@ui5/webcomponents-base";
import List from "../../src/List.js";
import ListItemStandard from "../../src/ListItemStandard.js";

Expand Down Expand Up @@ -256,3 +257,53 @@ describe("List - Wrapping Behavior", () => {
.should("not.exist");
});
});

describe("List - getFocusDomRef Method", () => {
it("should return undefined when the list is empty", () => {
cy.mount(<List></List>);

cy.get<List>("[ui5-list]")
.then(($el) => {
expect($el[0].getFocusDomRef()).to.be.undefined;
});
});

it("should return first item if no item was focused before", () => {
cy.mount(
<List>
<ListItemStandard id="item1">Item 1</ListItemStandard>
<ListItemStandard>Item 2</ListItemStandard>
<ListItemStandard>Item 3</ListItemStandard>
</List>
);

cy.get<UI5Element>("[ui5-list], #item1")
.then(($el) => {
const list = $el[0];
const item = $el[1];

expect(list.getFocusDomRef()).to.equal(item.getFocusDomRef());
});
});

it("should return last focused item in the list", () => {
cy.mount(
<List>
<ListItemStandard>Item 1</ListItemStandard>
<ListItemStandard id="item2">Item 2</ListItemStandard>
<ListItemStandard>Item 3</ListItemStandard>
</List>
);

cy.get("#item2").click();
cy.get("#item2").should("be.focused");

cy.get<UI5Element>("[ui5-list], #item2")
.then(($el) => {
const list = $el[0];
const item = $el[1];

expect(list.getFocusDomRef()).to.equal(item.getFocusDomRef());
});
});
});
4 changes: 4 additions & 0 deletions packages/main/src/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,10 @@ class List extends UI5Element {
});
}

getFocusDomRef() {
return this._itemNavigation._getCurrentItem();
}

get shouldRenderH1() {
return !this.header.length && this.headerText;
}
Expand Down
Loading