forked from SAP/ui5-webcomponents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegmentedButton.js
208 lines (175 loc) · 5.14 KB
/
SegmentedButton.js
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
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
// Template
import SegmentedButtonTemplate from "./generated/templates/SegmentedButtonTemplate.lit.js";
// Styles
import SegmentedButtonCss from "./generated/themes/SegmentedButton.css.js";
/**
* @public
*/
const metadata = {
tag: "ui5-segmentedbutton",
properties: /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ {},
managedSlots: true,
slots: /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ {
/**
* Defines the buttons of <code>ui5-segmentedbutton</code>.
* <br><br>
* <b>Note:</b> Multiple buttons are allowed.
* <br><br>
* <b>Note:</b> Use the <code>ui5-togglebutton</code> for the intended design.
* @type {HTMLElement[]}
* @slot
* @public
*/
"default": {
propertyName: "buttons",
type: HTMLElement,
},
},
events: /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ {
/**
* Fired when the selected button changes.
*
* @event
* @param {HTMLElement} selectedButton the pressed button.
* @public
*/
selectionChange: {
detail: {
selectedButton: { type: HTMLElement },
},
},
},
};
/**
* @class
*
* <h3 class="comment-api-title">Overview</h3>
*
* The <code>ui5-segmentedbutton</code> shows a group of buttons. When the user clicks or taps
* one of the buttons, it stays in a pressed state. It automatically resizes the buttons
* to fit proportionally within the component. When no width is set, the component uses the available width.
* <br><br>
* <b>Note:</b> There can be just one selected <code>button</code> at a time.
*
* <h3>ES6 Module Import</h3>
*
* <code>import "@ui5/webcomponents/dist/SegmentedButton";</code>
*
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.SegmentedButton
* @extends sap.ui.webcomponents.base.UI5Element
* @tagname ui5-segmentedbutton
* @since 1.0.0-rc.6
* @public
*/
class SegmentedButton extends UI5Element {
static get metadata() {
return metadata;
}
static get render() {
return litRender;
}
static get template() {
return SegmentedButtonTemplate;
}
static get styles() {
return SegmentedButtonCss;
}
constructor() {
super();
this.initItemNavigation();
this.absoluteWidthSet = false; // set to true whenever we set absolute width to the component
this.percentageWidthSet = false; // set to true whenever we set 100% width to the component
this.hasPreviouslyFocusedItem = false;
this._handleResizeBound = this._handleResize.bind(this);
}
onEnterDOM() {
ResizeHandler.register(this.parentNode, this._handleResizeBound);
}
onExitDOM() {
ResizeHandler.deregister(this.parentNode, this._handleResizeBound);
}
onBeforeRendering() {
this.normalizeSelection();
}
async onAfterRendering() {
await Promise.all(this.buttons.map(button => button._waitForDomRef));
this.widths = this.buttons.map(button => button.offsetWidth);
}
initItemNavigation() {
this._itemNavigation = new ItemNavigation(this);
this._itemNavigation.getItemsCallback = () => this.getSlottedNodes("buttons");
}
normalizeSelection() {
this._selectedButton = this.buttons.filter(button => button.pressed).pop();
if (this._selectedButton) {
this.buttons.forEach(button => {
button.pressed = false;
});
this._selectedButton.pressed = true;
}
}
_onclick(event) {
if (event.target.disabled || event.target === this.getDomRef()) {
return;
}
if (event.target !== this._selectedButton) {
if (this._selectedButton) {
this._selectedButton.pressed = false;
}
this._selectedButton = event.target;
this.fireEvent("selectionChange", {
selectedButton: this._selectedButton,
});
}
this._selectedButton.pressed = true;
this._itemNavigation.update(this._selectedButton);
return this;
}
_onfocusin(event) {
// If the component was previously focused,
// update the ItemNavigation to sync butons` tabindex values
if (this.hasPreviouslyFocusedItem) {
this._itemNavigation.update(event.target);
return;
}
// If the component is focused for the first time
// focus the selected item if such present
if (this.selectedButton) {
this.selectedButton.focus();
this._itemNavigation.update(this._selectedButton);
this.hasPreviouslyFocusedItem = true;
}
}
_handleResize() {
const parentWidth = this.parentNode.offsetWidth;
if (!this.style.width || this.percentageWidthSet) {
this.style.width = `${Math.max(...this.widths) * this.buttons.length}px`;
this.absoluteWidthSet = true;
}
this.buttons.forEach(button => {
button.style.width = "100%";
});
if (parentWidth <= this.offsetWidth && this.absoluteWidthSet) {
this.style.width = "100%";
this.percentageWidthSet = true;
}
}
/**
* Currently selected button.
*
* @readonly
* @type { ui5-togglebutton }
* @public
*/
get selectedButton() {
return this._selectedButton;
}
}
SegmentedButton.define();
export default SegmentedButton;