-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
47 lines (38 loc) · 1.44 KB
/
index.js
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
const WIDGET_NAME = '@apostrophecms/form-radio-field';
const WIDGET_SELECTOR = '[data-apos-form-radio]';
export default () => {
apos.util.widgetPlayers[WIDGET_NAME] = {
selector: WIDGET_SELECTOR,
player (el) {
const formWidget = apos.util.closest(el, '[data-apos-form-form]');
const inputs = el.querySelectorAll('input[type="radio"]');
if (!formWidget || inputs.length === 0) {
// Editing the form in the piece modal, it is not active for submissions
return;
}
const inputName = inputs[0].getAttribute('name');
const conditionalGroups = formWidget.querySelectorAll('[data-apos-form-condition="' + inputName + '"]');
if (conditionalGroups.length > 0) {
const input = el.querySelector('input[type="radio"]:checked');
const check = apos.aposForm.checkConditional;
check(conditionalGroups, input);
Array.prototype.forEach.call(inputs, function (radio) {
radio.addEventListener('change', function (e) {
check(conditionalGroups, e.target);
});
});
}
}
};
apos.aposForm.collectors[WIDGET_NAME] = {
selector: WIDGET_SELECTOR,
collector (el) {
const inputs = el.querySelectorAll('input[type="radio"]');
const checked = el.querySelector('input[type="radio"]:checked');
return {
field: inputs[0].getAttribute('name'),
value: checked ? checked.value : undefined
};
}
};
};