@uxf/form 11.31.0 → 11.31.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.
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { components } from "../components";
|
|
2
|
+
export interface FieldDefinition {
|
|
3
|
+
type: keyof typeof components;
|
|
4
|
+
name: string;
|
|
5
|
+
label: string;
|
|
6
|
+
isRequired?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare class FormCodeGenerator {
|
|
9
|
+
private formName;
|
|
10
|
+
private fields;
|
|
11
|
+
constructor(formName: string);
|
|
12
|
+
getFieldsCount(): number;
|
|
13
|
+
add(field: FieldDefinition): this;
|
|
14
|
+
generateComponent(field: FieldDefinition): string | null;
|
|
15
|
+
generate(): string;
|
|
16
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FormCodeGenerator = void 0;
|
|
4
|
+
function unique(value, index, array) {
|
|
5
|
+
return array.indexOf(value) === index;
|
|
6
|
+
}
|
|
7
|
+
function camelize(text) {
|
|
8
|
+
return text
|
|
9
|
+
.split(/[-_]+/)
|
|
10
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
11
|
+
.join("");
|
|
12
|
+
}
|
|
13
|
+
class FormCodeGenerator {
|
|
14
|
+
constructor(formName) {
|
|
15
|
+
this.fields = [];
|
|
16
|
+
this.formName = formName;
|
|
17
|
+
}
|
|
18
|
+
getFieldsCount() {
|
|
19
|
+
return this.fields.length;
|
|
20
|
+
}
|
|
21
|
+
add(field) {
|
|
22
|
+
this.fields.push(field);
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
generateComponent(field) {
|
|
26
|
+
return `<${camelize(field.type)} control={formApi.control} name="${field.name}" label="${field.label}" ${field.isRequired ? "isRequired " : ""}/>`;
|
|
27
|
+
}
|
|
28
|
+
generate() {
|
|
29
|
+
const componentName = camelize(this.formName);
|
|
30
|
+
return `import React from "react";
|
|
31
|
+
import { Form } from "@uxf/form/form";
|
|
32
|
+
import { useForm, SubmitHandler } from "react-hook-form";
|
|
33
|
+
${this.fields
|
|
34
|
+
.map((field) => field.type)
|
|
35
|
+
.filter(unique)
|
|
36
|
+
.map((type) => `import { ${camelize(type)}, ${camelize(type)}Value } from "@uxf/form/${type}";`)
|
|
37
|
+
.join("\n")}
|
|
38
|
+
|
|
39
|
+
export interface ${componentName}Data {
|
|
40
|
+
${this.fields.map((field) => ` ${field.name}: ${camelize(field.type)}Value;`).join("\n")}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ${componentName}Props {
|
|
44
|
+
onSubmit: SubmitHandler<${componentName}Data>;
|
|
45
|
+
defaultValues: ${componentName}Data;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function mapToFormData(values: any): ${componentName}Data {
|
|
49
|
+
return {
|
|
50
|
+
${this.fields.map((field) => ` ${field.name}: values.TODO`).join(",\n")}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function ${componentName}(props: ${componentName}Props)
|
|
55
|
+
{
|
|
56
|
+
const formApi = useForm({
|
|
57
|
+
defaultValues: props.defaultValues,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<Form formApi={formApi} onSubmit={props.onSubmit} id="${this.formName}">
|
|
62
|
+
${this.fields.map((field) => ` ${this.generateComponent(field)}`).join("\n")}
|
|
63
|
+
</Form>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
`;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.FormCodeGenerator = FormCodeGenerator;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/* eslint-disable no-console */
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const node_readline_1 = require("node:readline");
|
|
6
|
+
const form_code_generator_1 = require("../_code-generator/form-code-generator");
|
|
7
|
+
const components_1 = require("../components");
|
|
8
|
+
const rl = (0, node_readline_1.createInterface)({ input: process.stdin, output: process.stdout });
|
|
9
|
+
function question(text) {
|
|
10
|
+
return new Promise((resolve) => rl.question(text, resolve));
|
|
11
|
+
}
|
|
12
|
+
async function run() {
|
|
13
|
+
const formName = await question("Zadejte název formuláře: ");
|
|
14
|
+
const formGenerator = new form_code_generator_1.FormCodeGenerator(formName);
|
|
15
|
+
const allowedComponents = Object.keys(components_1.components).filter((item) => item !== "form");
|
|
16
|
+
console.log();
|
|
17
|
+
console.log(allowedComponents.map((component, index) => `[${index}] ${component}`).join("\n"));
|
|
18
|
+
console.log();
|
|
19
|
+
let componentDefinition = null;
|
|
20
|
+
do {
|
|
21
|
+
// eslint-disable-next-line no-await-in-loop
|
|
22
|
+
componentDefinition = await question(`Komponenta ${formGenerator.getFieldsCount() + 1} [type, name, label, isRequired]: `);
|
|
23
|
+
if (componentDefinition) {
|
|
24
|
+
const [componentIndexString, name, label, required] = componentDefinition
|
|
25
|
+
.split(",")
|
|
26
|
+
.map((item) => item.trim());
|
|
27
|
+
const componentIndex = Number.parseInt(componentIndexString, 10);
|
|
28
|
+
const componentType = allowedComponents[componentIndex];
|
|
29
|
+
if (!name || !label) {
|
|
30
|
+
console.log("❗ Špatná definice. Komponenta nebyla přidána.\n");
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const fieldDefinition = {
|
|
34
|
+
type: componentType,
|
|
35
|
+
name,
|
|
36
|
+
label,
|
|
37
|
+
isRequired: ["true", "1", "r", "required", "isRequired"].includes(required),
|
|
38
|
+
};
|
|
39
|
+
console.log(`✅ ${formGenerator.generateComponent(fieldDefinition)}\n`);
|
|
40
|
+
formGenerator.add(fieldDefinition);
|
|
41
|
+
}
|
|
42
|
+
} while (componentDefinition !== "");
|
|
43
|
+
const generatedFilename = `${process.cwd()}/${formName}.tsx`;
|
|
44
|
+
(0, fs_1.writeFileSync)(generatedFilename, formGenerator.generate());
|
|
45
|
+
console.log(`Generated file: ${generatedFilename}`);
|
|
46
|
+
}
|
|
47
|
+
run()
|
|
48
|
+
.then(() => {
|
|
49
|
+
process.exit(1);
|
|
50
|
+
})
|
|
51
|
+
.catch(console.error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uxf/form",
|
|
3
|
-
"version": "11.31.
|
|
3
|
+
"version": "11.31.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
"typecheck": "tsc --noEmit --skipLibCheck"
|
|
11
11
|
},
|
|
12
12
|
"author": "UX Fans s.r.o",
|
|
13
|
+
"bin": {
|
|
14
|
+
"uxf-form-code-generator": "bin/form-code-generator.js"
|
|
15
|
+
},
|
|
13
16
|
"license": "MIT",
|
|
14
17
|
"dependencies": {
|
|
15
18
|
"@uxf/ui": "11.31.0",
|