pdyform 2.0.1 → 2.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/package.json +9 -3
- package/packages/core/dist/chunk-TP3IHKWV.js +69 -0
- package/packages/core/dist/{chunk-GQASS6PM.js → chunk-WEDHXOHH.js} +22 -0
- package/packages/core/dist/formState.cjs +187 -0
- package/packages/core/dist/formState.d.cts +17 -0
- package/packages/core/dist/formState.d.ts +17 -0
- package/packages/core/dist/formState.js +15 -0
- package/packages/core/dist/index.cjs +92 -2
- package/packages/core/dist/index.d.cts +2 -1
- package/packages/core/dist/index.d.ts +2 -1
- package/packages/core/dist/index.js +21 -3
- package/packages/core/dist/utils.cjs +27 -2
- package/packages/core/dist/utils.d.cts +4 -1
- package/packages/core/dist/utils.d.ts +4 -1
- package/packages/core/dist/utils.js +9 -3
- package/packages/react/dist/index.cjs +14 -43
- package/packages/react/dist/index.js +20 -43
- package/packages/vue/dist/index.d.ts +2 -36
- package/packages/vue/dist/index.js +1 -28
- package/packages/vue/dist/index.mjs +267 -6530
- package/eslint.config.mjs +0 -53
- package/example/README.md +0 -36
- package/example/react-demo/index.html +0 -12
- package/example/react-demo/node_modules/.bin/browserslist +0 -17
- package/example/react-demo/node_modules/.bin/tsc +0 -17
- package/example/react-demo/node_modules/.bin/tsserver +0 -17
- package/example/react-demo/node_modules/.bin/vite +0 -17
- package/example/react-demo/node_modules/.vite/deps/@radix-ui_react-checkbox.js +0 -300
- package/example/react-demo/node_modules/.vite/deps/@radix-ui_react-checkbox.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/@radix-ui_react-label.js +0 -194
- package/example/react-demo/node_modules/.vite/deps/@radix-ui_react-label.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/@radix-ui_react-radio-group.js +0 -530
- package/example/react-demo/node_modules/.vite/deps/@radix-ui_react-radio-group.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/@radix-ui_react-select.js +0 -4808
- package/example/react-demo/node_modules/.vite/deps/@radix-ui_react-select.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/_metadata.json +0 -115
- package/example/react-demo/node_modules/.vite/deps/chunk-3D5PZ6F6.js +0 -49
- package/example/react-demo/node_modules/.vite/deps/chunk-3D5PZ6F6.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/chunk-5Q2RBQLA.js +0 -127
- package/example/react-demo/node_modules/.vite/deps/chunk-5Q2RBQLA.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/chunk-G3PMV62Z.js +0 -36
- package/example/react-demo/node_modules/.vite/deps/chunk-G3PMV62Z.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/chunk-GX7YZ5KV.js +0 -370
- package/example/react-demo/node_modules/.vite/deps/chunk-GX7YZ5KV.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/chunk-PUFJGYAC.js +0 -928
- package/example/react-demo/node_modules/.vite/deps/chunk-PUFJGYAC.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/chunk-SIU35MPB.js +0 -21
- package/example/react-demo/node_modules/.vite/deps/chunk-SIU35MPB.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/chunk-TOMGVNQP.js +0 -1906
- package/example/react-demo/node_modules/.vite/deps/chunk-TOMGVNQP.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/chunk-YYN6DZAU.js +0 -21628
- package/example/react-demo/node_modules/.vite/deps/chunk-YYN6DZAU.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/chunk-ZE5VSJFE.js +0 -144
- package/example/react-demo/node_modules/.vite/deps/chunk-ZE5VSJFE.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/class-variance-authority.js +0 -51
- package/example/react-demo/node_modules/.vite/deps/class-variance-authority.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/clsx.js +0 -10
- package/example/react-demo/node_modules/.vite/deps/clsx.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/lucide-react.js +0 -29725
- package/example/react-demo/node_modules/.vite/deps/lucide-react.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/package.json +0 -3
- package/example/react-demo/node_modules/.vite/deps/react-dom.js +0 -7
- package/example/react-demo/node_modules/.vite/deps/react-dom.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/react-dom_client.js +0 -39
- package/example/react-demo/node_modules/.vite/deps/react-dom_client.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/react.js +0 -6
- package/example/react-demo/node_modules/.vite/deps/react.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/react_jsx-dev-runtime.js +0 -913
- package/example/react-demo/node_modules/.vite/deps/react_jsx-dev-runtime.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/react_jsx-runtime.js +0 -7
- package/example/react-demo/node_modules/.vite/deps/react_jsx-runtime.js.map +0 -7
- package/example/react-demo/node_modules/.vite/deps/tailwind-merge.js +0 -2534
- package/example/react-demo/node_modules/.vite/deps/tailwind-merge.js.map +0 -7
- package/example/react-demo/package.json +0 -23
- package/example/react-demo/postcss.config.mjs +0 -6
- package/example/react-demo/src/App.tsx +0 -64
- package/example/react-demo/src/main.tsx +0 -10
- package/example/react-demo/src/styles.css +0 -102
- package/example/react-demo/tailwind.config.mjs +0 -50
- package/example/react-demo/tsconfig.json +0 -16
- package/example/react-demo/vite.config.ts +0 -14
- package/example/shared/defaultSchema.ts +0 -68
- package/example/vue-demo/index.html +0 -12
- package/example/vue-demo/node_modules/.bin/tsc +0 -17
- package/example/vue-demo/node_modules/.bin/tsserver +0 -17
- package/example/vue-demo/node_modules/.bin/vite +0 -17
- package/example/vue-demo/node_modules/.vite/deps/_metadata.json +0 -46
- package/example/vue-demo/node_modules/.vite/deps/chunk-PZ5AY32C.js +0 -10
- package/example/vue-demo/node_modules/.vite/deps/chunk-PZ5AY32C.js.map +0 -7
- package/example/vue-demo/node_modules/.vite/deps/chunk-TCXBSQ4M.js +0 -12877
- package/example/vue-demo/node_modules/.vite/deps/chunk-TCXBSQ4M.js.map +0 -7
- package/example/vue-demo/node_modules/.vite/deps/clsx.js +0 -22
- package/example/vue-demo/node_modules/.vite/deps/clsx.js.map +0 -7
- package/example/vue-demo/node_modules/.vite/deps/lucide-vue-next.js +0 -29720
- package/example/vue-demo/node_modules/.vite/deps/lucide-vue-next.js.map +0 -7
- package/example/vue-demo/node_modules/.vite/deps/package.json +0 -3
- package/example/vue-demo/node_modules/.vite/deps/radix-vue.js +0 -24321
- package/example/vue-demo/node_modules/.vite/deps/radix-vue.js.map +0 -7
- package/example/vue-demo/node_modules/.vite/deps/tailwind-merge.js +0 -2534
- package/example/vue-demo/node_modules/.vite/deps/tailwind-merge.js.map +0 -7
- package/example/vue-demo/node_modules/.vite/deps/vue.js +0 -348
- package/example/vue-demo/node_modules/.vite/deps/vue.js.map +0 -7
- package/example/vue-demo/package.json +0 -20
- package/example/vue-demo/postcss.config.mjs +0 -6
- package/example/vue-demo/src/App.vue +0 -61
- package/example/vue-demo/src/env.d.ts +0 -1
- package/example/vue-demo/src/main.ts +0 -5
- package/example/vue-demo/src/style.css +0 -102
- package/example/vue-demo/tailwind.config.mjs +0 -50
- package/example/vue-demo/tsconfig.json +0 -15
- package/example/vue-demo/vite.config.ts +0 -14
- package/packages/core/dist/parser.cjs +0 -1
- package/packages/core/dist/parser.d.cts +0 -2
- package/packages/core/dist/parser.d.ts +0 -2
- package/packages/core/dist/parser.js +0 -0
- package/packages/core/node_modules/.bin/api-extractor +0 -17
- package/packages/core/node_modules/.bin/esbuild +0 -14
- package/packages/core/node_modules/.bin/jiti +0 -17
- package/packages/core/node_modules/.bin/tsc +0 -17
- package/packages/core/node_modules/.bin/tsserver +0 -17
- package/packages/core/node_modules/.bin/tsup +0 -17
- package/packages/core/node_modules/.bin/tsup-node +0 -17
- package/packages/core/node_modules/.bin/vitest +0 -17
- package/packages/core/node_modules/.vite/vitest/results.json +0 -1
- package/packages/core/package.json +0 -37
- package/packages/core/src/index.ts +0 -2
- package/packages/core/src/parser.ts +0 -0
- package/packages/core/src/types.ts +0 -42
- package/packages/core/src/utils.ts +0 -80
- package/packages/core/test/utils.test.ts +0 -99
- package/packages/core/tsconfig.json +0 -15
- package/packages/core/tsup.config.ts +0 -9
- package/packages/react/node_modules/.bin/api-extractor +0 -17
- package/packages/react/node_modules/.bin/browserslist +0 -17
- package/packages/react/node_modules/.bin/esbuild +0 -14
- package/packages/react/node_modules/.bin/jiti +0 -17
- package/packages/react/node_modules/.bin/tsc +0 -17
- package/packages/react/node_modules/.bin/tsserver +0 -17
- package/packages/react/node_modules/.bin/tsup +0 -17
- package/packages/react/node_modules/.bin/tsup-node +0 -17
- package/packages/react/node_modules/.bin/vite +0 -17
- package/packages/react/node_modules/.bin/vitest +0 -17
- package/packages/react/node_modules/.vite/vitest/results.json +0 -1
- package/packages/react/package.json +0 -57
- package/packages/react/postcss.config.mjs +0 -6
- package/packages/react/src/DynamicForm.tsx +0 -93
- package/packages/react/src/FormFieldRenderer.tsx +0 -50
- package/packages/react/src/components/Checkbox.tsx +0 -28
- package/packages/react/src/components/CheckboxRenderer.tsx +0 -37
- package/packages/react/src/components/Input.tsx +0 -24
- package/packages/react/src/components/InputRenderer.tsx +0 -29
- package/packages/react/src/components/Label.tsx +0 -24
- package/packages/react/src/components/RadioGroup.tsx +0 -42
- package/packages/react/src/components/RadioRenderer.tsx +0 -29
- package/packages/react/src/components/Select.tsx +0 -93
- package/packages/react/src/components/SelectRenderer.tsx +0 -27
- package/packages/react/src/components/Textarea.tsx +0 -23
- package/packages/react/src/components/TextareaRenderer.tsx +0 -17
- package/packages/react/src/components/index.ts +0 -55
- package/packages/react/src/components/types.ts +0 -17
- package/packages/react/src/index.tsx +0 -3
- package/packages/react/src/utils.ts +0 -7
- package/packages/react/tailwind.config.mjs +0 -10
- package/packages/react/test/DynamicForm.test.tsx +0 -25
- package/packages/react/test/FormFieldRenderer.test.tsx +0 -127
- package/packages/react/tsconfig.json +0 -15
- package/packages/react/vitest.config.ts +0 -16
- package/packages/vue/node_modules/.bin/rollup +0 -17
- package/packages/vue/node_modules/.bin/tsc +0 -17
- package/packages/vue/node_modules/.bin/tsserver +0 -17
- package/packages/vue/node_modules/.bin/vite +0 -17
- package/packages/vue/node_modules/.bin/vitest +0 -17
- package/packages/vue/node_modules/.bin/vue-tsc +0 -17
- package/packages/vue/node_modules/.vite/vitest/results.json +0 -1
- package/packages/vue/node_modules/.vue-global-types/vue_3.5_0_0_0.d.ts +0 -118
- package/packages/vue/node_modules/.vue-global-types/vue_3.5_false.d.ts +0 -128
- package/packages/vue/package.json +0 -53
- package/packages/vue/postcss.config.mjs +0 -6
- package/packages/vue/src/DynamicForm.vue +0 -81
- package/packages/vue/src/FormFieldRenderer.vue +0 -70
- package/packages/vue/src/components/Checkbox.vue +0 -28
- package/packages/vue/src/components/CheckboxRenderer.vue +0 -35
- package/packages/vue/src/components/Input.vue +0 -21
- package/packages/vue/src/components/InputRenderer.vue +0 -28
- package/packages/vue/src/components/Label.vue +0 -21
- package/packages/vue/src/components/RadioGroup.vue +0 -30
- package/packages/vue/src/components/RadioGroupItem.vue +0 -26
- package/packages/vue/src/components/RadioRenderer.vue +0 -24
- package/packages/vue/src/components/Select.vue +0 -40
- package/packages/vue/src/components/SelectContent.vue +0 -38
- package/packages/vue/src/components/SelectItem.vue +0 -43
- package/packages/vue/src/components/SelectRenderer.vue +0 -30
- package/packages/vue/src/components/SelectTrigger.vue +0 -27
- package/packages/vue/src/components/Textarea.vue +0 -19
- package/packages/vue/src/components/TextareaRenderer.vue +0 -18
- package/packages/vue/src/components/index.ts +0 -24
- package/packages/vue/src/env.d.ts +0 -7
- package/packages/vue/src/fieldComponentMap.ts +0 -34
- package/packages/vue/src/index.ts +0 -4
- package/packages/vue/src/utils.ts +0 -6
- package/packages/vue/tailwind.config.mjs +0 -10
- package/packages/vue/test/DynamicForm.test.ts +0 -19
- package/packages/vue/test/FormFieldRenderer.test.ts +0 -133
- package/packages/vue/tsconfig.json +0 -16
- package/packages/vue/vite.config.ts +0 -29
- package/packages/vue/vitest.config.ts +0 -16
- package/pnpm-workspace.yaml +0 -4
- package/tsconfig.json +0 -21
- package/turbo.json +0 -25
package/package.json
CHANGED
|
@@ -9,11 +9,17 @@
|
|
|
9
9
|
"json-schema",
|
|
10
10
|
"form-builder"
|
|
11
11
|
],
|
|
12
|
+
"files": [
|
|
13
|
+
"README.md",
|
|
14
|
+
"packages/core/dist/**",
|
|
15
|
+
"packages/react/dist/**",
|
|
16
|
+
"packages/vue/dist/**"
|
|
17
|
+
],
|
|
12
18
|
"exports": {
|
|
13
19
|
"./core": {
|
|
14
20
|
"types": "./packages/core/dist/index.d.ts",
|
|
15
|
-
"import": "./packages/core/dist/index.
|
|
16
|
-
"require": "./packages/core/dist/index.
|
|
21
|
+
"import": "./packages/core/dist/index.js",
|
|
22
|
+
"require": "./packages/core/dist/index.cjs"
|
|
17
23
|
},
|
|
18
24
|
"./react": {
|
|
19
25
|
"types": "./packages/react/dist/index.d.ts",
|
|
@@ -51,7 +57,7 @@
|
|
|
51
57
|
"vitest": "^1.0.0",
|
|
52
58
|
"vue": "^3.5.0"
|
|
53
59
|
},
|
|
54
|
-
"version": "2.0
|
|
60
|
+
"version": "2.1.0",
|
|
55
61
|
"scripts": {
|
|
56
62
|
"build:all": "turbo run build",
|
|
57
63
|
"dev:all": "turbo run dev",
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getDefaultValues,
|
|
3
|
+
normalizeFieldValue,
|
|
4
|
+
validateFieldByName,
|
|
5
|
+
validateForm
|
|
6
|
+
} from "./chunk-WEDHXOHH.js";
|
|
7
|
+
|
|
8
|
+
// src/formState.ts
|
|
9
|
+
function createFormRuntimeState(fields) {
|
|
10
|
+
return {
|
|
11
|
+
values: getDefaultValues(fields),
|
|
12
|
+
errors: {},
|
|
13
|
+
isSubmitting: false
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function setSubmitting(state, isSubmitting) {
|
|
17
|
+
return {
|
|
18
|
+
...state,
|
|
19
|
+
isSubmitting
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function applyFieldChange(fields, state, name, rawValue) {
|
|
23
|
+
const field = fields.find((f) => f.name === name);
|
|
24
|
+
const normalizedValue = field ? normalizeFieldValue(field, rawValue) : rawValue;
|
|
25
|
+
const values = {
|
|
26
|
+
...state.values,
|
|
27
|
+
[name]: normalizedValue
|
|
28
|
+
};
|
|
29
|
+
const error = validateFieldByName(fields, name, normalizedValue);
|
|
30
|
+
const errors = {
|
|
31
|
+
...state.errors,
|
|
32
|
+
[name]: error || ""
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
...state,
|
|
36
|
+
values,
|
|
37
|
+
errors
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function applyFieldBlur(fields, state, name) {
|
|
41
|
+
const error = validateFieldByName(fields, name, state.values[name]);
|
|
42
|
+
return {
|
|
43
|
+
...state,
|
|
44
|
+
errors: {
|
|
45
|
+
...state.errors,
|
|
46
|
+
[name]: error || ""
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function runSubmitValidation(fields, state) {
|
|
51
|
+
const errors = validateForm(fields, state.values);
|
|
52
|
+
const hasError = Object.keys(errors).length > 0;
|
|
53
|
+
return {
|
|
54
|
+
state: {
|
|
55
|
+
...state,
|
|
56
|
+
errors,
|
|
57
|
+
isSubmitting: false
|
|
58
|
+
},
|
|
59
|
+
hasError
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
createFormRuntimeState,
|
|
65
|
+
setSubmitting,
|
|
66
|
+
applyFieldChange,
|
|
67
|
+
applyFieldBlur,
|
|
68
|
+
runSubmitValidation
|
|
69
|
+
};
|
|
@@ -5,6 +5,12 @@ function parseNumberish(value) {
|
|
|
5
5
|
const parsed = Number(value);
|
|
6
6
|
return Number.isNaN(parsed) ? null : parsed;
|
|
7
7
|
}
|
|
8
|
+
function normalizeFieldValue(field, value) {
|
|
9
|
+
if (field.type !== "number") return value;
|
|
10
|
+
if (value === "" || value === void 0 || value === null) return "";
|
|
11
|
+
const numericValue = parseNumberish(value);
|
|
12
|
+
return numericValue === null ? value : numericValue;
|
|
13
|
+
}
|
|
8
14
|
function validateField(value, field) {
|
|
9
15
|
if (!field.validations) return null;
|
|
10
16
|
for (const rule of field.validations) {
|
|
@@ -67,6 +73,19 @@ function validateField(value, field) {
|
|
|
67
73
|
}
|
|
68
74
|
return null;
|
|
69
75
|
}
|
|
76
|
+
function validateFieldByName(fields, name, value) {
|
|
77
|
+
const field = fields.find((f) => f.name === name);
|
|
78
|
+
if (!field) return null;
|
|
79
|
+
return validateField(value, field);
|
|
80
|
+
}
|
|
81
|
+
function validateForm(fields, values) {
|
|
82
|
+
const errors = {};
|
|
83
|
+
for (const field of fields) {
|
|
84
|
+
const error = validateField(values[field.name], field);
|
|
85
|
+
if (error) errors[field.name] = error;
|
|
86
|
+
}
|
|
87
|
+
return errors;
|
|
88
|
+
}
|
|
70
89
|
function getDefaultValues(fields) {
|
|
71
90
|
return fields.reduce((acc, field) => {
|
|
72
91
|
acc[field.name] = field.defaultValue !== void 0 ? field.defaultValue : field.type === "checkbox" ? [] : "";
|
|
@@ -75,6 +94,9 @@ function getDefaultValues(fields) {
|
|
|
75
94
|
}
|
|
76
95
|
|
|
77
96
|
export {
|
|
97
|
+
normalizeFieldValue,
|
|
78
98
|
validateField,
|
|
99
|
+
validateFieldByName,
|
|
100
|
+
validateForm,
|
|
79
101
|
getDefaultValues
|
|
80
102
|
};
|
|
@@ -0,0 +1,187 @@
|
|
|
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/formState.ts
|
|
21
|
+
var formState_exports = {};
|
|
22
|
+
__export(formState_exports, {
|
|
23
|
+
applyFieldBlur: () => applyFieldBlur,
|
|
24
|
+
applyFieldChange: () => applyFieldChange,
|
|
25
|
+
createFormRuntimeState: () => createFormRuntimeState,
|
|
26
|
+
runSubmitValidation: () => runSubmitValidation,
|
|
27
|
+
setSubmitting: () => setSubmitting
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(formState_exports);
|
|
30
|
+
|
|
31
|
+
// src/utils.ts
|
|
32
|
+
function parseNumberish(value) {
|
|
33
|
+
if (typeof value === "number") return Number.isNaN(value) ? null : value;
|
|
34
|
+
if (typeof value !== "string" || value.trim() === "") return null;
|
|
35
|
+
const parsed = Number(value);
|
|
36
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
37
|
+
}
|
|
38
|
+
function normalizeFieldValue(field, value) {
|
|
39
|
+
if (field.type !== "number") return value;
|
|
40
|
+
if (value === "" || value === void 0 || value === null) return "";
|
|
41
|
+
const numericValue = parseNumberish(value);
|
|
42
|
+
return numericValue === null ? value : numericValue;
|
|
43
|
+
}
|
|
44
|
+
function validateField(value, field) {
|
|
45
|
+
if (!field.validations) return null;
|
|
46
|
+
for (const rule of field.validations) {
|
|
47
|
+
switch (rule.type) {
|
|
48
|
+
case "required":
|
|
49
|
+
if (value === void 0 || value === null || value === "" || Array.isArray(value) && value.length === 0) {
|
|
50
|
+
return rule.message || `${field.label} is required`;
|
|
51
|
+
}
|
|
52
|
+
break;
|
|
53
|
+
case "min":
|
|
54
|
+
if (field.type === "number") {
|
|
55
|
+
const numericValue = parseNumberish(value);
|
|
56
|
+
if (numericValue !== null && numericValue < rule.value) {
|
|
57
|
+
return rule.message || `${field.label} must be at least ${rule.value}`;
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
if (typeof value === "number" && value < rule.value) {
|
|
62
|
+
return rule.message || `${field.label} must be at least ${rule.value}`;
|
|
63
|
+
}
|
|
64
|
+
if (typeof value === "string" && value.length < rule.value) {
|
|
65
|
+
return rule.message || `${field.label} must be at least ${rule.value} characters`;
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
68
|
+
case "max":
|
|
69
|
+
if (field.type === "number") {
|
|
70
|
+
const numericValue = parseNumberish(value);
|
|
71
|
+
if (numericValue !== null && numericValue > rule.value) {
|
|
72
|
+
return rule.message || `${field.label} must be at most ${rule.value}`;
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
if (typeof value === "number" && value > rule.value) {
|
|
77
|
+
return rule.message || `${field.label} must be at most ${rule.value}`;
|
|
78
|
+
}
|
|
79
|
+
if (typeof value === "string" && value.length > rule.value) {
|
|
80
|
+
return rule.message || `${field.label} must be at most ${rule.value} characters`;
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
case "email": {
|
|
84
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
85
|
+
if (value && !emailRegex.test(value)) {
|
|
86
|
+
return rule.message || "Invalid email address";
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case "pattern":
|
|
91
|
+
if (value && rule.value && !new RegExp(rule.value).test(value)) {
|
|
92
|
+
return rule.message || "Invalid format";
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
case "custom":
|
|
96
|
+
if (rule.validator) {
|
|
97
|
+
const result = rule.validator(value);
|
|
98
|
+
if (typeof result === "string") return result;
|
|
99
|
+
if (!result) return rule.message || "Invalid value";
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
function validateFieldByName(fields, name, value) {
|
|
107
|
+
const field = fields.find((f) => f.name === name);
|
|
108
|
+
if (!field) return null;
|
|
109
|
+
return validateField(value, field);
|
|
110
|
+
}
|
|
111
|
+
function validateForm(fields, values) {
|
|
112
|
+
const errors = {};
|
|
113
|
+
for (const field of fields) {
|
|
114
|
+
const error = validateField(values[field.name], field);
|
|
115
|
+
if (error) errors[field.name] = error;
|
|
116
|
+
}
|
|
117
|
+
return errors;
|
|
118
|
+
}
|
|
119
|
+
function getDefaultValues(fields) {
|
|
120
|
+
return fields.reduce((acc, field) => {
|
|
121
|
+
acc[field.name] = field.defaultValue !== void 0 ? field.defaultValue : field.type === "checkbox" ? [] : "";
|
|
122
|
+
return acc;
|
|
123
|
+
}, {});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/formState.ts
|
|
127
|
+
function createFormRuntimeState(fields) {
|
|
128
|
+
return {
|
|
129
|
+
values: getDefaultValues(fields),
|
|
130
|
+
errors: {},
|
|
131
|
+
isSubmitting: false
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function setSubmitting(state, isSubmitting) {
|
|
135
|
+
return {
|
|
136
|
+
...state,
|
|
137
|
+
isSubmitting
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function applyFieldChange(fields, state, name, rawValue) {
|
|
141
|
+
const field = fields.find((f) => f.name === name);
|
|
142
|
+
const normalizedValue = field ? normalizeFieldValue(field, rawValue) : rawValue;
|
|
143
|
+
const values = {
|
|
144
|
+
...state.values,
|
|
145
|
+
[name]: normalizedValue
|
|
146
|
+
};
|
|
147
|
+
const error = validateFieldByName(fields, name, normalizedValue);
|
|
148
|
+
const errors = {
|
|
149
|
+
...state.errors,
|
|
150
|
+
[name]: error || ""
|
|
151
|
+
};
|
|
152
|
+
return {
|
|
153
|
+
...state,
|
|
154
|
+
values,
|
|
155
|
+
errors
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function applyFieldBlur(fields, state, name) {
|
|
159
|
+
const error = validateFieldByName(fields, name, state.values[name]);
|
|
160
|
+
return {
|
|
161
|
+
...state,
|
|
162
|
+
errors: {
|
|
163
|
+
...state.errors,
|
|
164
|
+
[name]: error || ""
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function runSubmitValidation(fields, state) {
|
|
169
|
+
const errors = validateForm(fields, state.values);
|
|
170
|
+
const hasError = Object.keys(errors).length > 0;
|
|
171
|
+
return {
|
|
172
|
+
state: {
|
|
173
|
+
...state,
|
|
174
|
+
errors,
|
|
175
|
+
isSubmitting: false
|
|
176
|
+
},
|
|
177
|
+
hasError
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
181
|
+
0 && (module.exports = {
|
|
182
|
+
applyFieldBlur,
|
|
183
|
+
applyFieldChange,
|
|
184
|
+
createFormRuntimeState,
|
|
185
|
+
runSubmitValidation,
|
|
186
|
+
setSubmitting
|
|
187
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FormField } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
interface FormRuntimeState {
|
|
4
|
+
values: Record<string, any>;
|
|
5
|
+
errors: Record<string, string>;
|
|
6
|
+
isSubmitting: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare function createFormRuntimeState(fields: FormField[]): FormRuntimeState;
|
|
9
|
+
declare function setSubmitting(state: FormRuntimeState, isSubmitting: boolean): FormRuntimeState;
|
|
10
|
+
declare function applyFieldChange(fields: FormField[], state: FormRuntimeState, name: string, rawValue: unknown): FormRuntimeState;
|
|
11
|
+
declare function applyFieldBlur(fields: FormField[], state: FormRuntimeState, name: string): FormRuntimeState;
|
|
12
|
+
declare function runSubmitValidation(fields: FormField[], state: FormRuntimeState): {
|
|
13
|
+
state: FormRuntimeState;
|
|
14
|
+
hasError: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { type FormRuntimeState, applyFieldBlur, applyFieldChange, createFormRuntimeState, runSubmitValidation, setSubmitting };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FormField } from './types.js';
|
|
2
|
+
|
|
3
|
+
interface FormRuntimeState {
|
|
4
|
+
values: Record<string, any>;
|
|
5
|
+
errors: Record<string, string>;
|
|
6
|
+
isSubmitting: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare function createFormRuntimeState(fields: FormField[]): FormRuntimeState;
|
|
9
|
+
declare function setSubmitting(state: FormRuntimeState, isSubmitting: boolean): FormRuntimeState;
|
|
10
|
+
declare function applyFieldChange(fields: FormField[], state: FormRuntimeState, name: string, rawValue: unknown): FormRuntimeState;
|
|
11
|
+
declare function applyFieldBlur(fields: FormField[], state: FormRuntimeState, name: string): FormRuntimeState;
|
|
12
|
+
declare function runSubmitValidation(fields: FormField[], state: FormRuntimeState): {
|
|
13
|
+
state: FormRuntimeState;
|
|
14
|
+
hasError: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { type FormRuntimeState, applyFieldBlur, applyFieldChange, createFormRuntimeState, runSubmitValidation, setSubmitting };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyFieldBlur,
|
|
3
|
+
applyFieldChange,
|
|
4
|
+
createFormRuntimeState,
|
|
5
|
+
runSubmitValidation,
|
|
6
|
+
setSubmitting
|
|
7
|
+
} from "./chunk-TP3IHKWV.js";
|
|
8
|
+
import "./chunk-WEDHXOHH.js";
|
|
9
|
+
export {
|
|
10
|
+
applyFieldBlur,
|
|
11
|
+
applyFieldChange,
|
|
12
|
+
createFormRuntimeState,
|
|
13
|
+
runSubmitValidation,
|
|
14
|
+
setSubmitting
|
|
15
|
+
};
|
|
@@ -20,8 +20,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
applyFieldBlur: () => applyFieldBlur,
|
|
24
|
+
applyFieldChange: () => applyFieldChange,
|
|
25
|
+
createFormRuntimeState: () => createFormRuntimeState,
|
|
23
26
|
getDefaultValues: () => getDefaultValues,
|
|
24
|
-
|
|
27
|
+
normalizeFieldValue: () => normalizeFieldValue,
|
|
28
|
+
runSubmitValidation: () => runSubmitValidation,
|
|
29
|
+
setSubmitting: () => setSubmitting,
|
|
30
|
+
validateField: () => validateField,
|
|
31
|
+
validateFieldByName: () => validateFieldByName,
|
|
32
|
+
validateForm: () => validateForm
|
|
25
33
|
});
|
|
26
34
|
module.exports = __toCommonJS(index_exports);
|
|
27
35
|
|
|
@@ -32,6 +40,12 @@ function parseNumberish(value) {
|
|
|
32
40
|
const parsed = Number(value);
|
|
33
41
|
return Number.isNaN(parsed) ? null : parsed;
|
|
34
42
|
}
|
|
43
|
+
function normalizeFieldValue(field, value) {
|
|
44
|
+
if (field.type !== "number") return value;
|
|
45
|
+
if (value === "" || value === void 0 || value === null) return "";
|
|
46
|
+
const numericValue = parseNumberish(value);
|
|
47
|
+
return numericValue === null ? value : numericValue;
|
|
48
|
+
}
|
|
35
49
|
function validateField(value, field) {
|
|
36
50
|
if (!field.validations) return null;
|
|
37
51
|
for (const rule of field.validations) {
|
|
@@ -94,14 +108,90 @@ function validateField(value, field) {
|
|
|
94
108
|
}
|
|
95
109
|
return null;
|
|
96
110
|
}
|
|
111
|
+
function validateFieldByName(fields, name, value) {
|
|
112
|
+
const field = fields.find((f) => f.name === name);
|
|
113
|
+
if (!field) return null;
|
|
114
|
+
return validateField(value, field);
|
|
115
|
+
}
|
|
116
|
+
function validateForm(fields, values) {
|
|
117
|
+
const errors = {};
|
|
118
|
+
for (const field of fields) {
|
|
119
|
+
const error = validateField(values[field.name], field);
|
|
120
|
+
if (error) errors[field.name] = error;
|
|
121
|
+
}
|
|
122
|
+
return errors;
|
|
123
|
+
}
|
|
97
124
|
function getDefaultValues(fields) {
|
|
98
125
|
return fields.reduce((acc, field) => {
|
|
99
126
|
acc[field.name] = field.defaultValue !== void 0 ? field.defaultValue : field.type === "checkbox" ? [] : "";
|
|
100
127
|
return acc;
|
|
101
128
|
}, {});
|
|
102
129
|
}
|
|
130
|
+
|
|
131
|
+
// src/formState.ts
|
|
132
|
+
function createFormRuntimeState(fields) {
|
|
133
|
+
return {
|
|
134
|
+
values: getDefaultValues(fields),
|
|
135
|
+
errors: {},
|
|
136
|
+
isSubmitting: false
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function setSubmitting(state, isSubmitting) {
|
|
140
|
+
return {
|
|
141
|
+
...state,
|
|
142
|
+
isSubmitting
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function applyFieldChange(fields, state, name, rawValue) {
|
|
146
|
+
const field = fields.find((f) => f.name === name);
|
|
147
|
+
const normalizedValue = field ? normalizeFieldValue(field, rawValue) : rawValue;
|
|
148
|
+
const values = {
|
|
149
|
+
...state.values,
|
|
150
|
+
[name]: normalizedValue
|
|
151
|
+
};
|
|
152
|
+
const error = validateFieldByName(fields, name, normalizedValue);
|
|
153
|
+
const errors = {
|
|
154
|
+
...state.errors,
|
|
155
|
+
[name]: error || ""
|
|
156
|
+
};
|
|
157
|
+
return {
|
|
158
|
+
...state,
|
|
159
|
+
values,
|
|
160
|
+
errors
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function applyFieldBlur(fields, state, name) {
|
|
164
|
+
const error = validateFieldByName(fields, name, state.values[name]);
|
|
165
|
+
return {
|
|
166
|
+
...state,
|
|
167
|
+
errors: {
|
|
168
|
+
...state.errors,
|
|
169
|
+
[name]: error || ""
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function runSubmitValidation(fields, state) {
|
|
174
|
+
const errors = validateForm(fields, state.values);
|
|
175
|
+
const hasError = Object.keys(errors).length > 0;
|
|
176
|
+
return {
|
|
177
|
+
state: {
|
|
178
|
+
...state,
|
|
179
|
+
errors,
|
|
180
|
+
isSubmitting: false
|
|
181
|
+
},
|
|
182
|
+
hasError
|
|
183
|
+
};
|
|
184
|
+
}
|
|
103
185
|
// Annotate the CommonJS export names for ESM import in node:
|
|
104
186
|
0 && (module.exports = {
|
|
187
|
+
applyFieldBlur,
|
|
188
|
+
applyFieldChange,
|
|
189
|
+
createFormRuntimeState,
|
|
105
190
|
getDefaultValues,
|
|
106
|
-
|
|
191
|
+
normalizeFieldValue,
|
|
192
|
+
runSubmitValidation,
|
|
193
|
+
setSubmitting,
|
|
194
|
+
validateField,
|
|
195
|
+
validateFieldByName,
|
|
196
|
+
validateForm
|
|
107
197
|
});
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { FieldType, FormField, FormSchema, FormState, Option, ValidationRule } from './types.cjs';
|
|
2
|
-
export { getDefaultValues, validateField } from './utils.cjs';
|
|
2
|
+
export { getDefaultValues, normalizeFieldValue, validateField, validateFieldByName, validateForm } from './utils.cjs';
|
|
3
|
+
export { FormRuntimeState, applyFieldBlur, applyFieldChange, createFormRuntimeState, runSubmitValidation, setSubmitting } from './formState.cjs';
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { FieldType, FormField, FormSchema, FormState, Option, ValidationRule } from './types.js';
|
|
2
|
-
export { getDefaultValues, validateField } from './utils.js';
|
|
2
|
+
export { getDefaultValues, normalizeFieldValue, validateField, validateFieldByName, validateForm } from './utils.js';
|
|
3
|
+
export { FormRuntimeState, applyFieldBlur, applyFieldChange, createFormRuntimeState, runSubmitValidation, setSubmitting } from './formState.js';
|
|
@@ -1,8 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyFieldBlur,
|
|
3
|
+
applyFieldChange,
|
|
4
|
+
createFormRuntimeState,
|
|
5
|
+
runSubmitValidation,
|
|
6
|
+
setSubmitting
|
|
7
|
+
} from "./chunk-TP3IHKWV.js";
|
|
1
8
|
import {
|
|
2
9
|
getDefaultValues,
|
|
3
|
-
|
|
4
|
-
|
|
10
|
+
normalizeFieldValue,
|
|
11
|
+
validateField,
|
|
12
|
+
validateFieldByName,
|
|
13
|
+
validateForm
|
|
14
|
+
} from "./chunk-WEDHXOHH.js";
|
|
5
15
|
export {
|
|
16
|
+
applyFieldBlur,
|
|
17
|
+
applyFieldChange,
|
|
18
|
+
createFormRuntimeState,
|
|
6
19
|
getDefaultValues,
|
|
7
|
-
|
|
20
|
+
normalizeFieldValue,
|
|
21
|
+
runSubmitValidation,
|
|
22
|
+
setSubmitting,
|
|
23
|
+
validateField,
|
|
24
|
+
validateFieldByName,
|
|
25
|
+
validateForm
|
|
8
26
|
};
|
|
@@ -21,7 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var utils_exports = {};
|
|
22
22
|
__export(utils_exports, {
|
|
23
23
|
getDefaultValues: () => getDefaultValues,
|
|
24
|
-
|
|
24
|
+
normalizeFieldValue: () => normalizeFieldValue,
|
|
25
|
+
validateField: () => validateField,
|
|
26
|
+
validateFieldByName: () => validateFieldByName,
|
|
27
|
+
validateForm: () => validateForm
|
|
25
28
|
});
|
|
26
29
|
module.exports = __toCommonJS(utils_exports);
|
|
27
30
|
function parseNumberish(value) {
|
|
@@ -30,6 +33,12 @@ function parseNumberish(value) {
|
|
|
30
33
|
const parsed = Number(value);
|
|
31
34
|
return Number.isNaN(parsed) ? null : parsed;
|
|
32
35
|
}
|
|
36
|
+
function normalizeFieldValue(field, value) {
|
|
37
|
+
if (field.type !== "number") return value;
|
|
38
|
+
if (value === "" || value === void 0 || value === null) return "";
|
|
39
|
+
const numericValue = parseNumberish(value);
|
|
40
|
+
return numericValue === null ? value : numericValue;
|
|
41
|
+
}
|
|
33
42
|
function validateField(value, field) {
|
|
34
43
|
if (!field.validations) return null;
|
|
35
44
|
for (const rule of field.validations) {
|
|
@@ -92,6 +101,19 @@ function validateField(value, field) {
|
|
|
92
101
|
}
|
|
93
102
|
return null;
|
|
94
103
|
}
|
|
104
|
+
function validateFieldByName(fields, name, value) {
|
|
105
|
+
const field = fields.find((f) => f.name === name);
|
|
106
|
+
if (!field) return null;
|
|
107
|
+
return validateField(value, field);
|
|
108
|
+
}
|
|
109
|
+
function validateForm(fields, values) {
|
|
110
|
+
const errors = {};
|
|
111
|
+
for (const field of fields) {
|
|
112
|
+
const error = validateField(values[field.name], field);
|
|
113
|
+
if (error) errors[field.name] = error;
|
|
114
|
+
}
|
|
115
|
+
return errors;
|
|
116
|
+
}
|
|
95
117
|
function getDefaultValues(fields) {
|
|
96
118
|
return fields.reduce((acc, field) => {
|
|
97
119
|
acc[field.name] = field.defaultValue !== void 0 ? field.defaultValue : field.type === "checkbox" ? [] : "";
|
|
@@ -101,5 +123,8 @@ function getDefaultValues(fields) {
|
|
|
101
123
|
// Annotate the CommonJS export names for ESM import in node:
|
|
102
124
|
0 && (module.exports = {
|
|
103
125
|
getDefaultValues,
|
|
104
|
-
|
|
126
|
+
normalizeFieldValue,
|
|
127
|
+
validateField,
|
|
128
|
+
validateFieldByName,
|
|
129
|
+
validateForm
|
|
105
130
|
});
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { FormField } from './types.cjs';
|
|
2
2
|
|
|
3
|
+
declare function normalizeFieldValue(field: FormField, value: unknown): unknown;
|
|
3
4
|
declare function validateField(value: any, field: FormField): string | null;
|
|
5
|
+
declare function validateFieldByName(fields: FormField[], name: string, value: unknown): string | null;
|
|
6
|
+
declare function validateForm(fields: FormField[], values: Record<string, any>): Record<string, string>;
|
|
4
7
|
declare function getDefaultValues(fields: FormField[]): Record<string, any>;
|
|
5
8
|
|
|
6
|
-
export { getDefaultValues, validateField };
|
|
9
|
+
export { getDefaultValues, normalizeFieldValue, validateField, validateFieldByName, validateForm };
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { FormField } from './types.js';
|
|
2
2
|
|
|
3
|
+
declare function normalizeFieldValue(field: FormField, value: unknown): unknown;
|
|
3
4
|
declare function validateField(value: any, field: FormField): string | null;
|
|
5
|
+
declare function validateFieldByName(fields: FormField[], name: string, value: unknown): string | null;
|
|
6
|
+
declare function validateForm(fields: FormField[], values: Record<string, any>): Record<string, string>;
|
|
4
7
|
declare function getDefaultValues(fields: FormField[]): Record<string, any>;
|
|
5
8
|
|
|
6
|
-
export { getDefaultValues, validateField };
|
|
9
|
+
export { getDefaultValues, normalizeFieldValue, validateField, validateFieldByName, validateForm };
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getDefaultValues,
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
normalizeFieldValue,
|
|
4
|
+
validateField,
|
|
5
|
+
validateFieldByName,
|
|
6
|
+
validateForm
|
|
7
|
+
} from "./chunk-WEDHXOHH.js";
|
|
5
8
|
export {
|
|
6
9
|
getDefaultValues,
|
|
7
|
-
|
|
10
|
+
normalizeFieldValue,
|
|
11
|
+
validateField,
|
|
12
|
+
validateFieldByName,
|
|
13
|
+
validateForm
|
|
8
14
|
};
|