1
+ import { fetchI18nBundle , getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js" ;
2
+
1
3
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js" ;
2
4
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js" ;
3
- import ItemNavigationBehavior from "@ui5/webcomponents-base/dist/types/ItemNavigationBehavior.js" ;
4
5
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js" ;
5
6
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js" ;
6
7
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js" ;
8
+
9
+ import {
10
+ isDown ,
11
+ isUp ,
12
+ } from "@ui5/webcomponents-base/dist/Keys.js" ;
13
+
7
14
import ProductSwitchTemplate from "./generated/templates/ProductSwitchTemplate.lit.js" ;
8
15
16
+ import {
17
+ PRODUCT_SWITCH_CONTAINER_LABEL ,
18
+ } from "./generated/i18n/i18n-defaults.js" ;
19
+
9
20
// Styles
10
21
import ProductSwitchCss from "./generated/themes/ProductSwitch.css.js" ;
11
22
@@ -46,6 +57,20 @@ const metadata = {
46
57
* The <code>ui5-product-switch</code> is an SAP Fiori specific web component that is used in <code>ui5-shellbar</code>
47
58
* and allows the user to easily switch between products.
48
59
* <br><br>
60
+ *
61
+ * <h3>Keyboard Handling</h3>
62
+ * The <code>ui5-product-switch</code> provides advanced keyboard handling.
63
+ * When focused, the user can use the following keyboard
64
+ * shortcuts in order to perform a navigation:
65
+ * <br>
66
+ * <ul>
67
+ * <li>[TAB] - Move focus to the next interactive element after the <code>ui5-product-switch</code></li>
68
+ * <li>[UP/DOWN] - Navigates up and down the items </li>
69
+ * <li>[LEFT/RIGHT] - Navigates left and right the items</li>
70
+ * </ul>
71
+ * <br>
72
+ * <br>
73
+ *
49
74
* <h3>ES6 Module Import</h3>
50
75
* <code>import "@ui5/webcomponents-fiori/dist/ProductSwitch.js";</code>
51
76
* <br>
@@ -63,11 +88,15 @@ class ProductSwitch extends UI5Element {
63
88
constructor ( ) {
64
89
super ( ) ;
65
90
91
+ this . _currentIndex = 0 ;
92
+ this . _rowSize = 4 ;
93
+
66
94
this . _itemNavigation = new ItemNavigation ( this , {
67
- rowSize : 4 ,
68
- behavior : ItemNavigationBehavior . Cyclic ,
95
+ rowSize : this . _rowSize ,
69
96
getItemsCallback : ( ) => this . items ,
70
97
} ) ;
98
+
99
+ this . i18nBundle = getI18nBundle ( "@ui5/webcomponents" ) ;
71
100
}
72
101
73
102
static get metadata ( ) {
@@ -93,6 +122,14 @@ class ProductSwitch extends UI5Element {
93
122
} ;
94
123
}
95
124
125
+ static async onDefine ( ) {
126
+ await fetchI18nBundle ( "@ui5/webcomponents" ) ;
127
+ }
128
+
129
+ get _ariaLabelText ( ) {
130
+ return this . i18nBundle . getText ( PRODUCT_SWITCH_CONTAINER_LABEL ) ;
131
+ }
132
+
96
133
onEnterDOM ( ) {
97
134
this . _handleResizeBound = this . _handleResize . bind ( this ) ;
98
135
@@ -111,18 +148,45 @@ class ProductSwitch extends UI5Element {
111
148
const documentWidth = document . body . clientWidth ;
112
149
113
150
if ( documentWidth <= this . constructor . ROW_MIN_WIDTH . ONE_COLUMN ) {
114
- this . _itemNavigation . setRowSize ( 1 ) ;
151
+ this . _setRowSize ( 1 ) ;
115
152
} else if ( documentWidth <= this . constructor . ROW_MIN_WIDTH . THREE_COLUMN || this . items . length <= 6 ) {
116
- this . _itemNavigation . setRowSize ( 3 ) ;
153
+ this . _setRowSize ( 3 ) ;
117
154
} else {
118
- this . _itemNavigation . setRowSize ( 4 ) ;
155
+ this . _setRowSize ( 4 ) ;
119
156
}
120
157
}
121
158
122
159
_onfocusin ( event ) {
123
160
const target = event . target ;
124
161
125
162
this . _itemNavigation . setCurrentItem ( target ) ;
163
+ this . _currentIndex = this . items . indexOf ( target ) ;
164
+ }
165
+
166
+ _setRowSize ( size ) {
167
+ this . _rowSize = size ;
168
+ this . _itemNavigation . setRowSize ( size ) ;
169
+ }
170
+
171
+ _onkeydown ( event ) {
172
+ if ( isDown ( event ) ) {
173
+ this . _handleDown ( event ) ;
174
+ } else if ( isUp ( event ) ) {
175
+ this . _handleUp ( event ) ;
176
+ }
177
+ }
178
+
179
+ _handleDown ( event ) {
180
+ const itemsLength = this . items . length ;
181
+ if ( this . _currentIndex + this . _rowSize > itemsLength ) { // border reached, do nothing
182
+ event . stopPropagation ( ) ;
183
+ }
184
+ }
185
+
186
+ _handleUp ( event ) {
187
+ if ( this . _currentIndex - this . _rowSize < 0 ) { // border reached, do nothing
188
+ event . stopPropagation ( ) ;
189
+ }
126
190
}
127
191
}
128
192
0 commit comments