-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathSegmentedButton.ts
More file actions
355 lines (305 loc) · 9.47 KB
/
SegmentedButton.ts
File metadata and controls
355 lines (305 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import type { DefaultSlot } from "@ui5/webcomponents-base/dist/UI5Element.js";
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import event from "@ui5/webcomponents-base/dist/decorators/event-strict.js";
import slot from "@ui5/webcomponents-base/dist/decorators/slot-strict.js";
import {
getEffectiveAriaLabelText,
getAssociatedLabelForTexts,
getEffectiveAriaDescriptionText,
} from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";
import jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import type { ITabbable } from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
import {
isSpace,
isEnter,
isShift,
isEscape,
isSpaceShift,
} from "@ui5/webcomponents-base/dist/Keys.js";
import { SEGMENTEDBUTTON_ARIA_DESCRIPTION, SEGMENTEDBUTTON_ARIA_DESCRIBEDBY } from "./generated/i18n/i18n-defaults.js";
import "./SegmentedButtonItem.js";
import type SegmentedButtonItem from "./SegmentedButtonItem.js";
import SegmentedButtonSelectionMode from "./types/SegmentedButtonSelectionMode.js";
// Template
import SegmentedButtonTemplate from "./SegmentedButtonTemplate.js";
// Styles
import SegmentedButtonCss from "./generated/themes/SegmentedButton.css.js";
/**
* Interface for components that may be slotted inside `ui5-segmented-button` as items
* @public
*/
interface ISegmentedButtonItem extends UI5Element, ITabbable {
disabled: boolean,
selected: boolean,
}
type SegmentedButtonSelectionChangeEventDetail = {
selectedItems: Array<ISegmentedButtonItem>,
}
/**
* @class
*
* ### Overview
*
* The `ui5-segmented-button` shows a group of items. When the user clicks or taps
* one of the items, it stays in a pressed state. It automatically resizes the items
* to fit proportionally within the component. When no width is set, the component uses the available width.
*
*
* ### ES6 Module Import
*
* `import "@ui5/webcomponents/dist/SegmentedButton.js";`
* @constructor
* @extends UI5Element
* @since 1.0.0-rc.6
* @public
*/
@customElement({
tag: "ui5-segmented-button",
languageAware: true,
renderer: jsxRenderer,
template: SegmentedButtonTemplate,
styles: SegmentedButtonCss,
})
/**
* Fired when the selected item changes.
* @param {Array<ISegmentedButtonItem>} selectedItems an array of selected items. Since: 1.14.0
* @public
*/
@event("selection-change", {
bubbles: true,
})
class SegmentedButton extends UI5Element {
eventDetails!: {
"selection-change": SegmentedButtonSelectionChangeEventDetail,
}
/**
* Defines the accessible ARIA name of the component.
* @default undefined
* @public
* @since 1.0.3
*/
@property()
accessibleName?: string;
/**
* Defines the IDs of the HTML Elements that label the component.
* @default undefined
* @public
* @since 2.15.0
*/
@property()
accessibleNameRef?: string;
/**
* Defines the accessible description of the component.
* @default undefined
* @public
* @since 2.15.0
*/
@property()
accessibleDescription?: string;
/**
* Defines the IDs of the HTML Elements that describe the component.
* @default undefined
* @public
* @since 2.15.0
*/
@property()
accessibleDescriptionRef?: string;
/**
* Defines the component selection mode.
* @default "Single"
* @public
* @since 1.14.0
*/
@property()
selectionMode: `${SegmentedButtonSelectionMode}` = "Single";
/**
* Determines whether the segmented button items should be sized to fit their content.
*
* If set to `true`, each item will be sized to fit its content, with any extra space distributed after the last item.
* If set to `false` (the default), all items will be equally sized to fill the available space.
*
* @default false
* @public
* @since 2.16.0
*/
@property({ type: Boolean })
itemsFitContent: boolean = false;
/**
* Defines the items of `ui5-segmented-button`.
*
* **Note:** Multiple items are allowed.
*
* **Note:** Use the `ui5-segmented-button-item` for the intended design.
* @public
*/
@slot({ type: HTMLElement, invalidateOnChildChange: true, "default": true })
items!: DefaultSlot<ISegmentedButtonItem>;
@i18n("@ui5/webcomponents")
static i18nBundle: I18nBundle;
_itemNavigation: ItemNavigation;
hasPreviouslyFocusedItem: boolean;
_selectedItem?: ISegmentedButtonItem;
_cancelAction: boolean;
_isSpacePressed: boolean;
constructor() {
super();
this._itemNavigation = new ItemNavigation(this, {
getItemsCallback: () => this.navigatableItems,
});
this.hasPreviouslyFocusedItem = false;
this._cancelAction = false;
this._isSpacePressed = false;
}
onBeforeRendering() {
const items = this.getSlottedNodes<SegmentedButtonItem>("items");
const visibleItems = items.filter(item => !item.hidden);
let index = 1;
items.forEach(item => {
item.posInSet = item.hidden ? undefined : index++;
item.sizeOfSet = item.hidden ? undefined : visibleItems.length;
});
this.normalizeSelection();
if (!this.itemsFitContent) {
this.style.setProperty("--_ui5_segmented_btn_items_count", `${visibleItems.length}`);
}
}
normalizeSelection() {
if (!this.items.length) {
return;
}
switch (this.selectionMode) {
case SegmentedButtonSelectionMode.Single: {
const selectedItems = this.selectedItems;
const selectedItemIndex = this._selectedItem ? selectedItems.indexOf(this._selectedItem) : -1;
if (this._selectedItem && selectedItems.length > 1) {
selectedItems.splice(selectedItemIndex, 1);
}
const selectedItem = selectedItems.pop() || this.items[0];
this._applySingleSelection(selectedItem);
break;
}
default:
}
}
getFocusDomRef(): HTMLElement | undefined {
return this._itemNavigation._getCurrentItem();
}
_selectItem(e: MouseEvent | KeyboardEvent) {
const target = e.target as SegmentedButtonItem;
const isTargetSegmentedButtonItem = target.hasAttribute("ui5-segmented-button-item");
if (target.disabled || target === this.getDomRef() || !isTargetSegmentedButtonItem) {
return;
}
// Check if preventDefault was called on the native event (e.g., by item's semantic click handler)
if (e.defaultPrevented) {
return;
}
switch (this.selectionMode) {
case SegmentedButtonSelectionMode.Multiple:
target.selected = !target.selected;
break;
default:
this._applySingleSelection(target as unknown as ISegmentedButtonItem);
}
this.fireDecoratorEvent("selection-change", {
selectedItems: this.selectedItems,
});
this._itemNavigation.setCurrentItem(target);
return this;
}
_applySingleSelection(item: ISegmentedButtonItem) {
this.items.forEach(currentItem => {
currentItem.selected = false;
});
item.selected = true;
this._selectedItem = item;
}
_onclick(e: MouseEvent) {
this._selectItem(e);
}
_onkeydown(e: KeyboardEvent) {
if (isEnter(e)) {
this._selectItem(e);
} else if (isSpace(e)) {
e.preventDefault(); // Prevent scrolling
this._isSpacePressed = true;
} else if (isShift(e) || isEscape(e)) {
this._cancelAction = true; // Set the flag to cancel the action
}
}
_onkeyup(e: KeyboardEvent) {
const isSpaceKey = isSpace(e);
const isCancelKey = isShift(e) || isEscape(e);
if (isSpaceKey || isSpaceShift(e)) {
if (this._cancelAction) {
this._cancelAction = false;
this._isSpacePressed = false;
e.preventDefault();
return;
}
this._isSpacePressed = false;
} else if (isCancelKey && !this._isSpacePressed) {
this._cancelAction = false;
}
if (isSpaceKey) {
this._selectItem(e);
}
}
_onmousedown(e: MouseEvent) {
const eventTarget = e.target as SegmentedButtonItem;
const isTargetSegmentedButtonItem = eventTarget.hasAttribute("ui5-segmented-button-item");
if (isTargetSegmentedButtonItem) {
this._itemNavigation.setCurrentItem(eventTarget);
this.hasPreviouslyFocusedItem = true;
}
}
_onfocusin(e: FocusEvent) {
// If the component was previously focused,
// update the ItemNavigation to sync the button's tabindex values
if (this.hasPreviouslyFocusedItem) {
this._itemNavigation.setCurrentItem(e.target as SegmentedButtonItem);
return;
}
// If the component is focused for the first time
// focus the selected item if such is present
if (this.selectedItems.length) {
this._itemNavigation.setCurrentItem(this.selectedItems[0]);
this.selectedItems[0].focus();
this.hasPreviouslyFocusedItem = true;
}
}
/**
* Returns an array of the currently selected items.
* @since 1.14.0
* @public
* @default []
*/
get selectedItems(): Array<ISegmentedButtonItem> {
return this.items.filter(item => item.selected);
}
get navigatableItems() {
return this.getSlottedNodes<SegmentedButtonItem>("items").filter(item => {
return !item.disabled;
});
}
get ariaLabelText() {
return getEffectiveAriaLabelText(this) || getAssociatedLabelForTexts(this) || undefined;
}
get ariaDescriptionText() {
return `${(getEffectiveAriaDescriptionText(this) || "")} ${SegmentedButton.i18nBundle.getText(SEGMENTEDBUTTON_ARIA_DESCRIBEDBY)}`.trim();
}
get ariaRoleDescription() {
return SegmentedButton.i18nBundle.getText(SEGMENTEDBUTTON_ARIA_DESCRIPTION);
}
}
SegmentedButton.define();
export default SegmentedButton;
export type {
SegmentedButtonSelectionChangeEventDetail,
ISegmentedButtonItem,
};