@verbb/formie-browser 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/chunks/_survey-presentations-CFsgToRT.js +4 -0
- package/dist/chunks/{api-DztsCJ4r.js → api-C8wyLC-s.js} +184 -150
- package/dist/chunks/{bpoint-2qDxlKD9.js → bpoint-CwSw_p7A.js} +1 -1
- package/dist/chunks/{eway-CImXrB4g.js → eway-B30BMlDp.js} +1 -1
- package/dist/chunks/{go-cardless-Bz7HHtH0.js → go-cardless-BiF3iLel.js} +1 -1
- package/dist/chunks/{mollie-BtnB3KGh.js → mollie-D__SwtcH.js} +1 -1
- package/dist/chunks/{moneris-Bd4vUk8b.js → moneris-KJS1BKx7.js} +1 -1
- package/dist/chunks/{opayo-B-eRIXo-.js → opayo-A_LIkc-v.js} +1 -1
- package/dist/chunks/{paddle-BUFeEU4d.js → paddle-BjbOOb0m.js} +1 -1
- package/dist/chunks/{paypal-8Euw7Hrn.js → paypal-CyjHIV8v.js} +1 -1
- package/dist/chunks/{payway-dV0SzJxy.js → payway-a_cyrr3j.js} +1 -1
- package/dist/chunks/{square-reyJukb7.js → square-Bga6WXHV.js} +1 -1
- package/dist/chunks/{stripe-BC-2PBvY.js → stripe-BlqVocl3.js} +1 -1
- package/dist/chunks/{summary-D3AjxpYN.js → summary-Dks0gXS3.js} +42 -38
- package/dist/chunks/survey-likert-DmJ3klhP.js +20 -0
- package/dist/chunks/survey-rank-DeUenftw.js +127 -0
- package/dist/chunks/survey-rating-BPUYDgBJ.js +88 -0
- package/dist/css/formie-theme.css +1 -0
- package/dist/css/theme/fields/_survey-presentations.css +396 -0
- package/dist/index.js +469 -465
- package/dist/js/contracts/schema.d.ts +5 -0
- package/dist/js/contracts/schema.d.ts.map +1 -1
- package/dist/js/core/create-formie-client.d.ts.map +1 -1
- package/dist/js/core/page-client-event.d.ts +8 -4
- package/dist/js/core/page-client-event.d.ts.map +1 -1
- package/dist/js/core/submit-result-state.d.ts.map +1 -1
- package/dist/js/modules/fields/index.d.ts.map +1 -1
- package/dist/js/modules/fields/summary.d.ts.map +1 -1
- package/dist/js/modules/fields/survey-likert.d.ts +3 -0
- package/dist/js/modules/fields/survey-likert.d.ts.map +1 -0
- package/dist/js/modules/fields/survey-rank.d.ts +3 -0
- package/dist/js/modules/fields/survey-rank.d.ts.map +1 -0
- package/dist/js/modules/fields/survey-rating.d.ts +3 -0
- package/dist/js/modules/fields/survey-rating.d.ts.map +1 -0
- package/dist/js/transport/forms-api.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/css/formie-theme.css +1 -0
- package/src/css/theme/fields/_survey-presentations.css +396 -0
- package/src/js/contracts/schema.ts +6 -0
- package/src/js/core/create-formie-client.ts +14 -1
- package/src/js/core/page-client-event.ts +86 -18
- package/src/js/core/submit-result-state.ts +6 -2
- package/src/js/modules/fields/index.ts +3 -0
- package/src/js/modules/fields/summary.ts +17 -3
- package/src/js/modules/fields/survey-likert.ts +50 -0
- package/src/js/modules/fields/survey-rank.ts +385 -0
- package/src/js/modules/fields/survey-rating.ts +182 -0
- package/src/js/transport/forms-api.ts +1 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import surveyPresentationsCss from '#theme-css/fields/_survey-presentations.css?inline';
|
|
2
|
+
|
|
3
|
+
import type { FormieModuleDefinition } from '#contracts/modules';
|
|
4
|
+
import { dispatchFieldEvent } from '#modules/fields/shared';
|
|
5
|
+
import { ensureModuleStyles } from '#modules/styles';
|
|
6
|
+
import { createDebug } from '#utils/debug';
|
|
7
|
+
|
|
8
|
+
const FIELD_SELECTOR = '[data-formie-survey-rating]';
|
|
9
|
+
const STARS_SELECTOR = '[data-formie-survey-rating-stars]';
|
|
10
|
+
const OPTION_SELECTOR = '[data-formie-rating-option]';
|
|
11
|
+
const INPUT_SELECTOR = 'input[data-formie-rating-input]';
|
|
12
|
+
const PRESSED_WHILE_CHECKED = 'data-formie-rating-pressed-while-checked';
|
|
13
|
+
const MODULE_ID = 'survey-rating';
|
|
14
|
+
const debug = createDebug('fields', 'survey-rating');
|
|
15
|
+
|
|
16
|
+
ensureModuleStyles(MODULE_ID, [surveyPresentationsCss]);
|
|
17
|
+
|
|
18
|
+
function getRatingInputs(stars: HTMLElement): HTMLInputElement[] {
|
|
19
|
+
return Array.from(stars.querySelectorAll(INPUT_SELECTOR)).filter((input): input is HTMLInputElement => {
|
|
20
|
+
return input instanceof HTMLInputElement;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function updateRatingValue(stars: HTMLElement): void {
|
|
25
|
+
const selectedIndex = getRatingInputs(stars).findIndex((input) => {
|
|
26
|
+
return input.checked;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
if (selectedIndex >= 0) {
|
|
30
|
+
stars.setAttribute('data-formie-rating-value', String(selectedIndex + 1));
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
stars.removeAttribute('data-formie-rating-value');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function bindRatingField(field: HTMLElement): () => void {
|
|
38
|
+
const stars = field.querySelector(STARS_SELECTOR);
|
|
39
|
+
|
|
40
|
+
if (!(stars instanceof HTMLElement)) {
|
|
41
|
+
debug.warn('Missing rating stars container; skipping field.');
|
|
42
|
+
return () => {};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const cleanups: Array<() => void> = [];
|
|
46
|
+
|
|
47
|
+
const clearHover = () => {
|
|
48
|
+
stars.removeAttribute('data-formie-rating-hover');
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const setHoverFromOption = (option: HTMLElement) => {
|
|
52
|
+
const options = Array.from(stars.querySelectorAll(OPTION_SELECTOR));
|
|
53
|
+
const index = options.indexOf(option);
|
|
54
|
+
|
|
55
|
+
if (index >= 0) {
|
|
56
|
+
stars.setAttribute('data-formie-rating-hover', String(index + 1));
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
stars.querySelectorAll(OPTION_SELECTOR).forEach((option) => {
|
|
61
|
+
if (!(option instanceof HTMLElement)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const input = option.querySelector(INPUT_SELECTOR);
|
|
66
|
+
|
|
67
|
+
const onPointerDown = () => {
|
|
68
|
+
if (!(input instanceof HTMLInputElement)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (input.checked) {
|
|
73
|
+
input.setAttribute(PRESSED_WHILE_CHECKED, 'true');
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
input.removeAttribute(PRESSED_WHILE_CHECKED);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const onMouseEnter = () => {
|
|
81
|
+
setHoverFromOption(option);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
option.addEventListener('pointerdown', onPointerDown);
|
|
85
|
+
option.addEventListener('mouseenter', onMouseEnter);
|
|
86
|
+
cleanups.push(() => {
|
|
87
|
+
option.removeEventListener('pointerdown', onPointerDown);
|
|
88
|
+
option.removeEventListener('mouseenter', onMouseEnter);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
stars.addEventListener('mouseleave', clearHover);
|
|
93
|
+
cleanups.push(() => {
|
|
94
|
+
stars.removeEventListener('mouseleave', clearHover);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
getRatingInputs(stars).forEach((input) => {
|
|
98
|
+
const onClick = (event: MouseEvent) => {
|
|
99
|
+
if (input.getAttribute(PRESSED_WHILE_CHECKED) !== 'true') {
|
|
100
|
+
updateRatingValue(stars);
|
|
101
|
+
dispatchFieldEvent(field, MODULE_ID, 'change', {
|
|
102
|
+
ratingField: field,
|
|
103
|
+
value: input.checked ? input.value : '',
|
|
104
|
+
});
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
event.preventDefault();
|
|
109
|
+
getRatingInputs(stars).forEach((candidate) => {
|
|
110
|
+
candidate.checked = false;
|
|
111
|
+
candidate.removeAttribute(PRESSED_WHILE_CHECKED);
|
|
112
|
+
});
|
|
113
|
+
updateRatingValue(stars);
|
|
114
|
+
dispatchFieldEvent(field, MODULE_ID, 'change', {
|
|
115
|
+
ratingField: field,
|
|
116
|
+
value: '',
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
input.addEventListener('click', onClick);
|
|
121
|
+
cleanups.push(() => {
|
|
122
|
+
input.removeEventListener('click', onClick);
|
|
123
|
+
input.removeAttribute(PRESSED_WHILE_CHECKED);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
updateRatingValue(stars);
|
|
128
|
+
|
|
129
|
+
return () => {
|
|
130
|
+
cleanups.forEach((cleanup) => {
|
|
131
|
+
cleanup();
|
|
132
|
+
});
|
|
133
|
+
clearHover();
|
|
134
|
+
stars.removeAttribute('data-formie-rating-value');
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export const surveyRatingModule: FormieModuleDefinition = {
|
|
139
|
+
id: MODULE_ID,
|
|
140
|
+
kind: 'field',
|
|
141
|
+
match: (ctx) => {
|
|
142
|
+
return ctx.target instanceof HTMLElement && (
|
|
143
|
+
ctx.target.matches(FIELD_SELECTOR) ||
|
|
144
|
+
!!ctx.target.querySelector(FIELD_SELECTOR)
|
|
145
|
+
);
|
|
146
|
+
},
|
|
147
|
+
setup: async(ctx) => {
|
|
148
|
+
if (!(ctx.target instanceof HTMLElement)) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const fields = ctx.target.matches(FIELD_SELECTOR)
|
|
153
|
+
? [ctx.target]
|
|
154
|
+
: Array.from(ctx.target.querySelectorAll(FIELD_SELECTOR)).filter((field): field is HTMLElement => {
|
|
155
|
+
return field instanceof HTMLElement;
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
debug.log('Module setup.', {
|
|
159
|
+
fieldCount: fields.length,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const destroyBindings = fields.map((field) => {
|
|
163
|
+
return bindRatingField(field);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
await ctx.emit('formie:module:survey-rating:init', {
|
|
167
|
+
count: fields.length,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
destroy: () => {
|
|
172
|
+
destroyBindings.forEach((destroyBinding) => {
|
|
173
|
+
destroyBinding();
|
|
174
|
+
});
|
|
175
|
+
debug.log('Module destroy.', {
|
|
176
|
+
fieldCount: fields.length,
|
|
177
|
+
});
|
|
178
|
+
void ctx.emit('formie:module:survey-rating:destroy', {});
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
};
|
|
@@ -107,6 +107,7 @@ function normalizePayload(payload: Record<string, unknown>, fallbackFormError?:
|
|
|
107
107
|
}
|
|
108
108
|
: null,
|
|
109
109
|
submitData: Array.isArray(payload.submitData) ? payload.submitData : undefined,
|
|
110
|
+
clientEvents: Array.isArray(payload.clientEvents) ? payload.clientEvents as FormSubmitResult['clientEvents'] : undefined,
|
|
110
111
|
meta: payload,
|
|
111
112
|
};
|
|
112
113
|
|