Skip to content

Commit 9c43533

Browse files
MapTo0ilhan007
authored andcommittedNov 27, 2019
feat(ItemNavigation): introduce navigationMode property (#910)
(1) The ItemNavigation now has three navigation modes: - "Vertical" - "Up|Down" navigation - "Horizontal"- "Left|Right" navigation - "Auto" - both "Vertical" and "Horizontal" navigation (2) From now you can navigate throw the List items and Table rows only vertically via the "Up" and "Down" arrow keys. FIXES: #862
1 parent ae95a8d commit 9c43533

File tree

7 files changed

+77
-10
lines changed

7 files changed

+77
-10
lines changed
 

‎packages/base/src/delegate/ItemNavigation.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
import EventProvider from "../EventProvider.js";
1111
import UI5Element from "../UI5Element.js";
12+
import NavigationMode from "../types/NavigationMode.js";
1213

1314
// navigatable items must have id and tabindex
1415
class ItemNavigation extends EventProvider {
@@ -19,6 +20,11 @@ class ItemNavigation extends EventProvider {
1920
this.rowSize = options.rowSize || 1;
2021
this.cyclic = options.cyclic || false;
2122

23+
const navigationMode = options.navigationMode;
24+
const autoNavigation = !navigationMode || navigationMode === NavigationMode.Auto;
25+
this.horizontalNavigationOn = autoNavigation || navigationMode === NavigationMode.Horizontal;
26+
this.verticalNavigationOn = autoNavigation || navigationMode === NavigationMode.Vertical;
27+
2228
this.rootWebComponent = rootWebComponent;
2329
this.rootWebComponent.addEventListener("keydown", this.onkeydown.bind(this));
2430
this.rootWebComponent.addEventListener("_componentStateFinalized", () => {
@@ -32,6 +38,14 @@ class ItemNavigation extends EventProvider {
3238
});
3339
}
3440

41+
_horizontalNavigationOn() {
42+
return this.horizontalNavigationOn;
43+
}
44+
45+
_verticalNavigationOn() {
46+
return this.verticalNavigationOn;
47+
}
48+
3549
_onKeyPress(event) {
3650
const items = this._getItems();
3751

@@ -59,19 +73,19 @@ class ItemNavigation extends EventProvider {
5973
}
6074

6175
onkeydown(event) {
62-
if (isUp(event)) {
76+
if (isUp(event) && this._verticalNavigationOn()) {
6377
return this._handleUp(event);
6478
}
6579

66-
if (isDown(event)) {
80+
if (isDown(event) && this._verticalNavigationOn()) {
6781
return this._handleDown(event);
6882
}
6983

70-
if (isLeft(event)) {
84+
if (isLeft(event) && this._horizontalNavigationOn()) {
7185
return this._handleLeft(event);
7286
}
7387

74-
if (isRight(event)) {
88+
if (isRight(event) && this._horizontalNavigationOn()) {
7589
return this._handleRight(event);
7690
}
7791

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const NavigationMode = {
2+
Auto: "Auto",
3+
Vertical: "Vertical",
4+
Horizontal: "Horizontal",
5+
};
6+
export default NavigationMode;

‎packages/main/src/List.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
33
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
44
import { getLastTabbableElement } from "@ui5/webcomponents-base/dist/util/TabbableElements.js";
55
import { isTabNext } from "@ui5/webcomponents-base/dist/events/PseudoEvents.js";
6+
import NavigationMode from "@ui5/webcomponents-base/dist/types/NavigationMode.js";
67
import ListMode from "./types/ListMode.js";
78
import ListSeparators from "./types/ListSeparators.js";
89
import ListItemType from "./types/ListItemType.js";
@@ -259,7 +260,10 @@ class List extends UI5Element {
259260
}
260261

261262
initItemNavigation() {
262-
this._itemNavigation = new ItemNavigation(this);
263+
this._itemNavigation = new ItemNavigation(this, {
264+
navigationMode: NavigationMode.Vertical,
265+
});
266+
263267
this._itemNavigation.getItemsCallback = () => this.getSlottedNodes("items");
264268
}
265269

‎packages/main/src/Table.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
33
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
44
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
5+
import NavigationMode from "@ui5/webcomponents-base/dist/types/NavigationMode.js";
56
import TableTemplate from "./generated/templates/TableTemplate.lit.js";
67

78
// Styles
@@ -161,7 +162,9 @@ class Table extends UI5Element {
161162
constructor() {
162163
super();
163164

164-
this._itemNavigation = new ItemNavigation(this);
165+
this._itemNavigation = new ItemNavigation(this, {
166+
navigationMode: NavigationMode.Vertical,
167+
});
165168

166169
this._itemNavigation.getItemsCallback = function getItemsCallback() {
167170
const columnHeader = this.getColumnHeader();

‎packages/main/test/pages/ItemNavigation.html

+7
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,12 @@ <h2>Focus does not cycle</h2>
1616
<ui5-li id="item1">Option 1</ui5-li>
1717
<ui5-li id="item2">Option 2</ui5-li>
1818
</ui5-list>
19+
20+
<h2>Vertical navigation only</h2>
21+
<ui5-list>
22+
<ui5-li id="item3">Option 2.1</ui5-li>
23+
<ui5-li id="item4">Option 2.2</ui5-li>
24+
<ui5-li id="item5">Option 2.3</ui5-li>
25+
</ui5-list>
1926
</body>
2027
</html>

‎packages/main/test/specs/ItemNavigation.spec.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,31 @@ describe("Item Navigation Tests", () => {
99
const firstItem = $("#item1");
1010
const secondItem = $("#item2");
1111

12+
browser.pause(2000);
1213
firstItem.click();
13-
firstItem.keys("ArrowLeft");
1414
firstItem.keys("ArrowUp");
15-
assert.strictEqual(firstItem.isFocused(), true, "first item remains focused");
15+
assert.strictEqual(firstItem.isFocused(), true, "first item remains focused - border reached.");
1616

17+
browser.pause(2000);
1718
secondItem.click();
18-
secondItem.keys("ArrowRight");
1919
secondItem.keys("ArrowDown");
20-
assert.strictEqual(secondItem.isFocused(), true, "second item remains focused");
20+
assert.strictEqual(secondItem.isFocused(), true, "second item remains focused - border reached.");
21+
});
22+
23+
24+
it("vertical focus navigation", () => {
25+
const firstItem = $("#item3");
26+
const secondItem = $("#item4");
27+
28+
// horizontal navigation is allowed is prevented
29+
firstItem.click();
30+
firstItem.keys("ArrowRight");
31+
assert.strictEqual(firstItem.isFocused(), true, "first item remains focused - horizontal navigation prevented.");
32+
33+
browser.pause(2000);
34+
35+
// verical navigation is allowed
36+
firstItem.keys("ArrowDown");
37+
assert.strictEqual(secondItem.isFocused(), true, "second item is now focused - vertical navigation allowed.");
2138
});
2239
});

‎packages/main/test/specs/List.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,20 @@ describe("Date Picker Tests", () => {
143143
itemLink.keys("Tab");
144144
assert.strictEqual(itemRadioBtn.isFocused(), true, "the last tabbable element (radio) is focused");
145145
});
146+
147+
it("does not focus next / prev item when right / left arrow is pressed", () => {
148+
const firstListItem = $("#country1");
149+
const secondListItem = $("#country2");
150+
151+
firstListItem.click();
152+
153+
firstListItem.keys("ArrowRight");
154+
155+
assert.ok(firstListItem.isFocused(), "First item remains focussed");
156+
assert.strictEqual(secondListItem.isFocused(), false, "Second list item not should be focused");
157+
158+
firstListItem.keys("ArrowLeft");
159+
160+
assert.ok(firstListItem.isFocused(), "First item remains focussed");
161+
});
146162
});

0 commit comments

Comments
 (0)
Please sign in to comment.