@ez-gform/codegen 0.1.0
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/LICENSE +21 -0
- package/README.md +49 -0
- package/dist/index.cjs +614 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +582 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 webadeva
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @ez-gform/codegen
|
|
2
|
+
|
|
3
|
+
Turn a form's schema into code you can paste: TypeScript types, a React
|
|
4
|
+
component, or a plain HTML form. Runs in Node and the browser.
|
|
5
|
+
|
|
6
|
+
Just want the output? `npx @ez-gform/cli <google-form-url> --format react`
|
|
7
|
+
does this for you, no install needed.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pnpm add @ez-gform/codegen
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
`schema` is a `FormSchema` from `@ez-gform/core`'s `parseFormHtml`.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import {
|
|
21
|
+
generateHtmlForm,
|
|
22
|
+
generateReactComponent,
|
|
23
|
+
generateSchemaJson,
|
|
24
|
+
generateTypes,
|
|
25
|
+
} from "@ez-gform/codegen";
|
|
26
|
+
|
|
27
|
+
generateSchemaJson(schema); // the schema as JSON text
|
|
28
|
+
generateTypes(schema, { name: "MyForm" }); // MyFormSchema const + MyFormValues type
|
|
29
|
+
generateReactComponent(schema, { name: "MyForm" }); // a .tsx component using useGoogleForm
|
|
30
|
+
generateHtmlForm(schema); // a plain <form> that posts to Google, no JS
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Each function returns a string.
|
|
34
|
+
|
|
35
|
+
| Function | Options |
|
|
36
|
+
| ------------------------ | ------------------------------------------------------------------- |
|
|
37
|
+
| `generateSchemaJson` | `pretty` (default `true`) |
|
|
38
|
+
| `generateTypes` | `name` (default: from the form title) |
|
|
39
|
+
| `generateReactComponent` | `name`, `typescript` (default `true`; `false` emits plain JS + JSX) |
|
|
40
|
+
| `generateHtmlForm` | none |
|
|
41
|
+
|
|
42
|
+
Also exported: `toPascalCase`, `toIdentifier` (the naming helpers used above).
|
|
43
|
+
|
|
44
|
+
## Good to know
|
|
45
|
+
|
|
46
|
+
- File upload questions can't be submitted without a Google sign-in, so they
|
|
47
|
+
are left out of the types and shown as a disabled note in React/HTML output.
|
|
48
|
+
- Output is self-contained: the schema is embedded, so you can paste a single
|
|
49
|
+
file.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
generateHtmlForm: () => generateHtmlForm,
|
|
24
|
+
generateReactComponent: () => generateReactComponent,
|
|
25
|
+
generateSchemaJson: () => generateSchemaJson,
|
|
26
|
+
generateTypes: () => generateTypes,
|
|
27
|
+
toIdentifier: () => toIdentifier,
|
|
28
|
+
toPascalCase: () => toPascalCase
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/generate-html.ts
|
|
33
|
+
var import_core = require("@ez-gform/core");
|
|
34
|
+
var escapeHtml = (value) => {
|
|
35
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
36
|
+
};
|
|
37
|
+
var attr = (name, value) => {
|
|
38
|
+
return `${name}="${escapeHtml(value)}"`;
|
|
39
|
+
};
|
|
40
|
+
var renderChoiceInputs = (question, inputType) => {
|
|
41
|
+
const options = question.options ?? [];
|
|
42
|
+
const rows = options.map((option) => {
|
|
43
|
+
if (option.isOther) {
|
|
44
|
+
return [
|
|
45
|
+
`<label><input type="${inputType}" ${attr("name", question.entryId)} value="__other_option__"> Other:</label>`,
|
|
46
|
+
`<input type="text" ${attr("name", `${question.entryId}.other_option_response`)}>`
|
|
47
|
+
].join("\n ");
|
|
48
|
+
}
|
|
49
|
+
return `<label><input type="${inputType}" ${attr("name", question.entryId)} ${attr("value", option.value)}> ${escapeHtml(option.value)}</label>`;
|
|
50
|
+
});
|
|
51
|
+
return rows.join("\n ");
|
|
52
|
+
};
|
|
53
|
+
var renderGrid = (question) => {
|
|
54
|
+
const inputType = question.type === "checkbox_grid" ? "checkbox" : "radio";
|
|
55
|
+
const columns = question.options ?? [];
|
|
56
|
+
const rows = (question.rows ?? []).map((row) => {
|
|
57
|
+
const cells = columns.map((col) => {
|
|
58
|
+
return `<td><input type="${inputType}" ${attr("name", row.entryId)} ${attr("value", col.value)}></td>`;
|
|
59
|
+
}).join("");
|
|
60
|
+
return ` <tr><th>${escapeHtml(row.label)}</th>${cells}</tr>`;
|
|
61
|
+
}).join("\n");
|
|
62
|
+
return `<table>
|
|
63
|
+
${rows}
|
|
64
|
+
</table>`;
|
|
65
|
+
};
|
|
66
|
+
var renderQuestion = (question) => {
|
|
67
|
+
const label = `<label>${escapeHtml(question.title)}${question.required ? " *" : ""}</label>`;
|
|
68
|
+
const requiredAttr = question.required ? " required" : "";
|
|
69
|
+
switch (question.type) {
|
|
70
|
+
case "short_answer":
|
|
71
|
+
return `${label}
|
|
72
|
+
<input type="text" ${attr("name", question.entryId)}${requiredAttr}>`;
|
|
73
|
+
case "paragraph":
|
|
74
|
+
return `${label}
|
|
75
|
+
<textarea ${attr("name", question.entryId)}${requiredAttr}></textarea>`;
|
|
76
|
+
case "multiple_choice":
|
|
77
|
+
return `${label}
|
|
78
|
+
${renderChoiceInputs(question, "radio")}`;
|
|
79
|
+
case "checkboxes":
|
|
80
|
+
return `${label}
|
|
81
|
+
${renderChoiceInputs(question, "checkbox")}`;
|
|
82
|
+
case "dropdown": {
|
|
83
|
+
const options = (question.options ?? []).map((option) => {
|
|
84
|
+
return ` <option ${attr("value", option.value)}>${escapeHtml(option.value)}</option>`;
|
|
85
|
+
}).join("\n");
|
|
86
|
+
return `${label}
|
|
87
|
+
<select ${attr("name", question.entryId)}${requiredAttr}>
|
|
88
|
+
${options}
|
|
89
|
+
</select>`;
|
|
90
|
+
}
|
|
91
|
+
case "linear_scale": {
|
|
92
|
+
const min = question.scale?.min ?? 1;
|
|
93
|
+
const max = question.scale?.max ?? 5;
|
|
94
|
+
const inputs = [];
|
|
95
|
+
for (let value = min; value <= max; value++) {
|
|
96
|
+
inputs.push(
|
|
97
|
+
`<label><input type="radio" ${attr("name", question.entryId)} ${attr("value", String(value))}> ${value}</label>`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return `${label}
|
|
101
|
+
${inputs.join("\n")}`;
|
|
102
|
+
}
|
|
103
|
+
case "grid":
|
|
104
|
+
case "checkbox_grid":
|
|
105
|
+
return `${label}
|
|
106
|
+
${renderGrid(question)}`;
|
|
107
|
+
case "date": {
|
|
108
|
+
const inputs = [];
|
|
109
|
+
if (question.date?.includeYear !== false) {
|
|
110
|
+
inputs.push(
|
|
111
|
+
`<input type="number" ${attr("name", `${question.entryId}_year`)} placeholder="Year">`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
inputs.push(
|
|
115
|
+
`<input type="number" ${attr("name", `${question.entryId}_month`)} placeholder="Month">`
|
|
116
|
+
);
|
|
117
|
+
inputs.push(
|
|
118
|
+
`<input type="number" ${attr("name", `${question.entryId}_day`)} placeholder="Day">`
|
|
119
|
+
);
|
|
120
|
+
if (question.date?.includeTime) {
|
|
121
|
+
inputs.push(
|
|
122
|
+
`<input type="number" ${attr("name", `${question.entryId}_hour`)} placeholder="Hour">`
|
|
123
|
+
);
|
|
124
|
+
inputs.push(
|
|
125
|
+
`<input type="number" ${attr("name", `${question.entryId}_minute`)} placeholder="Minute">`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return `${label}
|
|
129
|
+
${inputs.join("\n")}`;
|
|
130
|
+
}
|
|
131
|
+
case "time":
|
|
132
|
+
return `${label}
|
|
133
|
+
<input type="number" ${attr("name", `${question.entryId}_hour`)} placeholder="Hour">
|
|
134
|
+
<input type="number" ${attr("name", `${question.entryId}_minute`)} placeholder="Minute">`;
|
|
135
|
+
case "file_upload":
|
|
136
|
+
return `${label}
|
|
137
|
+
<!-- file_upload is not supported by the Forms API without sign-in; no input rendered -->`;
|
|
138
|
+
default:
|
|
139
|
+
return label;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
var generateHtmlForm = (schema) => {
|
|
143
|
+
const { formResponse } = (0, import_core.formUrls)(schema.formId || "REPLACE_WITH_FORM_ID");
|
|
144
|
+
const body = schema.questions.map(renderQuestion).join("\n\n");
|
|
145
|
+
const hiddenFields = [];
|
|
146
|
+
if (schema.multiPage) {
|
|
147
|
+
hiddenFields.push(
|
|
148
|
+
`<input type="hidden" ${attr("name", "fbzx")} ${attr("value", schema.fbzx ?? "")}>`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return `<!-- ${escapeHtml(schema.title)} -->
|
|
152
|
+
<form action="${escapeHtml(formResponse)}" method="POST">
|
|
153
|
+
${hiddenFields.join("\n")}
|
|
154
|
+
${body}
|
|
155
|
+
|
|
156
|
+
<button type="submit">Submit</button>
|
|
157
|
+
</form>
|
|
158
|
+
`;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// src/names.ts
|
|
162
|
+
var splitWords = (input) => {
|
|
163
|
+
return input.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/[^a-zA-Z0-9]+/g, " ").trim().split(/\s+/).filter((word) => {
|
|
164
|
+
return word.length > 0;
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
var toPascalCase = (input) => {
|
|
168
|
+
const words = splitWords(input);
|
|
169
|
+
const pascal = words.map((word) => {
|
|
170
|
+
const lower = word.toLowerCase();
|
|
171
|
+
return lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
172
|
+
}).join("");
|
|
173
|
+
return pascal.length > 0 ? pascal : "Form";
|
|
174
|
+
};
|
|
175
|
+
var RESERVED_WORDS = /* @__PURE__ */ new Set([
|
|
176
|
+
"break",
|
|
177
|
+
"case",
|
|
178
|
+
"catch",
|
|
179
|
+
"class",
|
|
180
|
+
"const",
|
|
181
|
+
"continue",
|
|
182
|
+
"debugger",
|
|
183
|
+
"default",
|
|
184
|
+
"delete",
|
|
185
|
+
"do",
|
|
186
|
+
"else",
|
|
187
|
+
"export",
|
|
188
|
+
"extends",
|
|
189
|
+
"finally",
|
|
190
|
+
"for",
|
|
191
|
+
"function",
|
|
192
|
+
"if",
|
|
193
|
+
"import",
|
|
194
|
+
"in",
|
|
195
|
+
"instanceof",
|
|
196
|
+
"new",
|
|
197
|
+
"return",
|
|
198
|
+
"super",
|
|
199
|
+
"switch",
|
|
200
|
+
"this",
|
|
201
|
+
"throw",
|
|
202
|
+
"try",
|
|
203
|
+
"typeof",
|
|
204
|
+
"var",
|
|
205
|
+
"void",
|
|
206
|
+
"while",
|
|
207
|
+
"with",
|
|
208
|
+
"enum",
|
|
209
|
+
"implements",
|
|
210
|
+
"interface",
|
|
211
|
+
"let",
|
|
212
|
+
"package",
|
|
213
|
+
"private",
|
|
214
|
+
"protected",
|
|
215
|
+
"public",
|
|
216
|
+
"static",
|
|
217
|
+
"yield",
|
|
218
|
+
"await",
|
|
219
|
+
"null",
|
|
220
|
+
"true",
|
|
221
|
+
"false"
|
|
222
|
+
]);
|
|
223
|
+
var toIdentifier = (input) => {
|
|
224
|
+
const words = splitWords(input);
|
|
225
|
+
if (words.length === 0) return "Form";
|
|
226
|
+
const [first, ...rest] = words;
|
|
227
|
+
const camel = first.toLowerCase() + rest.map((word) => {
|
|
228
|
+
const lower = word.toLowerCase();
|
|
229
|
+
return lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
230
|
+
}).join("");
|
|
231
|
+
const prefixed = /^[0-9]/.test(camel) ? `_${camel}` : camel;
|
|
232
|
+
return RESERVED_WORDS.has(prefixed) ? `${prefixed}_` : prefixed;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
// src/generate-react.ts
|
|
236
|
+
var JSX_TEXT_ESCAPES = {
|
|
237
|
+
"&": "&",
|
|
238
|
+
"<": "<",
|
|
239
|
+
">": ">",
|
|
240
|
+
"{": "{",
|
|
241
|
+
"}": "}"
|
|
242
|
+
};
|
|
243
|
+
var jsxEscape = (value) => {
|
|
244
|
+
return value.replace(/[&<>{}]/g, (ch) => {
|
|
245
|
+
return JSX_TEXT_ESCAPES[ch];
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
var q = (value) => {
|
|
249
|
+
return JSON.stringify(value);
|
|
250
|
+
};
|
|
251
|
+
var hasOtherOption = (question) => {
|
|
252
|
+
return question.options?.some((option) => {
|
|
253
|
+
return option.isOther;
|
|
254
|
+
}) ?? false;
|
|
255
|
+
};
|
|
256
|
+
var renderShortText = (question, multiline) => {
|
|
257
|
+
const tag = multiline ? "textarea" : "input";
|
|
258
|
+
const typeAttr = multiline ? "" : ' type="text"';
|
|
259
|
+
return `<${tag}${typeAttr} {...form.register(${q(question.entryId)})}${question.required ? " required" : ""} />`;
|
|
260
|
+
};
|
|
261
|
+
var renderRadioGroup = (question) => {
|
|
262
|
+
const entryId = q(question.entryId);
|
|
263
|
+
const options = (question.options ?? []).map((option) => {
|
|
264
|
+
if (option.isOther) {
|
|
265
|
+
return ` <label>
|
|
266
|
+
<input
|
|
267
|
+
type="radio"
|
|
268
|
+
name={form.register(${entryId}).name}
|
|
269
|
+
checked={typeof form.values[${entryId}] === "object"}
|
|
270
|
+
onChange={() => form.setValue(${entryId}, { other: "" })}
|
|
271
|
+
/>
|
|
272
|
+
Other:
|
|
273
|
+
<input
|
|
274
|
+
type="text"
|
|
275
|
+
onChange={(e) => form.setValue(${entryId}, { other: e.target.value })}
|
|
276
|
+
/>
|
|
277
|
+
</label>`;
|
|
278
|
+
}
|
|
279
|
+
const value = q(option.value);
|
|
280
|
+
return ` <label>
|
|
281
|
+
<input
|
|
282
|
+
type="radio"
|
|
283
|
+
name={form.register(${entryId}).name}
|
|
284
|
+
value={${value}}
|
|
285
|
+
checked={form.register(${entryId}).value === ${value}}
|
|
286
|
+
onChange={() => form.setValue(${entryId}, ${value})}
|
|
287
|
+
/>
|
|
288
|
+
${jsxEscape(option.value)}
|
|
289
|
+
</label>`;
|
|
290
|
+
}).join("\n");
|
|
291
|
+
return `<div role="radiogroup">
|
|
292
|
+
${options}
|
|
293
|
+
</div>`;
|
|
294
|
+
};
|
|
295
|
+
var renderSelect = (question) => {
|
|
296
|
+
const options = (question.options ?? []).map((option) => {
|
|
297
|
+
return ` <option value={${q(option.value)}}>${jsxEscape(option.value)}</option>`;
|
|
298
|
+
}).join("\n");
|
|
299
|
+
return `<select {...form.register(${q(question.entryId)})}${question.required ? " required" : ""}>
|
|
300
|
+
<option value="">Select\u2026</option>
|
|
301
|
+
${options}
|
|
302
|
+
</select>`;
|
|
303
|
+
};
|
|
304
|
+
var renderCheckboxGroup = (question) => {
|
|
305
|
+
const entryId = q(question.entryId);
|
|
306
|
+
const options = (question.options ?? []).map((option) => {
|
|
307
|
+
if (option.isOther) {
|
|
308
|
+
return ` <label>
|
|
309
|
+
<input
|
|
310
|
+
type="checkbox"
|
|
311
|
+
onChange={(e) => form.setValue(${entryId}, e.target.checked ? { other: "" } : undefined)}
|
|
312
|
+
/>
|
|
313
|
+
Other:
|
|
314
|
+
<input
|
|
315
|
+
type="text"
|
|
316
|
+
onChange={(e) => form.setValue(${entryId}, { other: e.target.value })}
|
|
317
|
+
/>
|
|
318
|
+
</label>`;
|
|
319
|
+
}
|
|
320
|
+
const value = q(option.value);
|
|
321
|
+
return ` <label>
|
|
322
|
+
<input type="checkbox" {...form.registerCheckbox(${entryId}, ${value})} />
|
|
323
|
+
${jsxEscape(option.value)}
|
|
324
|
+
</label>`;
|
|
325
|
+
}).join("\n");
|
|
326
|
+
return `<div>
|
|
327
|
+
${options}
|
|
328
|
+
</div>`;
|
|
329
|
+
};
|
|
330
|
+
var renderLinearScale = (question) => {
|
|
331
|
+
const entryId = q(question.entryId);
|
|
332
|
+
const min = question.scale?.min ?? 1;
|
|
333
|
+
const max = question.scale?.max ?? 5;
|
|
334
|
+
const items = [];
|
|
335
|
+
for (let value = min; value <= max; value++) {
|
|
336
|
+
items.push(` <label>
|
|
337
|
+
<input
|
|
338
|
+
type="radio"
|
|
339
|
+
name={form.register(${entryId}).name}
|
|
340
|
+
checked={form.register(${entryId}).value === ${q(String(value))}}
|
|
341
|
+
onChange={() => form.setValue(${entryId}, ${value})}
|
|
342
|
+
/>
|
|
343
|
+
${value}
|
|
344
|
+
</label>`);
|
|
345
|
+
}
|
|
346
|
+
const lowLabel = question.scale?.lowLabel;
|
|
347
|
+
const highLabel = question.scale?.highLabel;
|
|
348
|
+
return `<div role="radiogroup">
|
|
349
|
+
${lowLabel ? `<span>${jsxEscape(lowLabel)}</span>` : ""}
|
|
350
|
+
${items.join("\n")}
|
|
351
|
+
${highLabel ? `<span>${jsxEscape(highLabel)}</span>` : ""}
|
|
352
|
+
</div>`;
|
|
353
|
+
};
|
|
354
|
+
var renderGrid2 = (question) => {
|
|
355
|
+
const isCheckbox = question.type === "checkbox_grid";
|
|
356
|
+
const columns = question.options ?? [];
|
|
357
|
+
const headerCells = columns.map((col) => {
|
|
358
|
+
return `<th key={${q(col.value)}}>${jsxEscape(col.value)}</th>`;
|
|
359
|
+
}).join("\n ");
|
|
360
|
+
const rows = (question.rows ?? []).map((row) => {
|
|
361
|
+
const entryId = q(row.entryId);
|
|
362
|
+
const cells = columns.map((col) => {
|
|
363
|
+
const value = q(col.value);
|
|
364
|
+
if (isCheckbox) {
|
|
365
|
+
return `<td key={${value}}><input type="checkbox" {...form.registerCheckbox(${entryId}, ${value})} /></td>`;
|
|
366
|
+
}
|
|
367
|
+
return `<td key={${value}}>
|
|
368
|
+
<input
|
|
369
|
+
type="radio"
|
|
370
|
+
name={form.register(${entryId}).name}
|
|
371
|
+
checked={form.register(${entryId}).value === ${value}}
|
|
372
|
+
onChange={() => form.setValue(${entryId}, ${value})}
|
|
373
|
+
/>
|
|
374
|
+
</td>`;
|
|
375
|
+
}).join("\n ");
|
|
376
|
+
return ` <tr>
|
|
377
|
+
<th>${jsxEscape(row.label)}</th>
|
|
378
|
+
${cells}
|
|
379
|
+
</tr>`;
|
|
380
|
+
}).join("\n");
|
|
381
|
+
return `<table>
|
|
382
|
+
<thead>
|
|
383
|
+
<tr>
|
|
384
|
+
<th />
|
|
385
|
+
${headerCells}
|
|
386
|
+
</tr>
|
|
387
|
+
</thead>
|
|
388
|
+
<tbody>
|
|
389
|
+
${rows}
|
|
390
|
+
</tbody>
|
|
391
|
+
</table>`;
|
|
392
|
+
};
|
|
393
|
+
var renderDate = (question) => {
|
|
394
|
+
const entryId = q(question.entryId);
|
|
395
|
+
return `<input
|
|
396
|
+
type="date"
|
|
397
|
+
required={${question.required ? "true" : "false"}}
|
|
398
|
+
onChange={(e) => {
|
|
399
|
+
const parts = e.target.value.split("-").map(Number);
|
|
400
|
+
const [year, month, day] = parts;
|
|
401
|
+
if (month === undefined || day === undefined) return;
|
|
402
|
+
form.setValue(${entryId}, { year, month, day });
|
|
403
|
+
}}
|
|
404
|
+
/>`;
|
|
405
|
+
};
|
|
406
|
+
var renderTime = (question) => {
|
|
407
|
+
const entryId = q(question.entryId);
|
|
408
|
+
return `<input
|
|
409
|
+
type="time"
|
|
410
|
+
required={${question.required ? "true" : "false"}}
|
|
411
|
+
onChange={(e) => {
|
|
412
|
+
const parts = e.target.value.split(":").map(Number);
|
|
413
|
+
const [hour, minute] = parts;
|
|
414
|
+
if (hour === undefined || minute === undefined) return;
|
|
415
|
+
form.setValue(${entryId}, { hour, minute });
|
|
416
|
+
}}
|
|
417
|
+
/>`;
|
|
418
|
+
};
|
|
419
|
+
var renderQuestion2 = (question) => {
|
|
420
|
+
const label = `<label>${jsxEscape(question.title)}${question.required ? " *" : ""}</label>`;
|
|
421
|
+
const description = question.description ? `
|
|
422
|
+
<p>${jsxEscape(question.description)}</p>` : "";
|
|
423
|
+
let field;
|
|
424
|
+
switch (question.type) {
|
|
425
|
+
case "short_answer":
|
|
426
|
+
field = renderShortText(question, false);
|
|
427
|
+
break;
|
|
428
|
+
case "paragraph":
|
|
429
|
+
field = renderShortText(question, true);
|
|
430
|
+
break;
|
|
431
|
+
case "multiple_choice":
|
|
432
|
+
field = renderRadioGroup(question);
|
|
433
|
+
break;
|
|
434
|
+
case "dropdown":
|
|
435
|
+
field = renderSelect(question);
|
|
436
|
+
break;
|
|
437
|
+
case "checkboxes":
|
|
438
|
+
field = renderCheckboxGroup(question);
|
|
439
|
+
break;
|
|
440
|
+
case "linear_scale":
|
|
441
|
+
field = renderLinearScale(question);
|
|
442
|
+
break;
|
|
443
|
+
case "grid":
|
|
444
|
+
case "checkbox_grid":
|
|
445
|
+
field = renderGrid2(question);
|
|
446
|
+
break;
|
|
447
|
+
case "date":
|
|
448
|
+
field = renderDate(question);
|
|
449
|
+
break;
|
|
450
|
+
case "time":
|
|
451
|
+
field = renderTime(question);
|
|
452
|
+
break;
|
|
453
|
+
case "file_upload":
|
|
454
|
+
field = `<p>"${jsxEscape(question.title)}" is a file upload question and is not supported by Google Forms submission without sign-in.</p>`;
|
|
455
|
+
break;
|
|
456
|
+
default:
|
|
457
|
+
field = `<input {...form.register(${q(question.entryId)})} />`;
|
|
458
|
+
}
|
|
459
|
+
return ` <div key={${q(question.id)}}>
|
|
460
|
+
${label}${description}
|
|
461
|
+
${field}
|
|
462
|
+
</div>`;
|
|
463
|
+
};
|
|
464
|
+
var renderSection = (section, questionsById) => {
|
|
465
|
+
const heading = section.title ? ` <h2>${jsxEscape(section.title)}</h2>
|
|
466
|
+
` : "";
|
|
467
|
+
const description = section.description ? ` <p>${jsxEscape(section.description)}</p>
|
|
468
|
+
` : "";
|
|
469
|
+
const body = section.questionIds.map((id) => {
|
|
470
|
+
return questionsById.get(id);
|
|
471
|
+
}).filter((question) => {
|
|
472
|
+
return question !== void 0;
|
|
473
|
+
}).map(renderQuestion2).join("\n");
|
|
474
|
+
return `${heading}${description}${body}`;
|
|
475
|
+
};
|
|
476
|
+
var usesOther = (question) => {
|
|
477
|
+
return hasOtherOption(question) && (question.type === "multiple_choice" || question.type === "checkboxes");
|
|
478
|
+
};
|
|
479
|
+
var generateReactComponent = (schema, options = {}) => {
|
|
480
|
+
const typescript = options.typescript ?? true;
|
|
481
|
+
const name = options.name ?? `${toPascalCase(schema.title)}Form`;
|
|
482
|
+
const schemaJson = JSON.stringify(schema, null, 2);
|
|
483
|
+
void usesOther;
|
|
484
|
+
const questionsById = new Map(
|
|
485
|
+
schema.questions.map((qn) => {
|
|
486
|
+
return [qn.id, qn];
|
|
487
|
+
})
|
|
488
|
+
);
|
|
489
|
+
const body = schema.sections.length > 1 || schema.sections[0]?.title && schema.sections[0].title.length > 0 ? schema.sections.map((section) => {
|
|
490
|
+
return renderSection(section, questionsById);
|
|
491
|
+
}).join("\n") : schema.questions.map(renderQuestion2).join("\n");
|
|
492
|
+
const schemaDeclaration = typescript ? `${schemaJson} as const satisfies FormSchema` : schemaJson;
|
|
493
|
+
const typeImport = typescript ? `import type { FormSchema } from "@ez-gform/core";
|
|
494
|
+
` : "";
|
|
495
|
+
return `${typeImport}import { useGoogleForm } from "@ez-gform/react";
|
|
496
|
+
|
|
497
|
+
const schema = ${schemaDeclaration};
|
|
498
|
+
|
|
499
|
+
export function ${name}() {
|
|
500
|
+
const form = useGoogleForm({ formId: schema.formId, schema });
|
|
501
|
+
|
|
502
|
+
return (
|
|
503
|
+
<form
|
|
504
|
+
onSubmit={form.submit}
|
|
505
|
+
>
|
|
506
|
+
<h1>${jsxEscape(schema.title)}</h1>
|
|
507
|
+
${schema.description ? `<p>${jsxEscape(schema.description)}</p>` : ""}
|
|
508
|
+
${body}
|
|
509
|
+
|
|
510
|
+
<div>Status: {form.status}</div>
|
|
511
|
+
{form.errors && form.errors.length > 0 && (
|
|
512
|
+
<ul>
|
|
513
|
+
{form.errors.map((error, index) => (
|
|
514
|
+
<li key={index}>{error.message}</li>
|
|
515
|
+
))}
|
|
516
|
+
</ul>
|
|
517
|
+
)}
|
|
518
|
+
|
|
519
|
+
<button type="submit" disabled={form.status === "submitting"}>
|
|
520
|
+
Submit
|
|
521
|
+
</button>
|
|
522
|
+
</form>
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
`;
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
// src/generate-types.ts
|
|
529
|
+
var quote = (value) => {
|
|
530
|
+
return JSON.stringify(value);
|
|
531
|
+
};
|
|
532
|
+
var hasOtherOption2 = (question) => {
|
|
533
|
+
return question.options?.some((option) => {
|
|
534
|
+
return option.isOther;
|
|
535
|
+
}) ?? false;
|
|
536
|
+
};
|
|
537
|
+
var fieldType = (question) => {
|
|
538
|
+
switch (question.type) {
|
|
539
|
+
case "short_answer":
|
|
540
|
+
case "paragraph":
|
|
541
|
+
return "string";
|
|
542
|
+
case "multiple_choice":
|
|
543
|
+
case "dropdown":
|
|
544
|
+
return hasOtherOption2(question) ? "string | { other: string }" : "string";
|
|
545
|
+
case "checkboxes":
|
|
546
|
+
return hasOtherOption2(question) ? "(string | { other: string })[]" : "string[]";
|
|
547
|
+
case "linear_scale":
|
|
548
|
+
return "number";
|
|
549
|
+
case "date":
|
|
550
|
+
return "DateValue";
|
|
551
|
+
case "time":
|
|
552
|
+
return "TimeValue";
|
|
553
|
+
case "grid":
|
|
554
|
+
case "checkbox_grid": {
|
|
555
|
+
const rowIds = (question.rows ?? []).map((row) => {
|
|
556
|
+
return quote(row.entryId);
|
|
557
|
+
});
|
|
558
|
+
const keyType = rowIds.length > 0 ? rowIds.join(" | ") : "string";
|
|
559
|
+
return `Record<${keyType}, string | string[]>`;
|
|
560
|
+
}
|
|
561
|
+
case "file_upload":
|
|
562
|
+
return void 0;
|
|
563
|
+
default:
|
|
564
|
+
return "string";
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
var generateTypes = (schema, options = {}) => {
|
|
568
|
+
const name = options.name ?? toPascalCase(schema.title);
|
|
569
|
+
const schemaJson = JSON.stringify(schema, null, 2);
|
|
570
|
+
const usesDate = schema.questions.some((q2) => {
|
|
571
|
+
return q2.type === "date";
|
|
572
|
+
});
|
|
573
|
+
const usesTime = schema.questions.some((q2) => {
|
|
574
|
+
return q2.type === "time";
|
|
575
|
+
});
|
|
576
|
+
const extraTypeImports = [
|
|
577
|
+
usesDate ? "DateValue" : void 0,
|
|
578
|
+
usesTime ? "TimeValue" : void 0
|
|
579
|
+
].filter((t) => {
|
|
580
|
+
return t !== void 0;
|
|
581
|
+
});
|
|
582
|
+
const fields = [];
|
|
583
|
+
for (const question of schema.questions) {
|
|
584
|
+
const type = fieldType(question);
|
|
585
|
+
if (type === void 0) continue;
|
|
586
|
+
const optional = question.required ? "" : "?";
|
|
587
|
+
fields.push(` ${quote(question.entryId)}${optional}: ${type};`);
|
|
588
|
+
}
|
|
589
|
+
const importLine = ["FormSchema", ...extraTypeImports].sort().join(", ");
|
|
590
|
+
return `import type { ${importLine} } from "@ez-gform/core";
|
|
591
|
+
|
|
592
|
+
export const ${name}Schema = ${schemaJson} as const satisfies FormSchema;
|
|
593
|
+
|
|
594
|
+
export type ${name}Values = {
|
|
595
|
+
${fields.join("\n")}
|
|
596
|
+
};
|
|
597
|
+
`;
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
// src/schema-json.ts
|
|
601
|
+
var generateSchemaJson = (schema, options = {}) => {
|
|
602
|
+
const pretty = options.pretty ?? true;
|
|
603
|
+
return pretty ? JSON.stringify(schema, null, 2) : JSON.stringify(schema);
|
|
604
|
+
};
|
|
605
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
606
|
+
0 && (module.exports = {
|
|
607
|
+
generateHtmlForm,
|
|
608
|
+
generateReactComponent,
|
|
609
|
+
generateSchemaJson,
|
|
610
|
+
generateTypes,
|
|
611
|
+
toIdentifier,
|
|
612
|
+
toPascalCase
|
|
613
|
+
});
|
|
614
|
+
//# sourceMappingURL=index.cjs.map
|