@builder.io/sdk-react-native 0.0.1-6 → 0.0.1-62
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 +42 -0
- package/README.md +19 -0
- package/package.json +6 -4
- package/src/blocks/button/button.js +31 -0
- package/src/blocks/button/component-info.js +42 -0
- package/src/blocks/columns/columns.js +59 -0
- package/src/blocks/columns/component-info.js +242 -0
- package/src/blocks/custom-code/component-info.js +32 -0
- package/src/blocks/{custom-code.lite.tsx → custom-code/custom-code.js} +16 -33
- package/src/blocks/embed/component-info.js +44 -0
- package/src/blocks/embed/embed.js +43 -0
- package/src/blocks/embed/helpers.js +10 -0
- package/src/blocks/form/component-info.js +263 -0
- package/src/blocks/form/form.js +221 -0
- package/src/blocks/fragment/component-info.js +12 -0
- package/src/blocks/fragment/fragment.js +8 -0
- package/src/blocks/image/component-info.js +151 -0
- package/src/blocks/image/image.helpers.js +49 -0
- package/src/blocks/image/image.js +41 -0
- package/src/blocks/img/component-info.js +21 -0
- package/src/blocks/img/img.js +36 -0
- package/src/blocks/input/component-info.js +75 -0
- package/src/blocks/input/input.js +36 -0
- package/src/blocks/raw-text/component-info.js +17 -0
- package/src/blocks/raw-text/raw-text.js +10 -0
- package/src/blocks/section/component-info.js +50 -0
- package/src/blocks/section/section.js +31 -0
- package/src/blocks/select/component-info.js +60 -0
- package/src/blocks/select/select.js +36 -0
- package/src/blocks/submit-button/component-info.js +29 -0
- package/src/blocks/submit-button/submit-button.js +29 -0
- package/src/blocks/symbol/component-info.js +44 -0
- package/src/blocks/symbol/symbol.js +69 -0
- package/src/blocks/{text.js → text/component-info.js} +4 -10
- package/src/blocks/text/text.js +65 -0
- package/src/blocks/textarea/component-info.js +48 -0
- package/src/blocks/textarea/textarea.js +32 -0
- package/src/blocks/video/component-info.js +107 -0
- package/src/blocks/video/video.js +33 -0
- package/src/components/render-block/block-styles.js +40 -0
- package/src/components/render-block/render-block.helpers.js +24 -0
- package/src/components/render-block/render-block.js +169 -0
- package/src/components/render-block/render-component.js +33 -0
- package/src/components/render-block/render-repeated-block.js +29 -0
- package/src/components/render-blocks.js +23 -12
- package/src/components/render-content/components/render-styles.js +58 -0
- package/src/components/render-content/index.js +5 -0
- package/src/components/render-content/render-content.js +254 -0
- package/src/components/render-inlined-styles.js +18 -0
- package/src/constants/builder-registered-components.js +43 -0
- package/src/constants/device-sizes.js +3 -21
- package/src/constants/target.js +5 -0
- package/src/context/builder.context.js +7 -1
- package/src/functions/camel-to-kebab-case.js +5 -0
- package/src/functions/convert-style-object.js +7 -0
- package/src/functions/evaluate.js +17 -8
- package/src/functions/event-handler-name.js +8 -0
- package/src/functions/get-block-actions.js +14 -13
- package/src/functions/get-block-component-options.js +22 -1
- package/src/functions/get-block-properties.js +2 -3
- package/src/functions/get-block-styles.js +9 -19
- package/src/functions/get-block-tag.js +1 -1
- package/src/functions/get-builder-search-params/fn.test.js +14 -0
- package/src/functions/get-builder-search-params/index.js +23 -0
- package/src/functions/get-content/ab-testing.js +39 -0
- package/src/functions/get-content/fn.test.js +32 -0
- package/src/functions/get-content/index.js +94 -0
- package/src/functions/get-content/types.js +1 -0
- package/src/functions/get-fetch.js +35 -0
- package/src/functions/get-global-this.js +19 -0
- package/src/functions/get-processed-block.js +18 -9
- package/src/functions/get-processed-block.test.js +14 -8
- package/src/functions/if-target.js +11 -2
- package/src/functions/is-browser.js +1 -2
- package/src/functions/is-editing.js +1 -1
- package/src/functions/is-iframe.js +1 -1
- package/src/functions/is-previewing.js +15 -0
- package/src/functions/is-react-native.js +1 -1
- package/src/functions/on-change.test.js +1 -2
- package/src/functions/previewing-model-name.js +12 -0
- package/src/functions/register-component.js +46 -26
- package/src/functions/register.js +30 -0
- package/src/functions/sanitize-styles.js +33 -0
- package/src/functions/set-editor-settings.js +16 -0
- package/src/functions/set.test.js +1 -2
- package/src/functions/track.js +7 -3
- package/src/functions/transform-block.js +38 -0
- package/src/helpers/css.js +13 -0
- package/src/helpers/flatten.js +35 -0
- package/src/index-helpers/blocks-exports.js +19 -0
- package/src/index-helpers/top-of-file.js +2 -0
- package/src/index.js +10 -23
- package/src/scripts/init-editing.js +73 -49
- package/src/types/components.js +1 -0
- package/src/types/deep-partial.js +1 -0
- package/src/types/element.js +1 -0
- package/src/types/targets.js +1 -0
- package/src/types/typescript.js +1 -0
- package/index.js +0 -11
- package/src/blocks/button.js +0 -37
- package/src/blocks/button.lite.tsx +0 -29
- package/src/blocks/columns.js +0 -42
- package/src/blocks/columns.lite.tsx +0 -41
- package/src/blocks/custom-code.js +0 -56
- package/src/blocks/embed.js +0 -56
- package/src/blocks/embed.lite.tsx +0 -65
- package/src/blocks/form.js +0 -229
- package/src/blocks/form.lite.tsx +0 -257
- package/src/blocks/image.js +0 -123
- package/src/blocks/image.lite.tsx +0 -35
- package/src/blocks/img.js +0 -41
- package/src/blocks/img.lite.tsx +0 -19
- package/src/blocks/input.js +0 -41
- package/src/blocks/input.lite.tsx +0 -23
- package/src/blocks/raw-text.js +0 -17
- package/src/blocks/raw-text.lite.tsx +0 -12
- package/src/blocks/section.js +0 -36
- package/src/blocks/section.lite.tsx +0 -20
- package/src/blocks/select.js +0 -40
- package/src/blocks/select.lite.tsx +0 -24
- package/src/blocks/submit-button.js +0 -34
- package/src/blocks/submit-button.lite.tsx +0 -11
- package/src/blocks/symbol.js +0 -20
- package/src/blocks/symbol.lite.tsx +0 -20
- package/src/blocks/text.lite.tsx +0 -12
- package/src/blocks/textarea.js +0 -37
- package/src/blocks/textarea.lite.tsx +0 -15
- package/src/blocks/video.js +0 -49
- package/src/blocks/video.lite.tsx +0 -28
- package/src/components/render-block.js +0 -84
- package/src/components/render-block.lite.tsx +0 -92
- package/src/components/render-blocks.lite.tsx +0 -56
- package/src/components/render-content.js +0 -66
- package/src/components/render-content.lite.tsx +0 -95
- package/src/functions/get-content.js +0 -103
- package/src/functions/get-target.js +0 -7
- package/src/functions/macro-eval.js +0 -6
- package/src/package.json +0 -18
package/src/blocks/form.js
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
import { registerComponent } from '../functions/register-component';
|
|
2
|
-
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defProps = Object.defineProperties;
|
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
-
var __spreadValues = (a, b) => {
|
|
11
|
-
for (var prop in b || (b = {}))
|
|
12
|
-
if (__hasOwnProp.call(b, prop))
|
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
|
14
|
-
if (__getOwnPropSymbols)
|
|
15
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
-
if (__propIsEnum.call(b, prop))
|
|
17
|
-
__defNormalProp(a, prop, b[prop]);
|
|
18
|
-
}
|
|
19
|
-
return a;
|
|
20
|
-
};
|
|
21
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
|
-
var __async = (__this, __arguments, generator) => {
|
|
23
|
-
return new Promise((resolve, reject) => {
|
|
24
|
-
var fulfilled = (value) => {
|
|
25
|
-
try {
|
|
26
|
-
step(generator.next(value));
|
|
27
|
-
} catch (e) {
|
|
28
|
-
reject(e);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
var rejected = (value) => {
|
|
32
|
-
try {
|
|
33
|
-
step(generator.throw(value));
|
|
34
|
-
} catch (e) {
|
|
35
|
-
reject(e);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
39
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
import * as React from "react";
|
|
43
|
-
import { View, StyleSheet } from "react-native";
|
|
44
|
-
import { useState, useRef } from "react";
|
|
45
|
-
import { Builder, builder } from "@builder.io/sdk";
|
|
46
|
-
import { BuilderBlocks } from "@dummy";
|
|
47
|
-
import { set } from "@dummy";
|
|
48
|
-
import { get } from "@dummy";
|
|
49
|
-
function FormComponent(props) {
|
|
50
|
-
var _a;
|
|
51
|
-
const [state, setState] = useState(() => "unsubmitted");
|
|
52
|
-
const [responseData, setResponseData] = useState(() => null);
|
|
53
|
-
const [formErrorMessage, setFormErrorMessage] = useState(() => "");
|
|
54
|
-
function submissionState() {
|
|
55
|
-
return Builder.isEditing && props.previewState || state;
|
|
56
|
-
}
|
|
57
|
-
function onSubmit(event) {
|
|
58
|
-
var _a2;
|
|
59
|
-
const sendWithJs = props.sendWithJs || props.sendSubmissionsTo === "email";
|
|
60
|
-
if (props.sendSubmissionsTo === "zapier") {
|
|
61
|
-
event.preventDefault();
|
|
62
|
-
} else if (sendWithJs) {
|
|
63
|
-
if (!(props.action || props.sendSubmissionsTo === "email")) {
|
|
64
|
-
event.preventDefault();
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
event.preventDefault();
|
|
68
|
-
const el = event.currentTarget;
|
|
69
|
-
const headers = props.customHeaders || {};
|
|
70
|
-
let body;
|
|
71
|
-
const formData = new FormData(el);
|
|
72
|
-
const formPairs = Array.from(event.currentTarget.querySelectorAll("input,select,textarea")).filter((el2) => !!el2.name).map((el2) => {
|
|
73
|
-
let value;
|
|
74
|
-
const key = el2.name;
|
|
75
|
-
if (el2 instanceof HTMLInputElement) {
|
|
76
|
-
if (el2.type === "radio") {
|
|
77
|
-
if (el2.checked) {
|
|
78
|
-
value = el2.name;
|
|
79
|
-
return {
|
|
80
|
-
key,
|
|
81
|
-
value
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
} else if (el2.type === "checkbox") {
|
|
85
|
-
value = el2.checked;
|
|
86
|
-
} else if (el2.type === "number" || el2.type === "range") {
|
|
87
|
-
const num = el2.valueAsNumber;
|
|
88
|
-
if (!isNaN(num)) {
|
|
89
|
-
value = num;
|
|
90
|
-
}
|
|
91
|
-
} else if (el2.type === "file") {
|
|
92
|
-
value = el2.files;
|
|
93
|
-
} else {
|
|
94
|
-
value = el2.value;
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
97
|
-
value = el2.value;
|
|
98
|
-
}
|
|
99
|
-
return {
|
|
100
|
-
key,
|
|
101
|
-
value
|
|
102
|
-
};
|
|
103
|
-
});
|
|
104
|
-
let contentType = props.contentType;
|
|
105
|
-
if (props.sendSubmissionsTo === "email") {
|
|
106
|
-
contentType = "multipart/form-data";
|
|
107
|
-
}
|
|
108
|
-
Array.from(formPairs).forEach(({ value }) => {
|
|
109
|
-
if (value instanceof File || Array.isArray(value) && value[0] instanceof File || value instanceof FileList) {
|
|
110
|
-
contentType = "multipart/form-data";
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
if (contentType !== "application/json") {
|
|
114
|
-
body = formData;
|
|
115
|
-
} else {
|
|
116
|
-
const json = {};
|
|
117
|
-
Array.from(formPairs).forEach(({ value, key }) => {
|
|
118
|
-
set(json, key, value);
|
|
119
|
-
});
|
|
120
|
-
body = JSON.stringify(json);
|
|
121
|
-
}
|
|
122
|
-
if (contentType && contentType !== "multipart/form-data") {
|
|
123
|
-
if (!(sendWithJs && ((_a2 = props.action) == null ? void 0 : _a2.includes("zapier.com")))) {
|
|
124
|
-
headers["content-type"] = contentType;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
const presubmitEvent = new CustomEvent("presubmit", { detail: { body } });
|
|
128
|
-
if (formRef.current) {
|
|
129
|
-
formRef.current.dispatchEvent(presubmitEvent);
|
|
130
|
-
if (presubmitEvent.defaultPrevented) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
setState("sending");
|
|
135
|
-
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 || "")}`;
|
|
136
|
-
fetch(props.sendSubmissionsTo === "email" ? formUrl : props.action, { body, headers, method: props.method || "post" }).then((res) => __async(this, null, function* () {
|
|
137
|
-
let body2;
|
|
138
|
-
const contentType2 = res.headers.get("content-type");
|
|
139
|
-
if (contentType2 && contentType2.indexOf("application/json") !== -1) {
|
|
140
|
-
body2 = yield res.json();
|
|
141
|
-
} else {
|
|
142
|
-
body2 = yield res.text();
|
|
143
|
-
}
|
|
144
|
-
if (!res.ok && props.errorMessagePath) {
|
|
145
|
-
let message = get(body2, props.errorMessagePath);
|
|
146
|
-
if (message) {
|
|
147
|
-
if (typeof message !== "string") {
|
|
148
|
-
message = JSON.stringify(message);
|
|
149
|
-
}
|
|
150
|
-
setFormErrorMessage(message);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
setResponseData(body2);
|
|
154
|
-
setState(res.ok ? "success" : "error");
|
|
155
|
-
if (res.ok) {
|
|
156
|
-
const submitSuccessEvent = new CustomEvent("submit:success", {
|
|
157
|
-
detail: { res, body: body2 }
|
|
158
|
-
});
|
|
159
|
-
if (formRef.current) {
|
|
160
|
-
formRef.current.dispatchEvent(submitSuccessEvent);
|
|
161
|
-
if (submitSuccessEvent.defaultPrevented) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
if (props.resetFormOnSubmit !== false) {
|
|
165
|
-
formRef.current.reset();
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
if (props.successUrl) {
|
|
169
|
-
if (formRef.current) {
|
|
170
|
-
const event2 = new CustomEvent("route", {
|
|
171
|
-
detail: { url: props.successUrl }
|
|
172
|
-
});
|
|
173
|
-
formRef.current.dispatchEvent(event2);
|
|
174
|
-
if (!event2.defaultPrevented) {
|
|
175
|
-
location.href = props.successUrl;
|
|
176
|
-
}
|
|
177
|
-
} else {
|
|
178
|
-
location.href = props.successUrl;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}), (err) => {
|
|
183
|
-
const submitErrorEvent = new CustomEvent("submit:error", {
|
|
184
|
-
detail: { error: err }
|
|
185
|
-
});
|
|
186
|
-
if (formRef.current) {
|
|
187
|
-
formRef.current.dispatchEvent(submitErrorEvent);
|
|
188
|
-
if (submitErrorEvent.defaultPrevented) {
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
setResponseData(err);
|
|
193
|
-
setState("error");
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
const formRef = useRef();
|
|
198
|
-
return /* @__PURE__ */ React.createElement(View, __spreadProps(__spreadValues({}, props.attributes), {
|
|
199
|
-
validate: props.validate,
|
|
200
|
-
ref: formRef,
|
|
201
|
-
action: !props.sendWithJs && props.action,
|
|
202
|
-
method: props.method,
|
|
203
|
-
name: props.name,
|
|
204
|
-
onSubmit: (event) => onSubmit(event)
|
|
205
|
-
}), " ", props.builderBlock && props.builderBlock.children && /* @__PURE__ */ React.createElement(React.Fragment, null, (_a = props.builderBlock) == null ? void 0 : _a.children.map((block) => /* @__PURE__ */ React.createElement(BuilderBlockComponent, {
|
|
206
|
-
block
|
|
207
|
-
}))), " ", submissionState() === "error" && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(BuilderBlocks, {
|
|
208
|
-
dataPath: "errorMessage",
|
|
209
|
-
blocks: props.errorMessage
|
|
210
|
-
})), " ", submissionState() === "sending" && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(BuilderBlocks, {
|
|
211
|
-
dataPath: "sendingMessage",
|
|
212
|
-
blocks: props.sendingMessage
|
|
213
|
-
})), " ", submissionState() === "error" && responseData && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(View, {
|
|
214
|
-
className: "builder-form-error-text",
|
|
215
|
-
style: styles.view1
|
|
216
|
-
}, " ", JSON.stringify(responseData, null, 2), " ")), " ", submissionState() === "success" && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(BuilderBlocks, {
|
|
217
|
-
dataPath: "successMessage",
|
|
218
|
-
blocks: props.successMessage
|
|
219
|
-
})), " ");
|
|
220
|
-
}
|
|
221
|
-
const styles = StyleSheet.create({
|
|
222
|
-
view1: { padding: 10, color: "red", textAlign: "center" }
|
|
223
|
-
});
|
|
224
|
-
export {
|
|
225
|
-
FormComponent as default
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
registerComponent(FormComponent, {name:'Form:Form',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'}}}]});
|
package/src/blocks/form.lite.tsx
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { View, StyleSheet, Image, Text } from "react-native";
|
|
3
|
-
import { useState, useContext, useRef } from "react";
|
|
4
|
-
import { BuilderBlockComponent as BuilderBlock } from "@dummy";
|
|
5
|
-
import { Builder, builder } from "@builder.io/sdk";
|
|
6
|
-
import { BuilderBlocks } from "@dummy";
|
|
7
|
-
import { set } from "@dummy";
|
|
8
|
-
import { get } from "@dummy";
|
|
9
|
-
|
|
10
|
-
export default function FormComponent(props) {
|
|
11
|
-
const [state, setState] = useState(() => "unsubmitted");
|
|
12
|
-
|
|
13
|
-
const [responseData, setResponseData] = useState(() => null);
|
|
14
|
-
|
|
15
|
-
const [formErrorMessage, setFormErrorMessage] = useState(() => "");
|
|
16
|
-
|
|
17
|
-
function submissionState() {
|
|
18
|
-
return (Builder.isEditing && props.previewState) || state;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function onSubmit(event) {
|
|
22
|
-
const sendWithJs = 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;
|
|
36
|
-
const formData = new FormData(el); // TODO: maybe support null
|
|
37
|
-
|
|
38
|
-
const formPairs = Array.from(
|
|
39
|
-
event.currentTarget.querySelectorAll("input,select,textarea")
|
|
40
|
-
)
|
|
41
|
-
.filter((el) => !!el.name)
|
|
42
|
-
.map((el) => {
|
|
43
|
-
let value;
|
|
44
|
-
const key = el.name;
|
|
45
|
-
|
|
46
|
-
if (el instanceof HTMLInputElement) {
|
|
47
|
-
if (el.type === "radio") {
|
|
48
|
-
if (el.checked) {
|
|
49
|
-
value = el.name;
|
|
50
|
-
return {
|
|
51
|
-
key,
|
|
52
|
-
value,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
} else if (el.type === "checkbox") {
|
|
56
|
-
value = el.checked;
|
|
57
|
-
} else if (el.type === "number" || el.type === "range") {
|
|
58
|
-
const num = el.valueAsNumber;
|
|
59
|
-
|
|
60
|
-
if (!isNaN(num)) {
|
|
61
|
-
value = num;
|
|
62
|
-
}
|
|
63
|
-
} else if (el.type === "file") {
|
|
64
|
-
// TODO: one vs multiple files
|
|
65
|
-
value = el.files;
|
|
66
|
-
} else {
|
|
67
|
-
value = el.value;
|
|
68
|
-
}
|
|
69
|
-
} else {
|
|
70
|
-
value = el.value;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
key,
|
|
75
|
-
value,
|
|
76
|
-
};
|
|
77
|
-
});
|
|
78
|
-
let contentType = props.contentType;
|
|
79
|
-
|
|
80
|
-
if (props.sendSubmissionsTo === "email") {
|
|
81
|
-
contentType = "multipart/form-data";
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
Array.from(formPairs).forEach(({ value }) => {
|
|
85
|
-
if (
|
|
86
|
-
value instanceof File ||
|
|
87
|
-
(Array.isArray(value) && value[0] instanceof File) ||
|
|
88
|
-
value instanceof FileList
|
|
89
|
-
) {
|
|
90
|
-
contentType = "multipart/form-data";
|
|
91
|
-
}
|
|
92
|
-
}); // TODO: send as urlEncoded or multipart by default
|
|
93
|
-
// because of ease of use and reliability in browser API
|
|
94
|
-
// for encoding the form?
|
|
95
|
-
|
|
96
|
-
if (contentType !== "application/json") {
|
|
97
|
-
body = formData;
|
|
98
|
-
} else {
|
|
99
|
-
// Json
|
|
100
|
-
const json = {};
|
|
101
|
-
Array.from(formPairs).forEach(({ value, key }) => {
|
|
102
|
-
set(json, key, value);
|
|
103
|
-
});
|
|
104
|
-
body = JSON.stringify(json);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (contentType && contentType !== "multipart/form-data") {
|
|
108
|
-
if (
|
|
109
|
-
/* Zapier doesn't allow content-type header to be sent from browsers */ !(
|
|
110
|
-
sendWithJs && props.action?.includes("zapier.com")
|
|
111
|
-
)
|
|
112
|
-
) {
|
|
113
|
-
headers["content-type"] = contentType;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
const presubmitEvent = new CustomEvent("presubmit", { detail: { body } });
|
|
117
|
-
if (formRef.current) {
|
|
118
|
-
formRef.current.dispatchEvent(presubmitEvent);
|
|
119
|
-
if (presubmitEvent.defaultPrevented) {
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
setState("sending");
|
|
124
|
-
const formUrl = `${
|
|
125
|
-
builder.env === "dev" ? "http://localhost:5000" : "https://builder.io"
|
|
126
|
-
}/api/v1/form-submit?apiKey=${builder.apiKey}&to=${btoa(
|
|
127
|
-
props.sendSubmissionsToEmail || ""
|
|
128
|
-
)}&name=${encodeURIComponent(props.name || "")}`;
|
|
129
|
-
fetch(
|
|
130
|
-
props.sendSubmissionsTo === "email"
|
|
131
|
-
? formUrl
|
|
132
|
-
: props.action /* TODO: throw error if no action URL */,
|
|
133
|
-
{ body, headers, method: props.method || "post" }
|
|
134
|
-
).then(
|
|
135
|
-
async (res) => {
|
|
136
|
-
let body;
|
|
137
|
-
const contentType = res.headers.get("content-type");
|
|
138
|
-
if (contentType && contentType.indexOf("application/json") !== -1) {
|
|
139
|
-
body = await res.json();
|
|
140
|
-
} else {
|
|
141
|
-
body = await res.text();
|
|
142
|
-
}
|
|
143
|
-
if (!res.ok && props.errorMessagePath) {
|
|
144
|
-
/* TODO: allow supplying an error formatter function */ let message =
|
|
145
|
-
get(body, props.errorMessagePath);
|
|
146
|
-
if (message) {
|
|
147
|
-
if (typeof message !== "string") {
|
|
148
|
-
/* TODO: ideally convert json to yaml so it woul dbe like error: - email has been taken */ message =
|
|
149
|
-
JSON.stringify(message);
|
|
150
|
-
}
|
|
151
|
-
setFormErrorMessage(message);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
setResponseData(body);
|
|
155
|
-
setState(res.ok ? "success" : "error");
|
|
156
|
-
if (res.ok) {
|
|
157
|
-
const submitSuccessEvent = new CustomEvent("submit:success", {
|
|
158
|
-
detail: { res, body },
|
|
159
|
-
});
|
|
160
|
-
if (formRef.current) {
|
|
161
|
-
formRef.current.dispatchEvent(submitSuccessEvent);
|
|
162
|
-
if (submitSuccessEvent.defaultPrevented) {
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
/* TODO: option to turn this on/off? */ if (
|
|
166
|
-
props.resetFormOnSubmit !== false
|
|
167
|
-
) {
|
|
168
|
-
formRef.current.reset();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
/* TODO: client side route event first that can be preventDefaulted */ if (
|
|
172
|
-
props.successUrl
|
|
173
|
-
) {
|
|
174
|
-
if (formRef.current) {
|
|
175
|
-
const event = new CustomEvent("route", {
|
|
176
|
-
detail: { url: props.successUrl },
|
|
177
|
-
});
|
|
178
|
-
formRef.current.dispatchEvent(event);
|
|
179
|
-
if (!event.defaultPrevented) {
|
|
180
|
-
location.href = props.successUrl;
|
|
181
|
-
}
|
|
182
|
-
} else {
|
|
183
|
-
location.href = props.successUrl;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
|
-
(err) => {
|
|
189
|
-
const submitErrorEvent = new CustomEvent("submit:error", {
|
|
190
|
-
detail: { error: err },
|
|
191
|
-
});
|
|
192
|
-
if (formRef.current) {
|
|
193
|
-
formRef.current.dispatchEvent(submitErrorEvent);
|
|
194
|
-
if (submitErrorEvent.defaultPrevented) {
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
setResponseData(err);
|
|
199
|
-
setState("error");
|
|
200
|
-
}
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
const formRef = useRef();
|
|
205
|
-
return (
|
|
206
|
-
<View
|
|
207
|
-
{...props.attributes}
|
|
208
|
-
validate={props.validate}
|
|
209
|
-
ref={formRef}
|
|
210
|
-
action={!props.sendWithJs && props.action}
|
|
211
|
-
method={props.method}
|
|
212
|
-
name={props.name}
|
|
213
|
-
onSubmit={(event) => onSubmit(event)}
|
|
214
|
-
>
|
|
215
|
-
{" "}
|
|
216
|
-
{props.builderBlock && props.builderBlock.children && (
|
|
217
|
-
<>
|
|
218
|
-
{props.builderBlock?.children.map((block) => (
|
|
219
|
-
<BuilderBlockComponent block={block} />
|
|
220
|
-
))}
|
|
221
|
-
</>
|
|
222
|
-
)}{" "}
|
|
223
|
-
{submissionState() === "error" && (
|
|
224
|
-
<>
|
|
225
|
-
<BuilderBlocks dataPath="errorMessage" blocks={props.errorMessage} />
|
|
226
|
-
</>
|
|
227
|
-
)}{" "}
|
|
228
|
-
{submissionState() === "sending" && (
|
|
229
|
-
<>
|
|
230
|
-
<BuilderBlocks
|
|
231
|
-
dataPath="sendingMessage"
|
|
232
|
-
blocks={props.sendingMessage}
|
|
233
|
-
/>
|
|
234
|
-
</>
|
|
235
|
-
)}{" "}
|
|
236
|
-
{submissionState() === "error" && responseData && (
|
|
237
|
-
<>
|
|
238
|
-
<View className="builder-form-error-text" style={styles.view1}>
|
|
239
|
-
{" "}
|
|
240
|
-
{JSON.stringify(responseData, null, 2)}{" "}
|
|
241
|
-
</View>
|
|
242
|
-
</>
|
|
243
|
-
)}{" "}
|
|
244
|
-
{submissionState() === "success" && (
|
|
245
|
-
<>
|
|
246
|
-
<BuilderBlocks
|
|
247
|
-
dataPath="successMessage"
|
|
248
|
-
blocks={props.successMessage}
|
|
249
|
-
/>
|
|
250
|
-
</>
|
|
251
|
-
)}{" "}
|
|
252
|
-
</View>
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
const styles = StyleSheet.create({
|
|
256
|
-
view1: { padding: 10, color: "red", textAlign: "center" },
|
|
257
|
-
});
|
package/src/blocks/image.js
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { Image as ReactImage, View } from "react-native";
|
|
3
|
-
import { registerComponent } from "../functions/register-component";
|
|
4
|
-
function Image(props) {
|
|
5
|
-
return /* @__PURE__ */ React.createElement(View, {
|
|
6
|
-
style: { position: "relative" }
|
|
7
|
-
}, /* @__PURE__ */ React.createElement(ReactImage, {
|
|
8
|
-
resizeMode: props.backgroundSize || "contain",
|
|
9
|
-
style: { position: "absolute", top: 0, bottom: 0, left: 0, right: 0 },
|
|
10
|
-
source: { uri: props.image }
|
|
11
|
-
}), /* @__PURE__ */ React.createElement("div", {
|
|
12
|
-
className: "builder-image-sizer",
|
|
13
|
-
style: {
|
|
14
|
-
width: "100%",
|
|
15
|
-
paddingTop: props.aspectRatio * 100 + "%",
|
|
16
|
-
pointerEvents: "none",
|
|
17
|
-
fontSize: 0
|
|
18
|
-
}
|
|
19
|
-
}, " "));
|
|
20
|
-
}
|
|
21
|
-
registerComponent(Image, {
|
|
22
|
-
name: "Image",
|
|
23
|
-
static: true,
|
|
24
|
-
image: "https://firebasestorage.googleapis.com/v0/b/builder-3b0a2.appspot.com/o/images%2Fbaseline-insert_photo-24px.svg?alt=media&token=4e5d0ef4-f5e8-4e57-b3a9-38d63a9b9dc4",
|
|
25
|
-
defaultStyles: {
|
|
26
|
-
position: "relative",
|
|
27
|
-
minHeight: "20px",
|
|
28
|
-
minWidth: "20px",
|
|
29
|
-
overflow: "hidden"
|
|
30
|
-
},
|
|
31
|
-
canHaveChildren: true,
|
|
32
|
-
inputs: [
|
|
33
|
-
{
|
|
34
|
-
name: "image",
|
|
35
|
-
type: "file",
|
|
36
|
-
bubble: true,
|
|
37
|
-
allowedFileTypes: ["jpeg", "jpg", "png", "svg"],
|
|
38
|
-
required: true,
|
|
39
|
-
defaultValue: "https://cdn.builder.io/api/v1/image/assets%2Fpwgjf0RoYWbdnJSbpBAjXNRMe9F2%2Ffb27a7c790324294af8be1c35fe30f4d",
|
|
40
|
-
onChange: " const DEFAULT_ASPECT_RATIO = 0.7041; options.delete('srcset'); options.delete('noWebp'); function loadImage(url, timeout) { return new Promise((resolve, reject) => { const img = document.createElement('img'); let loaded = false; img.onload = () => { loaded = true; resolve(img); }; img.addEventListener('error', event => { console.warn('Image load failed', event.error); reject(event.error); }); img.src = url; setTimeout(() => { if (!loaded) { reject(new Error('Image load timed out')); } }, timeout); }); } function round(num) { return Math.round(num * 1000) / 1000; } const value = options.get('image'); const aspectRatio = options.get('aspectRatio'); // For SVG images - don't render as webp, keep them as SVG fetch(value) .then(res => res.blob()) .then(blob => { if (blob.type.includes('svg')) { options.set('noWebp', true); } }); if (value && (!aspectRatio || aspectRatio === DEFAULT_ASPECT_RATIO)) { return loadImage(value).then(img => { const possiblyUpdatedAspectRatio = options.get('aspectRatio'); if ( options.get('image') === value && (!possiblyUpdatedAspectRatio || possiblyUpdatedAspectRatio === DEFAULT_ASPECT_RATIO) ) { if (img.width && img.height) { options.set('aspectRatio', round(img.height / img.width)); options.set('height', img.height); options.set('width', img.width); } } }); }"
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
name: "backgroundSize",
|
|
44
|
-
type: "text",
|
|
45
|
-
defaultValue: "cover",
|
|
46
|
-
enum: [
|
|
47
|
-
{
|
|
48
|
-
label: "contain",
|
|
49
|
-
value: "contain",
|
|
50
|
-
helperText: "The image should never get cropped"
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
label: "cover",
|
|
54
|
-
value: "cover",
|
|
55
|
-
helperText: `The image should fill it's box, cropping when needed`
|
|
56
|
-
}
|
|
57
|
-
]
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
name: "backgroundPosition",
|
|
61
|
-
type: "text",
|
|
62
|
-
defaultValue: "center",
|
|
63
|
-
enum: [
|
|
64
|
-
"center",
|
|
65
|
-
"top",
|
|
66
|
-
"left",
|
|
67
|
-
"right",
|
|
68
|
-
"bottom",
|
|
69
|
-
"top left",
|
|
70
|
-
"top right",
|
|
71
|
-
"bottom left",
|
|
72
|
-
"bottom right"
|
|
73
|
-
]
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
name: "altText",
|
|
77
|
-
type: "string",
|
|
78
|
-
helperText: "Text to display when the user has images off"
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
name: "height",
|
|
82
|
-
type: "number",
|
|
83
|
-
hideFromUI: true
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
name: "width",
|
|
87
|
-
type: "number",
|
|
88
|
-
hideFromUI: true
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
name: "sizes",
|
|
92
|
-
type: "string",
|
|
93
|
-
hideFromUI: true
|
|
94
|
-
},
|
|
95
|
-
{
|
|
96
|
-
name: "srcset",
|
|
97
|
-
type: "string",
|
|
98
|
-
hideFromUI: true
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
name: "lazy",
|
|
102
|
-
type: "boolean",
|
|
103
|
-
defaultValue: true,
|
|
104
|
-
hideFromUI: true
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
name: "fitContent",
|
|
108
|
-
type: "boolean",
|
|
109
|
-
helperText: "When child blocks are provided, fit to them instead of using the image's aspect ratio",
|
|
110
|
-
defaultValue: true
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
name: "aspectRatio",
|
|
114
|
-
type: "number",
|
|
115
|
-
helperText: "This is the ratio of height/width, e.g. set to 1.5 for a 300px wide and 200px tall photo. Set to 0 to not force the image to maintain it's aspect ratio",
|
|
116
|
-
advanced: true,
|
|
117
|
-
defaultValue: 0.7041
|
|
118
|
-
}
|
|
119
|
-
]
|
|
120
|
-
});
|
|
121
|
-
export {
|
|
122
|
-
Image as default
|
|
123
|
-
};
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { View, StyleSheet, Image, Text } from "react-native";
|
|
3
|
-
import { useContext } from "react";
|
|
4
|
-
|
|
5
|
-
export default function Image(props) {
|
|
6
|
-
return (
|
|
7
|
-
<>
|
|
8
|
-
<View>
|
|
9
|
-
<View
|
|
10
|
-
loading="lazy"
|
|
11
|
-
alt={props.altText}
|
|
12
|
-
aria-role={props.altText ? "presentation" : undefined}
|
|
13
|
-
className={"builder-image" + (props.class ? " " + props.class : "")}
|
|
14
|
-
src={props.image}
|
|
15
|
-
srcset={props.srcset}
|
|
16
|
-
sizes={props.sizes}
|
|
17
|
-
style={styles.view1}
|
|
18
|
-
/>
|
|
19
|
-
|
|
20
|
-
<View srcSet={props.srcset} />
|
|
21
|
-
</View>
|
|
22
|
-
|
|
23
|
-
{props.children}
|
|
24
|
-
</>
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const styles = StyleSheet.create({
|
|
29
|
-
view1: {
|
|
30
|
-
opacity: 1,
|
|
31
|
-
transition: "opacity 0.2s ease-in-out",
|
|
32
|
-
objectFit: "cover",
|
|
33
|
-
objectPosition: "center",
|
|
34
|
-
},
|
|
35
|
-
});
|