-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSclCheckbox.ts
167 lines (141 loc) · 4.49 KB
/
SclCheckbox.ts
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
import { css, html, LitElement, TemplateResult } from 'lit';
import { customElement, property, query, state } from 'lit/decorators.js';
import { ScopedElementsMixin } from '@open-wc/scoped-elements/lit-element.js';
import { MdCheckbox } from '@scopedelement/material-web/checkbox/MdCheckbox.js';
import { MdSwitch } from '@scopedelement/material-web/switch/MdSwtich.js';
/** TextField designed to be used for SCL element */
export class SclCheckbox extends ScopedElementsMixin(LitElement) {
static scopedElements = {
'md-switch': MdSwitch,
'md-checkbox': MdCheckbox,
};
/** Whether [[`value`]] may be set to `null` by nullSwitch */
@property({ type: Boolean })
nullable = false;
/** The default value defined by the XSD attribute default */
@property({ type: String })
defaultValue?: 'true' | 'false';
@state()
private checkboxValue: 'true' | 'false' = 'false';
/** SCL attributes `value`, can only be `null` if [[`nullable`]]. */
@property({ attribute: false })
set value(value: 'true' | 'false' | null) {
if (value === null) this.null = true;
else {
this.null = false;
this.checkboxValue = value;
}
}
get value(): 'true' | 'false' | null {
return this.null ? null : this.checkboxValue;
}
@property({ type: Boolean })
disabled: boolean = false;
@property({ type: String })
label: string = '';
@property({ type: String })
supportingText: string = '';
@state()
private get userText(): string {
return `${this.label}${
this.supportingText !== '' ? ` (${this.supportingText})` : ''
}`;
}
@state()
private isNull = false;
private parkedValue: 'true' | 'false' | null = null;
@state()
private get null(): boolean {
return this.nullable && this.isNull;
}
private set null(value: boolean) {
if (!this.nullable || value === this.isNull) return;
this.isNull = value;
if (this.isNull) {
this.parkedValue = this.checkboxValue;
if (this.defaultValue) this.checkboxValue = this.defaultValue;
} else {
this.checkboxValue = this.parkedValue!;
this.parkedValue = null;
}
}
@query('.nullswitch.element') nullSwitch?: MdSwitch;
// TODO (jakob-vogelsang): only make sense with the introduction of fixed value
// eslint-disable-next-line class-methods-use-this
reportValidity(): boolean {
return true;
}
// TODO (jakob-vogelsang): only make sense with the introduction of fixed value
// eslint-disable-next-line class-methods-use-this
checkValidity(): boolean {
return true;
}
private renderNullSwitch(): TemplateResult {
if (this.nullable) {
return html`<md-switch
class="nullswitch element"
?selected=${!this.null}
?disabled=${this.disabled}
@input="${async (evt: Event) => {
/** TODO(jakob-vogelsang): change when
* https://github.com/material-components/material-web/issues/5486
* is fixed */
evt.stopPropagation();
}}"
@change="${async (evt: Event) => {
this.null = !(evt.target as MdSwitch).selected;
await this.updateComplete;
this.dispatchEvent(new Event('input'));
}}"
></md-switch>`;
}
return html``;
}
render(): TemplateResult {
return html`
<div style="display: flex; flex-direction: row;">
<div class="input container">
<label
class="input element"
style="${this.disabled || this.isNull
? `color:rgba(0, 0, 0, 0.38)`
: ``}"
>
<md-checkbox
touch-target="wrapper"
?checked=${this.checkboxValue === 'true'}
?disabled=${this.disabled || this.isNull}
@input="${async (evt: Event) => {
this.checkboxValue =
(evt.target as MdCheckbox).checked === true
? 'true'
: 'false';
await this.updateComplete; // we want the changes of the value to be certain
}}"
></md-checkbox>
${this.userText}
</label>
</div>
<div class="nullswitch container">${this.renderNullSwitch()}</div>
</div>
`;
}
static styles = css`
.nullswitch.element {
margin-left: 12px;
}
.nullswitch.container {
display: flex;
align-items: center;
height: 56px;
}
.input.container {
flex: auto;
}
.input.element {
display: flex;
align-items: center;
height: 100%;
}
`;
}