-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathCheckBox.ts
More file actions
500 lines (435 loc) · 12.8 KB
/
CheckBox.ts
File metadata and controls
500 lines (435 loc) · 12.8 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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import { isDesktop } from "@ui5/webcomponents-base/dist/Device.js";
import UI5Element 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 jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js";
import { getEffectiveAriaLabelText, getAssociatedLabelForTexts } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
import type { IFormInputElement } from "@ui5/webcomponents-base/dist/features/InputElementsFormSupport.js";
import type WrappingType from "./types/WrappingType.js";
import type {
AriaRole,
AriaChecked,
AriaDisabled,
AriaReadonly,
} from "@ui5/webcomponents-base/dist/types.js";
import {
VALUE_STATE_ERROR,
VALUE_STATE_WARNING,
VALUE_STATE_SUCCESS,
FORM_CHECKABLE_REQUIRED,
CHECKBOX_CHECKED,
CHECKBOX_NOT_CHECKED,
CHECKBOX_ARIA_TYPE,
} from "./generated/i18n/i18n-defaults.js";
// Styles
import checkboxCss from "./generated/themes/CheckBox.css.js";
// Template
import CheckBoxTemplate from "./CheckBoxTemplate.js";
let isGlobalHandlerAttached = false;
let activeCb: CheckBox;
type CheckBoxAccInfo = {
role?: AriaRole,
ariaChecked?: AriaChecked,
ariaReadonly?: AriaReadonly,
ariaDisabled?: AriaDisabled,
ariaRequired?: boolean,
tabindex?: number | undefined,
}
/**
* @class
*
* ### Overview
*
* Allows the user to set a binary value, such as true/false or yes/no for an item.
*
* The `ui5-checkbox` component consists of a box and a label that describes its purpose.
* If it's checked, an indicator is displayed inside the box.
* To check/uncheck the `ui5-checkbox`, the user has to click or tap the square
* box or its label.
*
* The `ui5-checkbox` component only has 2 states - checked and unchecked.
* Clicking or tapping toggles the `ui5-checkbox` between checked and unchecked state.
*
* ### Usage
*
* You can define the checkbox text with via the `text` property. If the text exceeds the available width, it is truncated by default.
* In case you prefer text to truncate, set the `wrappingType` property to "None".
* The touchable area for toggling the `ui5-checkbox` ends where the text ends.
*
* You can disable the `ui5-checkbox` by setting the `disabled` property to
* `true`,
* or use the `ui5-checkbox` in read-only mode by setting the `readonly`
* property to `true`.
*
* ### Keyboard Handling
*
* The user can use the following keyboard shortcuts to toggle the checked state of the `ui5-checkbox`.
*
* - [Space],[Enter] - Toggles between different states: checked, not checked.
*
* ### ES6 Module Import
*
* `import "@ui5/webcomponents/dist/CheckBox.js";`
* @constructor
* @extends UI5Element
* @public
* @csspart root - Used to style the outermost wrapper of the `ui5-checkbox`
* @csspart label - Used to style the label of the `ui5-checkbox`
* @csspart icon - Used to style the icon of the `ui5-checkbox`
*/
@customElement({
tag: "ui5-checkbox",
languageAware: true,
formAssociated: true,
renderer: jsxRenderer,
template: CheckBoxTemplate,
styles: checkboxCss,
})
/**
* Fired when the component checked state changes.
* @public
*/
@event("change", {
bubbles: true,
cancelable: true,
})
/**
* Fired to make Angular two way data binding work properly.
* @private
*/
@event("value-changed", {
bubbles: true,
cancelable: true,
})
class CheckBox extends UI5Element implements IFormInputElement {
eventDetails!: {
"change": void,
"value-changed": void,
};
/**
* Receives id(or many ids) of the elements that label the component
* @default undefined
* @public
* @since 1.1.0
*/
@property()
accessibleNameRef?: string;
/**
* Defines the accessible ARIA name of the component.
* @public
* @default undefined
* @since 1.1.0
*/
@property()
accessibleName?: string;
/**
* Defines whether the component is disabled.
*
* **Note:** A disabled component is completely noninteractive.
* @default false
* @public
*/
@property({ type: Boolean })
disabled = false;
/**
* Defines whether the component is read-only.
*
* **Note:** A read-only component is not editable,
* but still provides visual feedback upon user interaction.
* @default false
* @public
*/
@property({ type: Boolean })
readonly = false;
/**
* Determines whether the `ui5-checkbox` is in display only state.
*
* When set to `true`, the `ui5-checkbox` is not interactive, not editable, not focusable
* and not in the tab chain. This setting is used for forms in review mode.
*
* **Note:** When the property `disabled` is set to `true` this property has no effect.
* @since 1.22.0
* @public
* @default false
*/
@property({ type: Boolean })
displayOnly = false;
/**
* Defines whether the component is required.
*
* **Note:** We advise against using the text property of the checkbox when there is a
* label associated with it to avoid having two required asterisks.
* @default false
* @public
* @since 1.3.0
*/
@property({ type: Boolean })
required = false;
/**
* Defines whether the component is displayed as partially checked.
*
* **Note:** The indeterminate state can be set only programmatically and can’t be achieved by user
* interaction and the resulting visual state depends on the values of the `indeterminate`
* and `checked` properties:
*
* - If the component is checked and indeterminate, it will be displayed as partially checked
* - If the component is checked and it is not indeterminate, it will be displayed as checked
* - If the component is not checked, it will be displayed as not checked regardless value of the indeterminate attribute
* @default false
* @public
* @since 1.0.0-rc.15
*/
@property({ type: Boolean })
indeterminate = false;
/**
* Defines if the component is checked.
*
* **Note:** The property can be changed with user interaction,
* either by cliking/tapping on the component, or by
* pressing the Enter or Space key.
* @default false
* @formEvents change
* @formProperty
* @public
*/
@property({ type: Boolean })
checked = false;
/**
* Defines the text of the component.
* @default undefined
* @public
*/
@property()
text?: string;
/**
* Defines the value state of the component.
* @default "None"
* @public
*/
@property()
valueState: `${ValueState}` = "None";
/**
* Defines whether the component text wraps when there is not enough space.
*
* **Note:** for option "Normal" the text will wrap and the words will not be broken based on hyphenation.
* **Note:** for option "None" the text will be truncated with an ellipsis.
* @default "Normal"
* @public
*/
@property()
wrappingType: `${WrappingType}` = "Normal";
/**
* Determines the name by which the component will be identified upon submission in an HTML form.
*
* **Note:** This property is only applicable within the context of an HTML Form element.
* @default undefined
* @public
*/
@property()
name?: string;
/**
* Defines the form value of the component that is submitted when the checkbox is checked.
*
* When a form containing `ui5-checkbox` elements is submitted, only the values of the
* **checked** checkboxes are included in the form data sent to the server. Unchecked
* checkboxes do not contribute any data to the form submission.
*
* This property is particularly useful for **checkbox groups**, where multiple checkboxes with the same `name` but different `value` properties can be used to represent a set of related options.
*
* @default "on"
* @public
*/
@property()
value = "on";
/**
* Defines the active state (pressed or not) of the component.
* @private
*/
@property({ type: Boolean })
active = false;
/**
* Defines custom aria implementation object.
* @private
*/
@property({ type: Object })
_accInfo?: CheckBoxAccInfo;
@i18n("@ui5/webcomponents")
static i18nBundle: I18nBundle;
_deactivate: () => void;
get formValidityMessage() {
return CheckBox.i18nBundle.getText(FORM_CHECKABLE_REQUIRED);
}
get formValidity(): ValidityStateFlags {
return { valueMissing: this.required && !this.checked };
}
async formElementAnchor() {
return this.getFocusDomRefAsync();
}
get formFormattedValue() {
return this.checked ? this.value : null;
}
constructor() {
super();
this._deactivate = () => {
if (activeCb) {
activeCb.active = false;
}
};
if (!isGlobalHandlerAttached) {
document.addEventListener("mouseup", this._deactivate);
isGlobalHandlerAttached = true;
}
}
onEnterDOM() {
if (isDesktop()) {
this.setAttribute("desktop", "");
}
}
_onclick() {
this.toggle();
}
_onmousedown() {
if (this.readonly || this.disabled) {
return;
}
this.active = true;
activeCb = this; // eslint-disable-line
}
_onmouseup() {
this.active = false;
}
_onfocusout() {
this.active = false;
}
_onkeydown(e: KeyboardEvent) {
if (isSpace(e)) {
e.preventDefault();
}
if (this.readonly || this.disabled) {
return;
}
if (isEnter(e)) {
this.toggle();
}
this.active = true;
}
_onkeyup(e: KeyboardEvent) {
if (isSpace(e)) {
this.toggle();
}
this.active = false;
}
toggle() {
if (this.canToggle()) {
const lastState = {
checked: this.checked,
indeterminate: this.indeterminate,
};
if (this.indeterminate) {
this.indeterminate = false;
this.checked = true;
} else {
this.checked = !this.checked;
}
const changePrevented = !this.fireDecoratorEvent("change");
// Angular two way data binding
const valueChangePrevented = !this.fireDecoratorEvent("value-changed");
if (changePrevented || valueChangePrevented) {
this.checked = lastState.checked;
this.indeterminate = lastState.indeterminate;
}
}
return this;
}
canToggle() {
return !(this.disabled || this.readonly || this.displayOnly);
}
valueStateTextMappings() {
return {
"Negative": CheckBox.i18nBundle.getText(VALUE_STATE_ERROR),
"Critical": CheckBox.i18nBundle.getText(VALUE_STATE_WARNING),
"Positive": CheckBox.i18nBundle.getText(VALUE_STATE_SUCCESS),
};
}
get ariaLabelText() {
return getEffectiveAriaLabelText(this) || getAssociatedLabelForTexts(this);
}
get classes() {
return {
main: {
"ui5-checkbox--hoverable": !this.disabled && !this.readonly && isDesktop(),
},
};
}
get ariaReadonly() {
return this.readonly || this.displayOnly ? "true" : undefined;
}
get effectiveAriaDisabled() {
return this.disabled ? "true" : undefined;
}
get effectiveAriaChecked() {
return this.indeterminate && this.checked ? "mixed" : this.checked;
}
get ariaLabelledBy() {
if (!this.ariaLabelText) {
return this.text ? `${this._id}-label` : undefined;
}
return undefined;
}
get ariaDescribedBy() {
return this.hasValueState ? `${this._id}-descr` : undefined;
}
get hasValueState() {
return this.valueState !== ValueState.None;
}
get valueStateText() {
if (this.valueState !== ValueState.None && this.valueState !== ValueState.Information) {
return this.valueStateTextMappings()[this.valueState];
}
}
get effectiveTabIndex() {
const tabindex = this.getAttribute("tabindex");
if (this.tabbable) {
return tabindex ? parseInt(tabindex) : 0;
}
}
get tabbable() {
return !this.disabled && !this.displayOnly;
}
get isCompletelyChecked() {
return this.checked && !this.indeterminate;
}
get isDisplayOnly() {
return this.displayOnly && !this.disabled;
}
get accessibilityInfo() {
const checkboxState = this.checked ? CheckBox.i18nBundle.getText(CHECKBOX_CHECKED) : CheckBox.i18nBundle.getText(CHECKBOX_NOT_CHECKED);
const description = [this.text || "", checkboxState].filter(Boolean).join(" ");
return {
role: this.accInfo.role,
type: CheckBox.i18nBundle.getText(CHECKBOX_ARIA_TYPE),
description,
label: this.ariaLabelText,
disabled: !!this.accInfo.ariaDisabled,
readonly: !!this.accInfo.ariaReadonly,
required: this.accInfo.ariaRequired,
};
}
get accInfo() {
return {
"role": this._accInfo ? this._accInfo.role : "checkbox" as AriaRole,
"ariaChecked": this._accInfo ? this._accInfo.ariaChecked : this.effectiveAriaChecked as AriaChecked,
"ariaReadonly": this._accInfo ? this._accInfo.ariaReadonly : this.ariaReadonly as AriaReadonly,
"ariaDisabled": this._accInfo ? this._accInfo.ariaDisabled : this.effectiveAriaDisabled as AriaDisabled,
"ariaRequired": this._accInfo ? this._accInfo.ariaRequired : this.required,
"tabindex": this._accInfo ? this._accInfo.tabindex : this.effectiveTabIndex,
};
}
}
CheckBox.define();
export default CheckBox;