@builder.io/sdk-solid 0.0.1
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/README.md +3 -0
- package/package.json +9 -0
- package/src/blocks/button.js +41 -0
- package/src/blocks/button.lite.tsx +18 -0
- package/src/blocks/columns.js +133 -0
- package/src/blocks/columns.lite.tsx +96 -0
- package/src/blocks/custom-code.js +72 -0
- package/src/blocks/custom-code.lite.tsx +65 -0
- package/src/blocks/embed.js +62 -0
- package/src/blocks/embed.lite.tsx +57 -0
- package/src/blocks/form.js +355 -0
- package/src/blocks/form.lite.tsx +291 -0
- package/src/blocks/fragment.js +15 -0
- package/src/blocks/fragment.lite.tsx +3 -0
- package/src/blocks/image.js +136 -0
- package/src/blocks/image.lite.tsx +81 -0
- package/src/blocks/img.js +39 -0
- package/src/blocks/img.lite.tsx +16 -0
- package/src/blocks/input.js +45 -0
- package/src/blocks/input.lite.tsx +18 -0
- package/src/blocks/raw-text.js +25 -0
- package/src/blocks/raw-text.lite.tsx +8 -0
- package/src/blocks/section.js +24 -0
- package/src/blocks/section.lite.tsx +16 -0
- package/src/blocks/select.js +57 -0
- package/src/blocks/select.lite.tsx +23 -0
- package/src/blocks/submit-button.js +18 -0
- package/src/blocks/submit-button.lite.tsx +7 -0
- package/src/blocks/symbol.js +69 -0
- package/src/blocks/symbol.lite.tsx +37 -0
- package/src/blocks/text.js +15 -0
- package/src/blocks/text.lite.tsx +3 -0
- package/src/blocks/textarea.js +34 -0
- package/src/blocks/textarea.lite.tsx +11 -0
- package/src/blocks/video.js +54 -0
- package/src/blocks/video.lite.tsx +24 -0
- package/src/components/block-styles.js +3 -0
- package/src/components/block-styles.lite.tsx +3 -0
- package/src/components/error-boundary.js +3 -0
- package/src/components/error-boundary.lite.tsx +3 -0
- package/src/components/render-block.js +154 -0
- package/src/components/render-block.lite.tsx +108 -0
- package/src/components/render-blocks.js +104 -0
- package/src/components/render-blocks.lite.tsx +72 -0
- package/src/components/render-content.js +314 -0
- package/src/components/render-content.lite.tsx +289 -0
- package/src/constants/device-sizes.js +39 -0
- package/src/context/builder.context.js +10 -0
- package/src/functions/evaluate.js +28 -0
- package/src/functions/event-handler-name.js +7 -0
- package/src/functions/get-block-actions.js +23 -0
- package/src/functions/get-block-component-options.js +23 -0
- package/src/functions/get-block-properties.js +29 -0
- package/src/functions/get-block-styles.js +42 -0
- package/src/functions/get-block-tag.js +6 -0
- package/src/functions/get-builder-search-params/fn.test.js +13 -0
- package/src/functions/get-builder-search-params/index.js +22 -0
- package/src/functions/get-content/fn.test.js +31 -0
- package/src/functions/get-content/index.js +137 -0
- package/src/functions/get-fetch.js +12 -0
- package/src/functions/get-global-this.js +18 -0
- package/src/functions/get-processed-block.js +46 -0
- package/src/functions/get-processed-block.test.js +31 -0
- package/src/functions/get-target.js +6 -0
- package/src/functions/if-target.js +6 -0
- package/src/functions/is-browser.js +6 -0
- package/src/functions/is-editing.js +7 -0
- package/src/functions/is-iframe.js +7 -0
- package/src/functions/is-previewing.js +14 -0
- package/src/functions/is-react-native.js +6 -0
- package/src/functions/macro-eval.js +5 -0
- package/src/functions/on-change.js +27 -0
- package/src/functions/on-change.test.js +19 -0
- package/src/functions/previewing-model-name.js +11 -0
- package/src/functions/register-component.js +53 -0
- package/src/functions/register.js +29 -0
- package/src/functions/set-editor-settings.js +15 -0
- package/src/functions/set.js +11 -0
- package/src/functions/set.test.js +16 -0
- package/src/functions/track.js +22 -0
- package/src/functions/transform-block.js +6 -0
- package/src/index-helpers/blocks-exports.js +20 -0
- package/src/index-helpers/top-of-file.js +4 -0
- package/src/index.js +13 -0
- package/src/scripts/init-editing.js +70 -0
- package/src/types/builder-block.js +0 -0
- package/src/types/builder-content.js +0 -0
- package/src/types/deep-partial.js +0 -0
- package/src/types/typescript.js +0 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { template as _$template } from "solid-js/web";
|
|
2
|
+
import { setAttribute as _$setAttribute } from "solid-js/web";
|
|
3
|
+
import { effect as _$effect } from "solid-js/web";
|
|
4
|
+
import { insert as _$insert } from "solid-js/web";
|
|
5
|
+
import { createComponent as _$createComponent } from "solid-js/web";
|
|
6
|
+
import { memo as _$memo } from "solid-js/web";
|
|
7
|
+
import { spread as _$spread } from "solid-js/web";
|
|
8
|
+
|
|
9
|
+
const _tmpl$ = /*#__PURE__*/_$template(`<pre></pre>`, 2),
|
|
10
|
+
_tmpl$2 = /*#__PURE__*/_$template(`<form></form>`, 2);
|
|
11
|
+
|
|
12
|
+
import { Show, For } from "solid-js";
|
|
13
|
+
import { createMutable } from "solid-js/store";
|
|
14
|
+
import { css } from "solid-styled-components";
|
|
15
|
+
import RenderBlock from "../components/render-block";
|
|
16
|
+
export default function FormComponent(props) {
|
|
17
|
+
const state = createMutable({
|
|
18
|
+
state: "unsubmitted",
|
|
19
|
+
responseData: null,
|
|
20
|
+
formErrorMessage: "",
|
|
21
|
+
|
|
22
|
+
get submissionState() {
|
|
23
|
+
return Builder.isEditing && props.previewState || state.state;
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
onSubmit(event) {
|
|
27
|
+
const sendWithJs = props.sendWithJs || props.sendSubmissionsTo === "email";
|
|
28
|
+
|
|
29
|
+
if (props.sendSubmissionsTo === "zapier") {
|
|
30
|
+
event.preventDefault();
|
|
31
|
+
} else if (sendWithJs) {
|
|
32
|
+
if (!(props.action || props.sendSubmissionsTo === "email")) {
|
|
33
|
+
event.preventDefault();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
event.preventDefault();
|
|
38
|
+
const el = event.currentTarget;
|
|
39
|
+
const headers = props.customHeaders || {};
|
|
40
|
+
let body;
|
|
41
|
+
const formData = new FormData(el); // TODO: maybe support null
|
|
42
|
+
|
|
43
|
+
const formPairs = Array.from(event.currentTarget.querySelectorAll("input,select,textarea")).filter(el => !!el.name).map(el => {
|
|
44
|
+
let value;
|
|
45
|
+
const key = el.name;
|
|
46
|
+
|
|
47
|
+
if (el instanceof HTMLInputElement) {
|
|
48
|
+
if (el.type === "radio") {
|
|
49
|
+
if (el.checked) {
|
|
50
|
+
value = el.name;
|
|
51
|
+
return {
|
|
52
|
+
key,
|
|
53
|
+
value
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
} else if (el.type === "checkbox") {
|
|
57
|
+
value = el.checked;
|
|
58
|
+
} else if (el.type === "number" || el.type === "range") {
|
|
59
|
+
const num = el.valueAsNumber;
|
|
60
|
+
|
|
61
|
+
if (!isNaN(num)) {
|
|
62
|
+
value = num;
|
|
63
|
+
}
|
|
64
|
+
} else if (el.type === "file") {
|
|
65
|
+
// TODO: one vs multiple files
|
|
66
|
+
value = el.files;
|
|
67
|
+
} else {
|
|
68
|
+
value = el.value;
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
value = el.value;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
key,
|
|
76
|
+
value
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
let contentType = props.contentType;
|
|
80
|
+
|
|
81
|
+
if (props.sendSubmissionsTo === "email") {
|
|
82
|
+
contentType = "multipart/form-data";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
Array.from(formPairs).forEach(({
|
|
86
|
+
value
|
|
87
|
+
}) => {
|
|
88
|
+
if (value instanceof File || Array.isArray(value) && value[0] instanceof File || value instanceof FileList) {
|
|
89
|
+
contentType = "multipart/form-data";
|
|
90
|
+
}
|
|
91
|
+
}); // TODO: send as urlEncoded or multipart by default
|
|
92
|
+
// because of ease of use and reliability in browser API
|
|
93
|
+
// for encoding the form?
|
|
94
|
+
|
|
95
|
+
if (contentType !== "application/json") {
|
|
96
|
+
body = formData;
|
|
97
|
+
} else {
|
|
98
|
+
// Json
|
|
99
|
+
const json = {};
|
|
100
|
+
Array.from(formPairs).forEach(({
|
|
101
|
+
value,
|
|
102
|
+
key
|
|
103
|
+
}) => {
|
|
104
|
+
set(json, key, value);
|
|
105
|
+
});
|
|
106
|
+
body = JSON.stringify(json);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (contentType && contentType !== "multipart/form-data") {
|
|
110
|
+
if (
|
|
111
|
+
/* Zapier doesn't allow content-type header to be sent from browsers */
|
|
112
|
+
!(sendWithJs && props.action?.includes("zapier.com"))) {
|
|
113
|
+
headers["content-type"] = contentType;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const presubmitEvent = new CustomEvent("presubmit", {
|
|
118
|
+
detail: {
|
|
119
|
+
body
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
if (formRef) {
|
|
124
|
+
formRef.dispatchEvent(presubmitEvent);
|
|
125
|
+
|
|
126
|
+
if (presubmitEvent.defaultPrevented) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
state.state = "sending";
|
|
132
|
+
const formUrl = `${builder.env === "dev" ? "http://localhost:5000" : "https://builder.io"}/api/v1/form-submit?apiKey=${builder.apiKey}&to=${btoa(props.sendSubmissionsToEmail || "")}&name=${encodeURIComponent(props.name || "")}`;
|
|
133
|
+
fetch(props.sendSubmissionsTo === "email" ? formUrl : props.action,
|
|
134
|
+
/* TODO: throw error if no action URL */
|
|
135
|
+
{
|
|
136
|
+
body,
|
|
137
|
+
headers,
|
|
138
|
+
method: props.method || "post"
|
|
139
|
+
}).then(async res => {
|
|
140
|
+
let body;
|
|
141
|
+
const contentType = res.headers.get("content-type");
|
|
142
|
+
|
|
143
|
+
if (contentType && contentType.indexOf("application/json") !== -1) {
|
|
144
|
+
body = await res.json();
|
|
145
|
+
} else {
|
|
146
|
+
body = await res.text();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!res.ok && props.errorMessagePath) {
|
|
150
|
+
/* TODO: allow supplying an error formatter function */
|
|
151
|
+
let message = get(body, props.errorMessagePath);
|
|
152
|
+
|
|
153
|
+
if (message) {
|
|
154
|
+
if (typeof message !== "string") {
|
|
155
|
+
/* TODO: ideally convert json to yaml so it woul dbe like
|
|
156
|
+
error: - email has been taken */
|
|
157
|
+
message = JSON.stringify(message);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
state.formErrorMessage = message;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
state.responseData = body;
|
|
165
|
+
state.state = res.ok ? "success" : "error";
|
|
166
|
+
|
|
167
|
+
if (res.ok) {
|
|
168
|
+
const submitSuccessEvent = new CustomEvent("submit:success", {
|
|
169
|
+
detail: {
|
|
170
|
+
res,
|
|
171
|
+
body
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
if (formRef) {
|
|
176
|
+
formRef.dispatchEvent(submitSuccessEvent);
|
|
177
|
+
|
|
178
|
+
if (submitSuccessEvent.defaultPrevented) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
/* TODO: option to turn this on/off? */
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
if (props.resetFormOnSubmit !== false) {
|
|
185
|
+
formRef.reset();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/* TODO: client side route event first that can be preventDefaulted */
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
if (props.successUrl) {
|
|
192
|
+
if (formRef) {
|
|
193
|
+
const event = new CustomEvent("route", {
|
|
194
|
+
detail: {
|
|
195
|
+
url: props.successUrl
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
formRef.dispatchEvent(event);
|
|
199
|
+
|
|
200
|
+
if (!event.defaultPrevented) {
|
|
201
|
+
location.href = props.successUrl;
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
location.href = props.successUrl;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}, err => {
|
|
209
|
+
const submitErrorEvent = new CustomEvent("submit:error", {
|
|
210
|
+
detail: {
|
|
211
|
+
error: err
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
if (formRef) {
|
|
216
|
+
formRef.dispatchEvent(submitErrorEvent);
|
|
217
|
+
|
|
218
|
+
if (submitErrorEvent.defaultPrevented) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
state.responseData = err;
|
|
224
|
+
state.state = "error";
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
});
|
|
230
|
+
const formRef = useRef();
|
|
231
|
+
return (() => {
|
|
232
|
+
const _el$ = _tmpl$2.cloneNode(true);
|
|
233
|
+
|
|
234
|
+
_el$.addEventListener("submit", event => state.state.onSubmit(event));
|
|
235
|
+
|
|
236
|
+
formRef(_el$);
|
|
237
|
+
|
|
238
|
+
_$spread(_el$, () => props.attributes, false, true);
|
|
239
|
+
|
|
240
|
+
_$insert(_el$, _$createComponent(Show, {
|
|
241
|
+
get when() {
|
|
242
|
+
return props.builderBlock && props.builderBlock.children;
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
get children() {
|
|
246
|
+
return _$createComponent(For, {
|
|
247
|
+
get each() {
|
|
248
|
+
return props.builderBlock?.children;
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
children: (block, index) => _$createComponent(RenderBlock, {
|
|
252
|
+
block: block
|
|
253
|
+
})
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
}), null);
|
|
258
|
+
|
|
259
|
+
_$insert(_el$, _$createComponent(Show, {
|
|
260
|
+
get when() {
|
|
261
|
+
return state.state.submissionState === "error";
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
get children() {
|
|
265
|
+
return _$createComponent(BuilderBlocks, {
|
|
266
|
+
dataPath: "errorMessage",
|
|
267
|
+
|
|
268
|
+
get blocks() {
|
|
269
|
+
return props.errorMessage;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
}), null);
|
|
276
|
+
|
|
277
|
+
_$insert(_el$, _$createComponent(Show, {
|
|
278
|
+
get when() {
|
|
279
|
+
return state.state.submissionState === "sending";
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
get children() {
|
|
283
|
+
return _$createComponent(BuilderBlocks, {
|
|
284
|
+
dataPath: "sendingMessage",
|
|
285
|
+
|
|
286
|
+
get blocks() {
|
|
287
|
+
return props.sendingMessage;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
}), null);
|
|
294
|
+
|
|
295
|
+
_$insert(_el$, _$createComponent(Show, {
|
|
296
|
+
get when() {
|
|
297
|
+
return state.state.submissionState === "error" && state.state.responseData;
|
|
298
|
+
},
|
|
299
|
+
|
|
300
|
+
get children() {
|
|
301
|
+
const _el$2 = _tmpl$.cloneNode(true);
|
|
302
|
+
|
|
303
|
+
_$insert(_el$2, () => JSON.stringify(state.state.responseData, null, 2));
|
|
304
|
+
|
|
305
|
+
_$effect(() => _el$2.className = css({
|
|
306
|
+
padding: "10px",
|
|
307
|
+
color: "red",
|
|
308
|
+
textAlign: "center"
|
|
309
|
+
}));
|
|
310
|
+
|
|
311
|
+
return _el$2;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
}), null);
|
|
315
|
+
|
|
316
|
+
_$insert(_el$, _$createComponent(Show, {
|
|
317
|
+
get when() {
|
|
318
|
+
return state.state.submissionState === "success";
|
|
319
|
+
},
|
|
320
|
+
|
|
321
|
+
get children() {
|
|
322
|
+
return _$createComponent(BuilderBlocks, {
|
|
323
|
+
dataPath: "successMessage",
|
|
324
|
+
|
|
325
|
+
get blocks() {
|
|
326
|
+
return props.successMessage;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
}), null);
|
|
333
|
+
|
|
334
|
+
_$effect(_p$ => {
|
|
335
|
+
const _v$ = props.validate,
|
|
336
|
+
_v$2 = !props.sendWithJs && props.action,
|
|
337
|
+
_v$3 = props.method,
|
|
338
|
+
_v$4 = props.name;
|
|
339
|
+
|
|
340
|
+
_v$ !== _p$._v$ && _$setAttribute(_el$, "validate", _p$._v$ = _v$);
|
|
341
|
+
_v$2 !== _p$._v$2 && _$setAttribute(_el$, "action", _p$._v$2 = _v$2);
|
|
342
|
+
_v$3 !== _p$._v$3 && _$setAttribute(_el$, "method", _p$._v$3 = _v$3);
|
|
343
|
+
_v$4 !== _p$._v$4 && _$setAttribute(_el$, "name", _p$._v$4 = _v$4);
|
|
344
|
+
return _p$;
|
|
345
|
+
}, {
|
|
346
|
+
_v$: undefined,
|
|
347
|
+
_v$2: undefined,
|
|
348
|
+
_v$3: undefined,
|
|
349
|
+
_v$4: undefined
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
return _el$;
|
|
353
|
+
})();
|
|
354
|
+
}import { registerComponent } from '../functions/register-component';
|
|
355
|
+
registerComponent(FormComponent, {name:'Form:Form',builtIn:true,defaults:{responsiveStyles:{large:{marginTop:'15px',paddingBottom:'15px'}}},image:'https://cdn.builder.io/api/v1/image/assets%2FIsxPKMo2gPRRKeakUztj1D6uqed2%2Fef36d2a846134910b64b88e6d18c5ca5',inputs:[{name:'sendSubmissionsTo',type:'string',enum:[{label:'Send to email',value:'email',helperText:'Send form submissions to the email address of your choosing'},{label:'Custom',value:'custom',helperText:'Handle where the form requests go manually with a little code, e.g. to your own custom backend'}],defaultValue:'email'},{name:'sendSubmissionsToEmail',type:'string',required:true,defaultValue:'your@email.com',showIf:'options.get("sendSubmissionsTo") === "email"'},{name:'sendWithJs',type:'boolean',helperText:'Set to false to use basic html form action',defaultValue:true,showIf:'options.get("sendSubmissionsTo") === "custom"'},{name:'name',type:'string',defaultValue:'My form'},{name:'action',type:'string',helperText:'URL to send the form data to',showIf:'options.get("sendSubmissionsTo") === "custom"'},{name:'contentType',type:'string',defaultValue:'application/json',advanced:true,enum:['application/json','multipart/form-data','application/x-www-form-urlencoded'],showIf:'options.get("sendSubmissionsTo") === "custom" && options.get("sendWithJs") === true'},{name:'method',type:'string',showIf:'options.get("sendSubmissionsTo") === "custom"',defaultValue:'POST',advanced:true},{name:'previewState',type:'string',enum:['unsubmitted','sending','success','error'],defaultValue:'unsubmitted',helperText:'Choose a state to edit, e.g. choose "success" to show what users see on success and edit the message',showIf:'options.get("sendSubmissionsTo") !== "zapier" && options.get("sendWithJs") === true'},{name:'successUrl',type:'url',helperText:'Optional URL to redirect the user to on form submission success',showIf:'options.get("sendSubmissionsTo") !== "zapier" && options.get("sendWithJs") === true'},{name:'resetFormOnSubmit',type:'boolean',showIf:"options.get('sendSubmissionsTo') === 'custom' && options.get('sendWithJs') === true",advanced:true},{name:'successMessage',type:'uiBlocks',hideFromUI:true,defaultValue:[{'@type':'@builder.io/sdk:Element',responsiveStyles:{large:{marginTop:'10px'}},component:{name:'Text',options:{text:'<span>Thanks!</span>'}}}]},{name:'validate',type:'boolean',defaultValue:true,advanced:true},{name:'errorMessagePath',type:'text',advanced:true,helperText:'Path to where to get the error message from in a JSON response to display to the user, e.g. "error.message" for a response like { "error": { "message": "this username is taken" }}'},{name:'errorMessage',type:'uiBlocks',hideFromUI:true,defaultValue:[{'@type':'@builder.io/sdk:Element',responsiveStyles:{large:{marginTop:'10px'}},bindings:{'component.options.text':'state.formErrorMessage || block.component.options.text'},component:{name:'Text',options:{text:'<span>Form submission error :( Please check your answers and try again</span>'}}}]},{name:'sendingMessage',type:'uiBlocks',hideFromUI:true,defaultValue:[{'@type':'@builder.io/sdk:Element',responsiveStyles:{large:{marginTop:'10px'}},component:{name:'Text',options:{text:'<span>Sending...</span>'}}}]},{name:'customHeaders',type:'map',valueType:{type:'string'},advanced:true,showIf:'options.get("sendSubmissionsTo") === "custom" && options.get("sendWithJs") === true'}],noWrap:true,canHaveChildren:true,defaultChildren:[{'@type':'@builder.io/sdk:Element',responsiveStyles:{large:{marginTop:'10px'}},component:{name:'Text',options:{text:'<span>Enter your name</span>'}}},{'@type':'@builder.io/sdk:Element',responsiveStyles:{large:{marginTop:'10px'}},component:{name:'Form:Input',options:{name:'name',placeholder:'Jane Doe'}}},{'@type':'@builder.io/sdk:Element',responsiveStyles:{large:{marginTop:'10px'}},component:{name:'Text',options:{text:'<span>Enter your email</span>'}}},{'@type':'@builder.io/sdk:Element',responsiveStyles:{large:{marginTop:'10px'}},component:{name:'Form:Input',options:{name:'email',placeholder:'jane@doe.com'}}},{'@type':'@builder.io/sdk:Element',responsiveStyles:{large:{marginTop:'10px'}},component:{name:'Form:SubmitButton',options:{text:'Submit'}}}]});
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { Show, For } from "solid-js";
|
|
2
|
+
|
|
3
|
+
import { createMutable } from "solid-js/store";
|
|
4
|
+
import { css } from "solid-styled-components";
|
|
5
|
+
|
|
6
|
+
import RenderBlock from "../components/render-block.lite";
|
|
7
|
+
|
|
8
|
+
export default function FormComponent(props) {
|
|
9
|
+
const state = createMutable({
|
|
10
|
+
state: "unsubmitted",
|
|
11
|
+
responseData: null,
|
|
12
|
+
formErrorMessage: "",
|
|
13
|
+
get submissionState() {
|
|
14
|
+
return (Builder.isEditing && props.previewState) || state.state;
|
|
15
|
+
},
|
|
16
|
+
onSubmit(
|
|
17
|
+
event: Event & {
|
|
18
|
+
currentTarget: HTMLFormElement;
|
|
19
|
+
}
|
|
20
|
+
) {
|
|
21
|
+
const sendWithJs =
|
|
22
|
+
props.sendWithJs || props.sendSubmissionsTo === "email";
|
|
23
|
+
|
|
24
|
+
if (props.sendSubmissionsTo === "zapier") {
|
|
25
|
+
event.preventDefault();
|
|
26
|
+
} else if (sendWithJs) {
|
|
27
|
+
if (!(props.action || props.sendSubmissionsTo === "email")) {
|
|
28
|
+
event.preventDefault();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
event.preventDefault();
|
|
33
|
+
const el = event.currentTarget;
|
|
34
|
+
const headers = props.customHeaders || {};
|
|
35
|
+
let body: any;
|
|
36
|
+
const formData = new FormData(el); // TODO: maybe support null
|
|
37
|
+
|
|
38
|
+
const formPairs: {
|
|
39
|
+
key: string;
|
|
40
|
+
value: File | boolean | number | string | FileList;
|
|
41
|
+
}[] = Array.from(
|
|
42
|
+
event.currentTarget.querySelectorAll("input,select,textarea")
|
|
43
|
+
)
|
|
44
|
+
.filter((el) => !!(el as HTMLInputElement).name)
|
|
45
|
+
.map((el) => {
|
|
46
|
+
let value: any;
|
|
47
|
+
const key = (el as HTMLImageElement).name;
|
|
48
|
+
|
|
49
|
+
if (el instanceof HTMLInputElement) {
|
|
50
|
+
if (el.type === "radio") {
|
|
51
|
+
if (el.checked) {
|
|
52
|
+
value = el.name;
|
|
53
|
+
return {
|
|
54
|
+
key,
|
|
55
|
+
value,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
} else if (el.type === "checkbox") {
|
|
59
|
+
value = el.checked;
|
|
60
|
+
} else if (el.type === "number" || el.type === "range") {
|
|
61
|
+
const num = el.valueAsNumber;
|
|
62
|
+
|
|
63
|
+
if (!isNaN(num)) {
|
|
64
|
+
value = num;
|
|
65
|
+
}
|
|
66
|
+
} else if (el.type === "file") {
|
|
67
|
+
// TODO: one vs multiple files
|
|
68
|
+
value = el.files;
|
|
69
|
+
} else {
|
|
70
|
+
value = el.value;
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
value = (el as HTMLInputElement).value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
key,
|
|
78
|
+
value,
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
let contentType = props.contentType;
|
|
82
|
+
|
|
83
|
+
if (props.sendSubmissionsTo === "email") {
|
|
84
|
+
contentType = "multipart/form-data";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
Array.from(formPairs).forEach(({ value }) => {
|
|
88
|
+
if (
|
|
89
|
+
value instanceof File ||
|
|
90
|
+
(Array.isArray(value) && value[0] instanceof File) ||
|
|
91
|
+
value instanceof FileList
|
|
92
|
+
) {
|
|
93
|
+
contentType = "multipart/form-data";
|
|
94
|
+
}
|
|
95
|
+
}); // TODO: send as urlEncoded or multipart by default
|
|
96
|
+
// because of ease of use and reliability in browser API
|
|
97
|
+
// for encoding the form?
|
|
98
|
+
|
|
99
|
+
if (contentType !== "application/json") {
|
|
100
|
+
body = formData;
|
|
101
|
+
} else {
|
|
102
|
+
// Json
|
|
103
|
+
const json = {};
|
|
104
|
+
Array.from(formPairs).forEach(({ value, key }) => {
|
|
105
|
+
set(json, key, value);
|
|
106
|
+
});
|
|
107
|
+
body = JSON.stringify(json);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (contentType && contentType !== "multipart/form-data") {
|
|
111
|
+
if (
|
|
112
|
+
/* Zapier doesn't allow content-type header to be sent from browsers */
|
|
113
|
+
!(sendWithJs && props.action?.includes("zapier.com"))
|
|
114
|
+
) {
|
|
115
|
+
headers["content-type"] = contentType;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const presubmitEvent = new CustomEvent("presubmit", {
|
|
120
|
+
detail: {
|
|
121
|
+
body,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
if (formRef) {
|
|
126
|
+
formRef.dispatchEvent(presubmitEvent);
|
|
127
|
+
|
|
128
|
+
if (presubmitEvent.defaultPrevented) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
state.state = "sending";
|
|
134
|
+
const formUrl = `${
|
|
135
|
+
builder.env === "dev" ? "http://localhost:5000" : "https://builder.io"
|
|
136
|
+
}/api/v1/form-submit?apiKey=${builder.apiKey}&to=${btoa(
|
|
137
|
+
props.sendSubmissionsToEmail || ""
|
|
138
|
+
)}&name=${encodeURIComponent(props.name || "")}`;
|
|
139
|
+
fetch(
|
|
140
|
+
props.sendSubmissionsTo === "email" ? formUrl : props.action!,
|
|
141
|
+
/* TODO: throw error if no action URL */
|
|
142
|
+
{
|
|
143
|
+
body,
|
|
144
|
+
headers,
|
|
145
|
+
method: props.method || "post",
|
|
146
|
+
}
|
|
147
|
+
).then(
|
|
148
|
+
async (res) => {
|
|
149
|
+
let body;
|
|
150
|
+
const contentType = res.headers.get("content-type");
|
|
151
|
+
|
|
152
|
+
if (contentType && contentType.indexOf("application/json") !== -1) {
|
|
153
|
+
body = await res.json();
|
|
154
|
+
} else {
|
|
155
|
+
body = await res.text();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!res.ok && props.errorMessagePath) {
|
|
159
|
+
/* TODO: allow supplying an error formatter function */
|
|
160
|
+
let message = get(body, props.errorMessagePath);
|
|
161
|
+
|
|
162
|
+
if (message) {
|
|
163
|
+
if (typeof message !== "string") {
|
|
164
|
+
/* TODO: ideally convert json to yaml so it woul dbe like
|
|
165
|
+
error: - email has been taken */
|
|
166
|
+
message = JSON.stringify(message);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
state.formErrorMessage = message;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
state.responseData = body;
|
|
174
|
+
state.state = res.ok ? "success" : "error";
|
|
175
|
+
|
|
176
|
+
if (res.ok) {
|
|
177
|
+
const submitSuccessEvent = new CustomEvent("submit:success", {
|
|
178
|
+
detail: {
|
|
179
|
+
res,
|
|
180
|
+
body,
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
if (formRef) {
|
|
185
|
+
formRef.dispatchEvent(submitSuccessEvent);
|
|
186
|
+
|
|
187
|
+
if (submitSuccessEvent.defaultPrevented) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
/* TODO: option to turn this on/off? */
|
|
191
|
+
|
|
192
|
+
if (props.resetFormOnSubmit !== false) {
|
|
193
|
+
formRef.reset();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/* TODO: client side route event first that can be preventDefaulted */
|
|
197
|
+
|
|
198
|
+
if (props.successUrl) {
|
|
199
|
+
if (formRef) {
|
|
200
|
+
const event = new CustomEvent("route", {
|
|
201
|
+
detail: {
|
|
202
|
+
url: props.successUrl,
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
formRef.dispatchEvent(event);
|
|
206
|
+
|
|
207
|
+
if (!event.defaultPrevented) {
|
|
208
|
+
location.href = props.successUrl;
|
|
209
|
+
}
|
|
210
|
+
} else {
|
|
211
|
+
location.href = props.successUrl;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
(err) => {
|
|
217
|
+
const submitErrorEvent = new CustomEvent("submit:error", {
|
|
218
|
+
detail: {
|
|
219
|
+
error: err,
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
if (formRef) {
|
|
224
|
+
formRef.dispatchEvent(submitErrorEvent);
|
|
225
|
+
|
|
226
|
+
if (submitErrorEvent.defaultPrevented) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
state.responseData = err;
|
|
232
|
+
state.state = "error";
|
|
233
|
+
}
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const formRef = useRef();
|
|
240
|
+
|
|
241
|
+
return (
|
|
242
|
+
<form
|
|
243
|
+
{...props.attributes}
|
|
244
|
+
validate={props.validate}
|
|
245
|
+
ref={formRef}
|
|
246
|
+
action={!props.sendWithJs && props.action}
|
|
247
|
+
method={props.method}
|
|
248
|
+
name={props.name}
|
|
249
|
+
onSubmit={(event) => state.state.onSubmit(event)}
|
|
250
|
+
>
|
|
251
|
+
<Show when={props.builderBlock && props.builderBlock.children}>
|
|
252
|
+
<For each={props.builderBlock?.children}>
|
|
253
|
+
{(block, index) => <RenderBlock block={block}></RenderBlock>}
|
|
254
|
+
</For>
|
|
255
|
+
</Show>
|
|
256
|
+
<Show when={state.state.submissionState === "error"}>
|
|
257
|
+
<BuilderBlocks
|
|
258
|
+
dataPath="errorMessage"
|
|
259
|
+
blocks={props.errorMessage}
|
|
260
|
+
></BuilderBlocks>
|
|
261
|
+
</Show>
|
|
262
|
+
<Show when={state.state.submissionState === "sending"}>
|
|
263
|
+
<BuilderBlocks
|
|
264
|
+
dataPath="sendingMessage"
|
|
265
|
+
blocks={props.sendingMessage}
|
|
266
|
+
></BuilderBlocks>
|
|
267
|
+
</Show>
|
|
268
|
+
<Show
|
|
269
|
+
when={
|
|
270
|
+
state.state.submissionState === "error" && state.state.responseData
|
|
271
|
+
}
|
|
272
|
+
>
|
|
273
|
+
<pre
|
|
274
|
+
class={css({
|
|
275
|
+
padding: "10px",
|
|
276
|
+
color: "red",
|
|
277
|
+
textAlign: "center",
|
|
278
|
+
})}
|
|
279
|
+
>
|
|
280
|
+
{JSON.stringify(state.state.responseData, null, 2)}
|
|
281
|
+
</pre>
|
|
282
|
+
</Show>
|
|
283
|
+
<Show when={state.state.submissionState === "success"}>
|
|
284
|
+
<BuilderBlocks
|
|
285
|
+
dataPath="successMessage"
|
|
286
|
+
blocks={props.successMessage}
|
|
287
|
+
></BuilderBlocks>
|
|
288
|
+
</Show>
|
|
289
|
+
</form>
|
|
290
|
+
);
|
|
291
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { template as _$template } from "solid-js/web";
|
|
2
|
+
import { insert as _$insert } from "solid-js/web";
|
|
3
|
+
|
|
4
|
+
const _tmpl$ = /*#__PURE__*/_$template(`<span></span>`, 2);
|
|
5
|
+
|
|
6
|
+
export default function FragmentComponent(props) {
|
|
7
|
+
return (() => {
|
|
8
|
+
const _el$ = _tmpl$.cloneNode(true);
|
|
9
|
+
|
|
10
|
+
_$insert(_el$, () => props.children);
|
|
11
|
+
|
|
12
|
+
return _el$;
|
|
13
|
+
})();
|
|
14
|
+
}import { registerComponent } from '../functions/register-component';
|
|
15
|
+
registerComponent(FragmentComponent, {name:'Fragment',static:true,hidden:true,builtIn:true,canHaveChildren:true,noWrap:true});
|