-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathcustomPreChatFormField.js
More file actions
68 lines (59 loc) · 1.98 KB
/
customPreChatFormField.js
File metadata and controls
68 lines (59 loc) · 1.98 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
import { track, api, LightningElement } from "lwc";
export default class CustomPreChatFormField extends LightningElement {
choiceListDefaultValue;
/**
* Form field data.
* @type {Object}
*/
@api fieldInfo = {};
@api
get name() {
return this.fieldInfo.name;
}
@api
get value() {
const lightningCmp = this.isTypeChoiceList ? this.template.querySelector("lightning-combobox") : this.template.querySelector("lightning-input");
return this.isTypeCheckbox ? lightningCmp.checked : lightningCmp.value;
}
@api
reportValidity() {
const lightningCmp = this.isTypeChoiceList ? this.template.querySelector("lightning-combobox") : this.template.querySelector("lightning-input");
return lightningCmp.reportValidity();
}
get type() {
switch (this.fieldInfo.type) {
case "Phone":
return "tel";
case "Text":
case "Email":
case "Number":
case "Checkbox":
case "ChoiceList":
return this.fieldInfo.type.toLowerCase();
default:
return "text";
}
}
get isTypeCheckbox() {
return this.type === "Checkbox".toLowerCase();
}
get isTypeChoiceList() {
return this.type === "ChoiceList".toLowerCase();
}
/**
* Formats choiceList options and sets the default value.
* @type {Array}
*/
get choiceListOptions() {
let choiceListOptions = [];
const choiceListValues = [...this.fieldInfo.choiceListValues];
choiceListValues.sort((valueA, valueB) => valueA.order - valueB.order);
for (const listValue of choiceListValues) {
if (listValue.isDefaultValue) {
this.choiceListDefaultValue = listValue.choiceListValueName;
}
choiceListOptions.push({ label: listValue.label, value: listValue.choiceListValueName });
}
return choiceListOptions;
}
}