Skip to content

Commit 130e9c5

Browse files
authored
feat(ui5-breadcrumbs, ui5-meadia-gallery): getFocusDomRef added (#11869)
Fixes #11483
1 parent 390376f commit 130e9c5

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import MediaGallery from "../../src/MediaGallery.js";
2+
import MediaGalleryItem from "../../src/MediaGalleryItem.js";
3+
import type UI5Element from "@ui5/webcomponents-base";
4+
5+
describe("MediaGallery - getFocusDomRef Method", () => {
6+
it("should return undefined when the MediaGallery is empty", () => {
7+
cy.mount(<MediaGallery></MediaGallery>);
8+
9+
cy.get<MediaGallery>("[ui5-media-gallery]")
10+
.then(($el) => {
11+
expect($el[0].getFocusDomRef()).to.be.undefined;
12+
});
13+
});
14+
15+
it("should return first item if no item was focused before", () => {
16+
cy.mount(
17+
<MediaGallery showAllThumbnails>
18+
<MediaGalleryItem id="item1">
19+
<img src="./img/HT-1000.jpg"/>
20+
<img src="./img/HT-1000.jpg" slot="thumbnail"/>
21+
</MediaGalleryItem>
22+
<MediaGalleryItem id="item2">
23+
<img src="./img/HT-1000.jpg"/>
24+
<img src="./img/HT-1000.jpg" slot="thumbnail"/>
25+
</MediaGalleryItem>
26+
</MediaGallery>
27+
);
28+
29+
cy.get<UI5Element>("[ui5-media-gallery], #item1")
30+
.then(($el) => {
31+
const mg = $el[0];
32+
const item = $el[1];
33+
expect(mg.getFocusDomRef()).to.equal(item.getFocusDomRef());
34+
});
35+
});
36+
37+
it("should return last focused item in the MediaGallery", () => {
38+
cy.mount(
39+
<MediaGallery>
40+
<MediaGalleryItem id="item1">
41+
<img src="./img/HT-1000.jpg"/>
42+
<img src="./img/HT-1000.jpg" slot="thumbnail"/>
43+
</MediaGalleryItem>
44+
<MediaGalleryItem id="item2">
45+
<img src="./img/HT-1000.jpg"/>
46+
<img src="./img/HT-1000.jpg" slot="thumbnail"/>
47+
</MediaGalleryItem>
48+
</MediaGallery>
49+
);
50+
51+
cy.get("#item2").click();
52+
cy.get("#item2").should("be.focused");
53+
54+
cy.get<UI5Element>("[ui5-media-gallery], #item2")
55+
.then(($el) => {
56+
const mg = $el[0];
57+
const item = $el[1];
58+
59+
expect(mg.getFocusDomRef()).to.equal(item.getFocusDomRef());
60+
});
61+
});
62+
});

packages/fiori/src/MediaGallery.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ class MediaGallery extends UI5Element {
377377
return items;
378378
}
379379

380+
getFocusDomRef() {
381+
return this._itemNavigation._getCurrentItem();
382+
}
383+
380384
_selectItem(item: IMediaGalleryItem, userInteraction = false) {
381385
if (item === this._selectedItem) {
382386
return;

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Breadcrumbs from "../../src/Breadcrumbs.js";
22
import BreadcrumbsItem from "../../src/BreadcrumbsItem.js";
33
import { getFirstFocusableElement } from "@ui5/webcomponents-base/dist/util/FocusableElements.js";
4+
import type UI5Element from "@ui5/webcomponents-base";
45

56
describe("breadcrumbs navigation", () => {
67

@@ -32,4 +33,65 @@ describe("breadcrumbs navigation", () => {
3233
})
3334
.should("equal", 0); // index is back to 0 after arrow up
3435
});
35-
});
36+
});
37+
38+
describe("Breadcrumbs - getFocusDomRef Method", () => {
39+
it("should return undefined when the Breadcrumbs is empty", () => {
40+
cy.mount(<Breadcrumbs design="NoCurrentPage"></Breadcrumbs>);
41+
42+
cy.get<Breadcrumbs>("[ui5-breadcrumbs]")
43+
.then(($el) => {
44+
expect($el[0].getFocusDomRef()).to.be.undefined;
45+
});
46+
});
47+
48+
it("should return first item if no item was focused before", () => {
49+
cy.mount(
50+
<Breadcrumbs>
51+
<BreadcrumbsItem id="first">Link1</BreadcrumbsItem>
52+
<BreadcrumbsItem>Link2</BreadcrumbsItem>
53+
</Breadcrumbs>
54+
);
55+
56+
cy.get<UI5Element>("[ui5-breadcrumbs], #first")
57+
.then(($el) => {
58+
const breadcrumbs = $el[0];
59+
const item = $el[1];
60+
61+
const breadcrumbsAnchor = breadcrumbs.getFocusDomRef();
62+
const itemAnchor = item.getFocusDomRef().shadowRoot.querySelector("a");
63+
64+
expect(breadcrumbsAnchor.textContent).to.equal(itemAnchor.textContent);
65+
});
66+
});
67+
68+
it("should return last focused item in the Breadcrumbs", () => {
69+
cy.mount(
70+
<Breadcrumbs id="breadcrumbs">
71+
<BreadcrumbsItem>Link1</BreadcrumbsItem>
72+
<BreadcrumbsItem id="second">Link2</BreadcrumbsItem>
73+
<BreadcrumbsItem>Link1</BreadcrumbsItem>
74+
<BreadcrumbsItem>Link1</BreadcrumbsItem>
75+
</Breadcrumbs>
76+
);
77+
78+
cy.get("#breadcrumbs")
79+
.shadow()
80+
.find("ui5-link")
81+
.eq(1)
82+
.as("second");
83+
84+
cy.get("@second").click();
85+
86+
cy.get<UI5Element>("[ui5-breadcrumbs], #second")
87+
.then(($el) => {
88+
const breadcrumbs = $el[0];
89+
const item = $el[1];
90+
91+
const breadcrumbsAnchor = breadcrumbs.getFocusDomRef();
92+
const itemAnchor = item.getFocusDomRef().shadowRoot.querySelector("a");
93+
94+
expect(breadcrumbsAnchor.textContent).to.equal(itemAnchor.textContent);
95+
});
96+
});
97+
});

packages/main/src/Breadcrumbs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ class Breadcrumbs extends UI5Element {
249249
return items;
250250
}
251251

252+
getFocusDomRef() {
253+
return this._itemNavigation._getCurrentItem();
254+
}
255+
252256
/**
253257
* Returns the translatable accessible name for the popover
254258
* @private

0 commit comments

Comments
 (0)