-
Notifications
You must be signed in to change notification settings - Fork 253
/
RadioButton.js
418 lines (358 loc) · 9.99 KB
/
RadioButton.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import { isDesktop } from "@ui5/webcomponents-base/dist/Device.js";
import { getCompactSize } from "@ui5/webcomponents-base/dist/config/CompactSize.js";
import { getRTL } from "@ui5/webcomponents-base/dist/config/RTL.js";
import { getFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js";
import {
isSpace,
isEnter,
isDown,
isLeft,
isUp,
isRight,
} from "@ui5/webcomponents-base/dist/events/PseudoEvents.js";
import Label from "./Label.js";
import RadioButtonGroup from "./RadioButtonGroup.js";
// Template
import RadioButtonTemplate from "./generated/templates/RadioButtonTemplate.lit.js";
// i18n
import { VALUE_STATE_ERROR, VALUE_STATE_WARNING } from "./generated/i18n/i18n-defaults.js";
// Styles
import radioButtonCss from "./generated/themes/RadioButton.css.js";
/**
* @public
*/
const metadata = {
tag: "ui5-radiobutton",
properties: /** @lends sap.ui.webcomponents.main.RadioButton.prototype */ {
/**
* Determines whether the <code>ui5-radiobutton</code> is disabled.
* <br><br>
* <b>Note:</b> A disabled <code>ui5-radiobutton</code> is completely uninteractive.
*
* @type {boolean}
* @defaultvalue false
* @public
*/
disabled: {
type: Boolean,
},
/**
* Determines whether the <code>ui5-radiobutton</code> is read-only.
* <br><br>
* <b>Note:</b> A read-only <code>ui5-radiobutton</code> is not editable,
* but still provides visual feedback upon user interaction.
*
* @type {boolean}
* @defaultvalue false
* @public
*/
readonly: {
type: Boolean,
},
/**
* Determines whether the <code>ui5-radiobutton</code> is selected or not.
* <br><br>
* <b>Note:</b> The property value can be changed with user interaction,
* either by cliking/tapping on the <code>ui5-radiobutton</code>,
* or by using the Space or Enter key.
*
* @type {boolean}
* @defaultvalue false
* @public
*/
selected: {
type: Boolean,
},
/**
* Defines the text of the <code>ui5-radiobutton</code>.
*
* @type {string}
* @public
*/
text: {
type: String,
},
/**
* Defines the value state of the <code>ui5-radiobutton</code>.
* Available options are <code>Warning</code>, <code>Error</code>, and
* <code>None</code> (by default).
* <br><br>
* <b>Note:</b> Using the value states affects the visual appearance of
* the <code>ui5-radiobutton</code>.
*
* @type {string}
* @defaultvalue "None"
* @public
*/
valueState: {
defaultValue: ValueState.None,
type: ValueState,
},
/**
* Defines the name of the <code>ui5-radiobutton</code>.
* Radio buttons with the same <code>name</code> will form a radio button group.
* <br><b>Note:</b>
* The selection can be changed with <code>ARROW_UP/DOWN</code> and <code>ARROW_LEFT/RIGHT</code> keys between radios in same group.
* <br><b>Note:</b>
* Only one radio button can be selected per group.
* <br>
* <b>Important:</b> For the <code>name</code> property to have effect when submitting forms, you must add the following import to your project:
* <code>import "@ui5/webcomponents/dist/features/InputElementsFormSupport.js";</code>
*
* <b>Note:</b> When set, a native <code>input</code> HTML element
* will be created inside the <code>ui5-radiobutton</code> so that it can be submitted as
* part of an HTML form.
*
* @type {string}
* @defaultvalue: ""
* @public
*/
name: {
type: String,
},
/**
* Defines the form value of the <code>ui5-radiobutton</code>.
* When a form with a radio button group is submitted, the group's value
* will be the value of the currently selected radio button.
* <br>
* <b>Important:</b> For the <code>value</code> property to have effect, you must add the following import to your project:
* <code>import "@ui5/webcomponents/dist/features/InputElementsFormSupport.js";</code>
*
* @type {string}
* @defaultvalue: ""
* @public
*/
value: {
type: String,
},
/**
* Defines whether the <code>ui5-radiobutton</code> text wraps when there is not enough space.
* <br><br>
* <b>Note:</b> By default, the text truncates when there is not enough space.
*
* @type {boolean}
* @defaultvalue false
* @public
*/
wrap: {
type: Boolean,
},
},
events: /** @lends sap.ui.webcomponents.main.RadioButton.prototype */ {
/**
* Fired when the <code>ui5-radiobutton</code> selected state changes.
*
* @event
* @public
*/
select: {},
},
invalidateOnContentDensityChange: true,
};
const SVGConfig = {
"compact": {
x: 16,
y: 16,
rInner: 3,
rOuter: 8,
},
"default": {
x: 22,
y: 22,
rInner: 5,
rOuter: 11,
},
};
/**
* @class
*
* <h3 class="comment-api-title">Overview</h3>
*
* The <code>ui5-radibutton</code> component enables users to select a single option from a set of options.
* When a <code>ui5-radiobutton</code> is selected by the user, the
* <code>select</code> event is fired.
* When a <code>ui5-radiobutton</code> that is within a group is selected, the one
* that was previously selected gets automatically deselected. You can group radio buttons by using the <code>name</code> property.
* <br>
* Note: if <code>ui5-radiobutton</code> is not part of a group, it can be selected once, but can not be deselected back.
*
* <h3>Keyboard Handling</h3>
*
* Once the <code>ui5-radiobutton</code> is on focus, it might be selected by pressing the Space and Enter keys.
* <br>
* The Arrow Down/Arrow Up and Arrow Left/Arrow Right keys can be used to change selection between next/previous radio buttons in one group,
* while TAB and SHIFT + TAB can be used to enter or leave the radio button group.
* <br>
* Note: On entering radio button group, the focus goes to the currently selected radio button.
*
* <h3>ES6 Module Import</h3>
*
* <code>import "@ui5/webcomponents/dist/RadioButton";</code>
*
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.RadioButton
* @extends sap.ui.webcomponents.base.UI5Element
* @tagname ui5-radiobutton
* @public
*/
class RadioButton extends UI5Element {
constructor() {
super();
this.i18nBundle = getI18nBundle("@ui5/webcomponents");
}
static get metadata() {
return metadata;
}
static get render() {
return litRender;
}
static get template() {
return RadioButtonTemplate;
}
static get styles() {
return radioButtonCss;
}
static async define(...params) {
await Promise.all([
Label.define(),
fetchI18nBundle("@ui5/webcomponents"),
]);
super.define(...params);
}
onBeforeRendering() {
this.syncGroup();
this._enableFormSupport();
}
syncGroup() {
const oldGroup = this._name;
const currentGroup = this.name;
if (currentGroup !== oldGroup) {
if (oldGroup) {
// remove the control from the previous group
RadioButtonGroup.removeFromGroup(this, oldGroup);
}
if (currentGroup) {
// add the control to the existing group
RadioButtonGroup.addToGroup(this, currentGroup);
}
} else if (currentGroup) {
RadioButtonGroup.enforceSingleSelection(this, currentGroup);
}
this._name = this.name;
}
_enableFormSupport() {
const FormSupport = getFeature("FormSupport");
if (FormSupport) {
FormSupport.syncNativeHiddenInput(this, (element, nativeInput) => {
nativeInput.disabled = element.disabled || !element.selected;
nativeInput.value = element.selected ? element.value : "";
});
} else if (this.value) {
console.warn(`In order for the "value" property to have effect, you should also: import "@ui5/webcomponents/dist/features/InputElementsFormSupport.js";`); // eslint-disable-line
}
}
_onclick() {
return this.toggle();
}
_handleDown(event) {
const currentGroup = this.name;
if (!currentGroup) {
return;
}
event.preventDefault();
RadioButtonGroup.selectNextItem(this, currentGroup);
}
_handleUp(event) {
const currentGroup = this.name;
if (!currentGroup) {
return;
}
event.preventDefault();
RadioButtonGroup.selectPreviousItem(this, currentGroup);
}
_onkeydown(event) {
if (isSpace(event)) {
return event.preventDefault();
}
if (isEnter(event)) {
return this.toggle();
}
if (isDown(event) || isRight(event)) {
this._handleDown(event);
}
if (isUp(event) || isLeft(event)) {
this._handleUp(event);
}
}
_onkeyup(event) {
if (isSpace(event)) {
this.toggle();
}
}
toggle() {
if (!this.canToggle()) {
return this;
}
if (!this.name) {
this.selected = !this.selected;
this.fireEvent("select");
return this;
}
RadioButtonGroup.selectItem(this, this.name);
return this;
}
canToggle() {
return !(this.disabled || this.readonly || this.selected);
}
valueStateTextMappings() {
const i18nBundle = this.i18nBundle;
return {
"Error": i18nBundle.getText(VALUE_STATE_ERROR),
"Warning": i18nBundle.getText(VALUE_STATE_WARNING),
};
}
get classes() {
return {
inner: {
"ui5-radio-inner--hoverable": !this.disabled && !this.readonly && isDesktop(),
},
};
}
get ariaReadonly() {
return this.readonly ? "true" : undefined;
}
get ariaDisabled() {
return this.disabled ? "true" : undefined;
}
get ariaLabelledBy() {
return this.text ? `${this._id}-label` : undefined;
}
get ariaDescribedBy() {
return this.hasValueState ? `${this._id}-descr` : undefined;
}
get hasValueState() {
return this.valueState !== ValueState.None;
}
get valueStateText() {
return this.valueStateTextMappings()[this.valueState];
}
get tabIndex() {
return this.disabled || (!this.selected && this.name) ? "-1" : "0";
}
get strokeWidth() {
return this.valueState === "None" ? "1" : "2";
}
get circle() {
return getCompactSize() ? SVGConfig.compact : SVGConfig.default;
}
get rtl() {
return getRTL() ? "rtl" : undefined;
}
}
RadioButton.define();
export default RadioButton;