-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFormElement.ts
69 lines (52 loc) · 1.52 KB
/
FormElement.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
import { RapidElement } from './RapidElement';
import { property } from 'lit/decorators.js';
/**
* FormElement is a component that appends a hidden input (outside of
* its own shadow) with its value to be included in forms.
*/
export class FormElement extends RapidElement {
@property({ type: String })
name = '';
@property({ type: String, attribute: 'help_text' })
helpText: string;
@property({ type: Boolean, attribute: 'help_always' })
helpAlways: boolean;
@property({ type: Boolean, attribute: 'widget_only' })
widgetOnly: boolean;
@property({ type: Boolean, attribute: 'hide_label' })
hideLabel: boolean;
@property({ type: String })
label: string;
@property({ type: Array })
errors: string[];
@property({ type: String })
value = null;
@property({ attribute: false })
inputRoot: HTMLElement = this;
@property({ type: Boolean })
disabled = false;
static formAssociated = true;
protected internals: ElementInternals;
constructor() {
super();
this.internals = this.attachInternals();
}
public updated(changedProperties: Map<string, any>) {
super.updated(changedProperties);
if (changedProperties.has('value')) {
this.internals.setFormValue(this.value);
}
}
get form() {
return this.internals.form;
}
public setValue(value: any) {
this.value = this.serializeValue(value);
}
public getDeserializedValue(): any {
return JSON.parse(this.value);
}
public serializeValue(value: any): string {
return JSON.stringify(value);
}
}