Skip to content

Commit 3d46e2d

Browse files
authored
fix: fix Item Navigation cycling (#985)
**Background** By default the focus (managed by ItemNavigation) does not cycle trough the items, which turned out that has not been working. **Root Cause** We check for the `cycle` config when border cases are reached (first or last item), but the only thing we do is to fire a private event ("_borderReach"), but updating the index, so it actually makes a whole loop (cycle). **Fix** In these border cases we just update the internal index to `0` (when "top" border is reached) and `items.length-1` (when "bottom" border is reached).
1 parent 6e0077d commit 3d46e2d

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

packages/base/src/delegate/ItemNavigation.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ class ItemNavigation extends EventProvider {
3737

3838
if (this.currentIndex >= items.length) {
3939
if (!this.cyclic) {
40+
this.currentIndex = items.length - 1;
4041
this.fireEvent(ItemNavigation.BORDER_REACH, { start: false, end: true, offset: this.currentIndex });
42+
} else {
43+
this.currentIndex = this.currentIndex - items.length;
4144
}
42-
43-
this.currentIndex = this.currentIndex - items.length;
4445
} else if (this.currentIndex < 0) {
4546
if (!this.cyclic) {
47+
this.currentIndex = 0;
4648
this.fireEvent(ItemNavigation.BORDER_REACH, { start: true, end: false, offset: this.currentIndex });
49+
} else {
50+
this.currentIndex = items.length + this.currentIndex;
4751
}
48-
49-
this.currentIndex = items.length + this.currentIndex;
5052
}
5153

5254
this.update();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
5+
<meta charset="utf-8">
6+
<title>Item Navigation</title>
7+
8+
<script src="../../webcomponentsjs/webcomponents-loader.js"></script>
9+
<script src="../../resources/bundle.esm.js" type="module"></script>
10+
<script nomodule src="../../resources/bundle.es5.js"></script>
11+
</head>
12+
13+
<body>
14+
<h2>Focus does not cycle</h2>
15+
<ui5-list>
16+
<ui5-li id="item1">Option 1</ui5-li>
17+
<ui5-li id="item2">Option 2</ui5-li>
18+
</ui5-list>
19+
</body>
20+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const assert = require("assert");
2+
3+
describe("Item Navigation Tests", () => {
4+
before(() => {
5+
browser.url("http://localhost:8080/test-resources/pages/ItemNavigation.html");
6+
});
7+
8+
it("focus does not cycle", () => {
9+
const firstItem = $("#item1");
10+
const secondItem = $("#item2");
11+
12+
firstItem.click();
13+
firstItem.keys("ArrowLeft");
14+
firstItem.keys("ArrowUp");
15+
assert.strictEqual(firstItem.isFocused(), true, "first item remains focused");
16+
17+
secondItem.click();
18+
secondItem.keys("ArrowRight");
19+
secondItem.keys("ArrowDown");
20+
assert.strictEqual(secondItem.isFocused(), true, "second item remains focused");
21+
});
22+
});

0 commit comments

Comments
 (0)