@betterstart/cli 0.1.40 → 0.1.41
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/dist/cli.js +48 -7
- package/dist/cli.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -2226,6 +2226,15 @@ function toTypeScriptType(field, mode = "output") {
|
|
|
2226
2226
|
}
|
|
2227
2227
|
|
|
2228
2228
|
// src/generators/form-pipeline/form-component.ts
|
|
2229
|
+
function resolveUiImport(cwd, componentName) {
|
|
2230
|
+
const locations = ["components/ui", "src/components/ui"];
|
|
2231
|
+
for (const loc of locations) {
|
|
2232
|
+
if (fs7.existsSync(path7.join(cwd, loc, `${componentName}.tsx`)) || fs7.existsSync(path7.join(cwd, loc, `${componentName}.ts`))) {
|
|
2233
|
+
return `@/components/ui/${componentName}`;
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
return `@cms/components/ui/${componentName}`;
|
|
2237
|
+
}
|
|
2229
2238
|
function escapeJsx(str) {
|
|
2230
2239
|
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
2231
2240
|
}
|
|
@@ -2256,13 +2265,45 @@ function generateFormComponent(schema, cwd, cmsDir, options) {
|
|
|
2256
2265
|
const fieldArrayDecls = listFields.map(
|
|
2257
2266
|
(f) => ` const ${f.name}FieldArray = useFieldArray({ control: form.control, name: '${f.name}' })`
|
|
2258
2267
|
).join("\n");
|
|
2259
|
-
const
|
|
2268
|
+
const watchFields = /* @__PURE__ */ new Set();
|
|
2269
|
+
for (const f of fields) {
|
|
2270
|
+
if (f.showWhen) {
|
|
2271
|
+
watchFields.add(f.showWhen.field);
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
const watchDecls = Array.from(watchFields).map((wf) => ` const ${wf}Value = form.watch('${wf}')`).join("\n");
|
|
2275
|
+
const watchSetup = watchFields.size > 0 ? `
|
|
2276
|
+
${watchDecls}
|
|
2277
|
+
` : "";
|
|
2278
|
+
const fieldJSX = fields.filter((f) => f.name && !f.hidden).map((f) => {
|
|
2279
|
+
let jsx = generateFieldJSX(f);
|
|
2280
|
+
if (f.showWhen) {
|
|
2281
|
+
const watchVar = `${f.showWhen.field}Value`;
|
|
2282
|
+
const { value } = f.showWhen;
|
|
2283
|
+
if (Array.isArray(value)) {
|
|
2284
|
+
const vals = value.map((v) => `'${v}'`).join(", ");
|
|
2285
|
+
jsx = ` {[${vals}].includes(${watchVar} as string) && (
|
|
2286
|
+
${jsx}
|
|
2287
|
+
)}`;
|
|
2288
|
+
} else {
|
|
2289
|
+
jsx = ` {${watchVar} === '${value}' && (
|
|
2290
|
+
${jsx}
|
|
2291
|
+
)}`;
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
return jsx;
|
|
2295
|
+
}).join("\n\n");
|
|
2260
2296
|
const submitText = schema.submitButtonText || "Submit";
|
|
2261
2297
|
const successMessage = escapeJsx(schema.successMessage || "Form submitted successfully!");
|
|
2262
2298
|
const rhfImport = hasListFields ? `import { useFieldArray, useForm } from 'react-hook-form'` : `import { useForm } from 'react-hook-form'`;
|
|
2263
2299
|
const fieldArraySetup = hasListFields ? `
|
|
2264
2300
|
${fieldArrayDecls}
|
|
2265
2301
|
` : "";
|
|
2302
|
+
const buttonImport = resolveUiImport(cwd, "button");
|
|
2303
|
+
const formImport = resolveUiImport(cwd, "form");
|
|
2304
|
+
const inputImport = resolveUiImport(cwd, "input");
|
|
2305
|
+
const textareaImport = resolveUiImport(cwd, "textarea");
|
|
2306
|
+
const selectImport = resolveUiImport(cwd, "select");
|
|
2266
2307
|
const content = `'use client'
|
|
2267
2308
|
|
|
2268
2309
|
import { zodResolver } from '@hookform/resolvers/zod'
|
|
@@ -2270,7 +2311,7 @@ import { useState } from 'react'
|
|
|
2270
2311
|
${rhfImport}
|
|
2271
2312
|
import { z } from 'zod/v3'
|
|
2272
2313
|
import { create${pascal}Submission } from '@cms/actions/${kebab}-form'
|
|
2273
|
-
import { Button } from '
|
|
2314
|
+
import { Button } from '${buttonImport}'
|
|
2274
2315
|
import {
|
|
2275
2316
|
Form,
|
|
2276
2317
|
FormControl,
|
|
@@ -2279,16 +2320,16 @@ import {
|
|
|
2279
2320
|
FormItem,
|
|
2280
2321
|
FormLabel,
|
|
2281
2322
|
FormMessage,
|
|
2282
|
-
} from '
|
|
2283
|
-
import { Input } from '
|
|
2284
|
-
import { Textarea } from '
|
|
2323
|
+
} from '${formImport}'
|
|
2324
|
+
import { Input } from '${inputImport}'
|
|
2325
|
+
import { Textarea } from '${textareaImport}'
|
|
2285
2326
|
import {
|
|
2286
2327
|
Select,
|
|
2287
2328
|
SelectContent,
|
|
2288
2329
|
SelectItem,
|
|
2289
2330
|
SelectTrigger,
|
|
2290
2331
|
SelectValue,
|
|
2291
|
-
} from '
|
|
2332
|
+
} from '${selectImport}'
|
|
2292
2333
|
|
|
2293
2334
|
const formSchema = z.object({
|
|
2294
2335
|
${zodFields}
|
|
@@ -2306,7 +2347,7 @@ export function ${pascal}Form() {
|
|
|
2306
2347
|
${defaults}
|
|
2307
2348
|
},
|
|
2308
2349
|
})
|
|
2309
|
-
${fieldArraySetup}
|
|
2350
|
+
${fieldArraySetup}${watchSetup}
|
|
2310
2351
|
async function onSubmit(values: FormValues) {
|
|
2311
2352
|
setSubmitting(true)
|
|
2312
2353
|
try {
|