pdyform 2.1.0 → 2.2.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.
- package/README.md +98 -74
- package/package.json +1 -1
- package/packages/core/dist/chunk-6F4PWJZI.js +0 -0
- package/packages/core/dist/chunk-B7OMM2UC.js +160 -0
- package/packages/core/dist/chunk-J6ESJZ4U.js +82 -0
- package/packages/core/dist/formState.cjs +140 -78
- package/packages/core/dist/formState.d.cts +14 -10
- package/packages/core/dist/formState.d.ts +14 -10
- package/packages/core/dist/formState.js +4 -12
- package/packages/core/dist/index.cjs +146 -78
- package/packages/core/dist/index.d.cts +4 -3
- package/packages/core/dist/index.d.ts +4 -3
- package/packages/core/dist/index.js +11 -12
- package/packages/core/dist/types.d.cts +17 -5
- package/packages/core/dist/types.d.ts +17 -5
- package/packages/core/dist/types.js +1 -0
- package/packages/core/dist/utils.cjs +80 -19
- package/packages/core/dist/utils.d.cts +17 -5
- package/packages/core/dist/utils.d.ts +17 -5
- package/packages/core/dist/utils.js +7 -1
- package/packages/react/dist/index.cjs +1 -461
- package/packages/react/dist/index.d.cts +33 -2
- package/packages/react/dist/index.d.ts +33 -2
- package/packages/react/dist/index.js +1 -411
- package/packages/vue/dist/index.d.ts +55 -9
- package/packages/vue/dist/index.js +1 -1
- package/packages/vue/dist/index.mjs +600 -247
- package/packages/core/dist/chunk-TP3IHKWV.js +0 -69
- package/packages/core/dist/chunk-WEDHXOHH.js +0 -102
|
@@ -1,69 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
// src/utils.ts
|
|
2
|
-
function parseNumberish(value) {
|
|
3
|
-
if (typeof value === "number") return Number.isNaN(value) ? null : value;
|
|
4
|
-
if (typeof value !== "string" || value.trim() === "") return null;
|
|
5
|
-
const parsed = Number(value);
|
|
6
|
-
return Number.isNaN(parsed) ? null : parsed;
|
|
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
|
-
}
|
|
14
|
-
function validateField(value, field) {
|
|
15
|
-
if (!field.validations) return null;
|
|
16
|
-
for (const rule of field.validations) {
|
|
17
|
-
switch (rule.type) {
|
|
18
|
-
case "required":
|
|
19
|
-
if (value === void 0 || value === null || value === "" || Array.isArray(value) && value.length === 0) {
|
|
20
|
-
return rule.message || `${field.label} is required`;
|
|
21
|
-
}
|
|
22
|
-
break;
|
|
23
|
-
case "min":
|
|
24
|
-
if (field.type === "number") {
|
|
25
|
-
const numericValue = parseNumberish(value);
|
|
26
|
-
if (numericValue !== null && numericValue < rule.value) {
|
|
27
|
-
return rule.message || `${field.label} must be at least ${rule.value}`;
|
|
28
|
-
}
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
if (typeof value === "number" && value < rule.value) {
|
|
32
|
-
return rule.message || `${field.label} must be at least ${rule.value}`;
|
|
33
|
-
}
|
|
34
|
-
if (typeof value === "string" && value.length < rule.value) {
|
|
35
|
-
return rule.message || `${field.label} must be at least ${rule.value} characters`;
|
|
36
|
-
}
|
|
37
|
-
break;
|
|
38
|
-
case "max":
|
|
39
|
-
if (field.type === "number") {
|
|
40
|
-
const numericValue = parseNumberish(value);
|
|
41
|
-
if (numericValue !== null && numericValue > rule.value) {
|
|
42
|
-
return rule.message || `${field.label} must be at most ${rule.value}`;
|
|
43
|
-
}
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
if (typeof value === "number" && value > rule.value) {
|
|
47
|
-
return rule.message || `${field.label} must be at most ${rule.value}`;
|
|
48
|
-
}
|
|
49
|
-
if (typeof value === "string" && value.length > rule.value) {
|
|
50
|
-
return rule.message || `${field.label} must be at most ${rule.value} characters`;
|
|
51
|
-
}
|
|
52
|
-
break;
|
|
53
|
-
case "email": {
|
|
54
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
55
|
-
if (value && !emailRegex.test(value)) {
|
|
56
|
-
return rule.message || "Invalid email address";
|
|
57
|
-
}
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
case "pattern":
|
|
61
|
-
if (value && rule.value && !new RegExp(rule.value).test(value)) {
|
|
62
|
-
return rule.message || "Invalid format";
|
|
63
|
-
}
|
|
64
|
-
break;
|
|
65
|
-
case "custom":
|
|
66
|
-
if (rule.validator) {
|
|
67
|
-
const result = rule.validator(value);
|
|
68
|
-
if (typeof result === "string") return result;
|
|
69
|
-
if (!result) return rule.message || "Invalid value";
|
|
70
|
-
}
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return null;
|
|
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
|
-
}
|
|
89
|
-
function getDefaultValues(fields) {
|
|
90
|
-
return fields.reduce((acc, field) => {
|
|
91
|
-
acc[field.name] = field.defaultValue !== void 0 ? field.defaultValue : field.type === "checkbox" ? [] : "";
|
|
92
|
-
return acc;
|
|
93
|
-
}, {});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export {
|
|
97
|
-
normalizeFieldValue,
|
|
98
|
-
validateField,
|
|
99
|
-
validateFieldByName,
|
|
100
|
-
validateForm,
|
|
101
|
-
getDefaultValues
|
|
102
|
-
};
|