@@ -5,8 +5,9 @@ import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18
5
5
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js" ;
6
6
import { renderFinished } from "@ui5/webcomponents-base/dist/Render.js" ;
7
7
import { isIE } from "@ui5/webcomponents-base/dist/Device.js" ;
8
- import { SEGMENTEDBUTTON_ARIA_DESCRIPTION } from "./generated/i18n/i18n-defaults.js" ;
9
- import ToggleButton from "./ToggleButton.js" ;
8
+ import { isSpace , isEnter } from "@ui5/webcomponents-base/dist/Keys.js" ;
9
+ import { SEGMENTEDBUTTON_ARIA_DESCRIPTION , SEGMENTEDBUTTON_ARIA_DESCRIBEDBY } from "./generated/i18n/i18n-defaults.js" ;
10
+ import SegmentedButtonItem from "./SegmentedButtonItem.js" ;
10
11
11
12
// Template
12
13
import SegmentedButtonTemplate from "./generated/templates/SegmentedButtonTemplate.lit.js" ;
@@ -26,32 +27,32 @@ const metadata = {
26
27
slots : /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ {
27
28
28
29
/**
29
- * Defines the buttons of component .
30
+ * Defines the items of <code>ui5-segmented-button</code> .
30
31
* <br><br>
31
- * <b>Note:</b> Multiple buttons are allowed.
32
+ * <b>Note:</b> Multiple items are allowed.
32
33
* <br><br>
33
- * <b>Note:</b> Use the <code>ui5-toggle -button</code> for the intended design.
34
+ * <b>Note:</b> Use the <code>ui5-segmented -button-item </code> for the intended design.
34
35
* @type {sap.ui.webcomponents.main.IButton[] }
35
- * @slot buttons
36
+ * @slot items
36
37
* @public
37
38
*/
38
39
"default" : {
39
- propertyName : "buttons " ,
40
+ propertyName : "items " ,
40
41
type : HTMLElement ,
41
42
} ,
42
43
} ,
43
44
events : /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ {
44
45
45
46
/**
46
- * Fired when the selected button changes.
47
+ * Fired when the selected item changes.
47
48
*
48
49
* @event sap.ui.webcomponents.main.SegmentedButton#selection-change
49
- * @param {HTMLElement } selectedButton the pressed button .
50
+ * @param {HTMLElement } selectedItem the pressed item .
50
51
* @public
51
52
*/
52
53
"selection-change" : {
53
54
detail : {
54
- selectedButton : { type : HTMLElement } ,
55
+ selectedItem : { type : HTMLElement } ,
55
56
} ,
56
57
} ,
57
58
} ,
@@ -62,11 +63,11 @@ const metadata = {
62
63
*
63
64
* <h3 class="comment-api-title">Overview</h3>
64
65
*
65
- * The <code>ui5-segmented-button</code> shows a group of buttons . When the user clicks or taps
66
- * one of the buttons , it stays in a pressed state. It automatically resizes the buttons
66
+ * The <code>ui5-segmented-button</code> shows a group of items . When the user clicks or taps
67
+ * one of the items , it stays in a pressed state. It automatically resizes the items
67
68
* to fit proportionally within the component. When no width is set, the component uses the available width.
68
69
* <br><br>
69
- * <b>Note:</b> There can be just one selected <code>button </code> at a time.
70
+ * <b>Note:</b> There can be just one selected <code>item </code> at a time.
70
71
*
71
72
* <h3>ES6 Module Import</h3>
72
73
*
@@ -78,6 +79,7 @@ const metadata = {
78
79
* @extends sap.ui.webcomponents.base.UI5Element
79
80
* @tagname ui5-segmented-button
80
81
* @since 1.0.0-rc.6
82
+ * @appenddocs SegmentedButtonItem
81
83
* @public
82
84
*/
83
85
class SegmentedButton extends UI5Element {
@@ -98,7 +100,7 @@ class SegmentedButton extends UI5Element {
98
100
}
99
101
100
102
static get dependencies ( ) {
101
- return [ ToggleButton ] ;
103
+ return [ SegmentedButtonItem ] ;
102
104
}
103
105
104
106
static async onDefine ( ) {
@@ -109,7 +111,7 @@ class SegmentedButton extends UI5Element {
109
111
super ( ) ;
110
112
111
113
this . _itemNavigation = new ItemNavigation ( this , {
112
- getItemsCallback : ( ) => this . getSlottedNodes ( "buttons " ) ,
114
+ getItemsCallback : ( ) => this . getSlottedNodes ( "items " ) ,
113
115
} ) ;
114
116
115
117
this . absoluteWidthSet = false ; // set to true whenever we set absolute width to the component
@@ -129,31 +131,38 @@ class SegmentedButton extends UI5Element {
129
131
}
130
132
131
133
onBeforeRendering ( ) {
134
+ const items = this . getSlottedNodes ( "items" ) ;
135
+
136
+ items . forEach ( ( item , index , arr ) => {
137
+ item . posInSet = index + 1 ;
138
+ item . sizeOfSet = arr . length ;
139
+ } ) ;
140
+
132
141
this . normalizeSelection ( ) ;
133
142
}
134
143
135
144
async onAfterRendering ( ) {
136
145
await this . _doLayout ( ) ;
137
146
}
138
147
139
- prepareToMeasureButtons ( ) {
148
+ prepareToMeasureItems ( ) {
140
149
this . style . width = "" ;
141
- this . buttons . forEach ( button => {
142
- button . style . width = "" ;
150
+ this . items . forEach ( item => {
151
+ item . style . width = "" ;
143
152
} ) ;
144
153
}
145
154
146
- async measureButtonsWidth ( ) {
155
+ async measureItemsWidth ( ) {
147
156
await renderFinished ( ) ;
148
- this . prepareToMeasureButtons ( ) ;
157
+ this . prepareToMeasureItems ( ) ;
149
158
150
- this . widths = this . buttons . map ( button => {
159
+ this . widths = this . items . map ( item => {
151
160
// +1 is added because for width 100.44px the offsetWidth property returns 100px and not 101px
152
- let width = button . offsetWidth + 1 ;
161
+ let width = item . offsetWidth + 1 ;
153
162
154
163
if ( isIE ( ) ) {
155
- // in IE we are adding 1 one px beacause the width of the border on a button in the middle is not calculated and if the
156
- // longest button is in the middle, it truncates
164
+ // in IE we are adding 1 one px beacause the width of the border on an item in the middle is not calculated and if the
165
+ // longest item is in the middle, it truncates
157
166
width += 1 ;
158
167
}
159
168
@@ -162,37 +171,55 @@ class SegmentedButton extends UI5Element {
162
171
}
163
172
164
173
normalizeSelection ( ) {
165
- this . _selectedButton = this . buttons . filter ( button => button . pressed ) . pop ( ) ;
174
+ this . _selectedItem = this . items . filter ( item => item . pressed ) . pop ( ) ;
166
175
167
- if ( this . _selectedButton ) {
168
- this . buttons . forEach ( button => {
169
- button . pressed = false ;
176
+ if ( this . _selectedItem ) {
177
+ this . items . forEach ( item => {
178
+ item . pressed = false ;
170
179
} ) ;
171
- this . _selectedButton . pressed = true ;
180
+ this . _selectedItem . pressed = true ;
172
181
}
173
182
}
174
183
175
- _onclick ( event ) {
184
+ _selectItem ( event ) {
176
185
if ( event . target . disabled || event . target === this . getDomRef ( ) ) {
177
186
return ;
178
187
}
179
188
180
- if ( event . target !== this . _selectedButton ) {
181
- if ( this . _selectedButton ) {
182
- this . _selectedButton . pressed = false ;
189
+ if ( event . target !== this . _selectedItem ) {
190
+ if ( this . _selectedItem ) {
191
+ this . _selectedItem . pressed = false ;
183
192
}
184
- this . _selectedButton = event . target ;
193
+ this . _selectedItem = event . target ;
185
194
this . fireEvent ( "selection-change" , {
186
- selectedButton : this . _selectedButton ,
195
+ selectedItem : this . _selectedItem ,
187
196
} ) ;
188
197
}
189
198
190
- this . _selectedButton . pressed = true ;
191
- this . _itemNavigation . setCurrentItem ( this . _selectedButton ) ;
199
+ this . _selectedItem . pressed = true ;
200
+ this . _itemNavigation . setCurrentItem ( this . _selectedItem ) ;
192
201
193
202
return this ;
194
203
}
195
204
205
+ _onclick ( event ) {
206
+ this . _selectItem ( event ) ;
207
+ }
208
+
209
+ _onkeydown ( event ) {
210
+ if ( isEnter ( event ) ) {
211
+ this . _selectItem ( event ) ;
212
+ } else if ( isSpace ( event ) ) {
213
+ event . preventDefault ( ) ;
214
+ }
215
+ }
216
+
217
+ _onkeyup ( event ) {
218
+ if ( isSpace ( event ) ) {
219
+ this . _selectItem ( event ) ;
220
+ }
221
+ }
222
+
196
223
_onfocusin ( event ) {
197
224
// If the component was previously focused,
198
225
// update the ItemNavigation to sync butons` tabindex values
@@ -203,28 +230,28 @@ class SegmentedButton extends UI5Element {
203
230
204
231
// If the component is focused for the first time
205
232
// focus the selected item if such present
206
- if ( this . selectedButton ) {
207
- this . selectedButton . focus ( ) ;
208
- this . _itemNavigation . setCurrentItem ( this . _selectedButton ) ;
233
+ if ( this . selectedItem ) {
234
+ this . selectedItem . focus ( ) ;
235
+ this . _itemNavigation . setCurrentItem ( this . _selectedItem ) ;
209
236
this . hasPreviouslyFocusedItem = true ;
210
237
}
211
238
}
212
239
213
240
async _doLayout ( ) {
214
- const buttonsHaveWidth = this . widths && this . widths . some ( button => button . offsetWidth > 2 ) ; // 2 are the pixel's added for rounding & IE
215
- if ( ! buttonsHaveWidth ) {
216
- await this . measureButtonsWidth ( ) ;
241
+ const itemsHaveWidth = this . widths && this . widths . some ( item => item . offsetWidth > 2 ) ; // 2 are the pixel's added for rounding & IE
242
+ if ( ! itemsHaveWidth ) {
243
+ await this . measureItemsWidth ( ) ;
217
244
}
218
245
219
246
const parentWidth = this . parentNode . offsetWidth ;
220
247
221
248
if ( ! this . style . width || this . percentageWidthSet ) {
222
- this . style . width = `${ Math . max ( ...this . widths ) * this . buttons . length } px` ;
249
+ this . style . width = `${ Math . max ( ...this . widths ) * this . items . length } px` ;
223
250
this . absoluteWidthSet = true ;
224
251
}
225
252
226
- this . buttons . forEach ( button => {
227
- button . style . width = "100%" ;
253
+ this . items . forEach ( item => {
254
+ item . style . width = "100%" ;
228
255
} ) ;
229
256
230
257
if ( parentWidth <= this . offsetWidth && this . absoluteWidthSet ) {
@@ -234,14 +261,18 @@ class SegmentedButton extends UI5Element {
234
261
}
235
262
236
263
/**
237
- * Currently selected button .
264
+ * Currently selected item .
238
265
*
239
266
* @readonly
240
- * @type { ui5-toggle -button }
267
+ * @type { ui5-segmented -button-item }
241
268
* @public
242
269
*/
243
- get selectedButton ( ) {
244
- return this . _selectedButton ;
270
+ get selectedItem ( ) {
271
+ return this . _selectedItem ;
272
+ }
273
+
274
+ get ariaDescribedBy ( ) {
275
+ return this . i18nBundle . getText ( SEGMENTEDBUTTON_ARIA_DESCRIBEDBY ) ;
245
276
}
246
277
247
278
get ariaDescription ( ) {
0 commit comments