-
Notifications
You must be signed in to change notification settings - Fork 51
/
customwidget.ts
55 lines (53 loc) · 1.95 KB
/
customwidget.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
// Refer to the following help topic for details:
// https://surveyjs.io/Documentation/Survey-Creator?id=Create-Custom-Widget
export function init(Survey) {
const widget = {
name: "textwithbutton",
title: "Text with button",
iconName: "",
widgetIsLoaded: function () {
return true;
},
isFit: function (question) {
return question.getType() === 'textwithbutton';
},
activatedByChanged: function () {
Survey.JsonObject.metaData.addClass("textwithbutton", [], null, "text");
Survey.JsonObject.metaData.addProperties("textwithbutton", [
{ name: "buttonText", default: "Click Me" }
]);
},
isDefaultRender: false,
htmlTemplate: "<div><input /><button></button></div>",
afterRender: function (question, el) {
const text = el.getElementsByTagName("input")[0];
text.inputType = question.inputType;
text.placeholder = question.placeHolder;
const button = el.getElementsByTagName("button")[0];
button.innerText = question.buttonText;
button.onclick = function () {
question.value = "You have clicked me";
}
text.onchange = function () {
question.value = text.value;
}
const onValueChangedCallback = function () {
text.value = question.value ? question.value : "";
}
const onReadOnlyChangedCallback = function() {
if (question.isReadOnly) {
text.setAttribute('disabled', 'disabled');
button.setAttribute('disabled', 'disabled');
} else {
text.removeAttribute("disabled");
button.removeAttribute("disabled");
}
};
question.readOnlyChangedCallback = onReadOnlyChangedCallback;
question.valueChangedCallback = onValueChangedCallback;
onValueChangedCallback();
onReadOnlyChangedCallback();
}
}
Survey.CustomWidgetCollection.Instance.addCustomWidget(widget, "customtype");
}