@tachui/forms 0.7.0-alpha1 → 0.7.1-alpha
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 +321 -0
- package/dist/TextField-CGBM3x7K.js +1799 -0
- package/dist/components/index.js +25 -28
- package/dist/index.js +79 -81
- package/dist/state/index.js +240 -7
- package/dist/validation/index.js +598 -19
- package/package.json +5 -6
- package/dist/Form-ueYEcSg1.cjs +0 -2
- package/dist/Form-ueYEcSg1.cjs.map +0 -1
- package/dist/Form-ylAr3o_e.js +0 -376
- package/dist/Form-ylAr3o_e.js.map +0 -1
- package/dist/components/index.cjs +0 -2
- package/dist/components/index.cjs.map +0 -1
- package/dist/components/index.js.map +0 -1
- package/dist/forms-complex-BiQsZZlT.js +0 -361
- package/dist/forms-complex-BiQsZZlT.js.map +0 -1
- package/dist/forms-complex-DLEnXXJ5.cjs +0 -2
- package/dist/forms-complex-DLEnXXJ5.cjs.map +0 -1
- package/dist/forms-core-B1bx1drO.js +0 -839
- package/dist/forms-core-B1bx1drO.js.map +0 -1
- package/dist/forms-core-W_JGVLAI.cjs +0 -9
- package/dist/forms-core-W_JGVLAI.cjs.map +0 -1
- package/dist/forms-inputs-6QdeMWFk.js +0 -1075
- package/dist/forms-inputs-6QdeMWFk.js.map +0 -1
- package/dist/forms-inputs-DQ5QI_SU.cjs +0 -2
- package/dist/forms-inputs-DQ5QI_SU.cjs.map +0 -1
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/state/index.cjs +0 -2
- package/dist/state/index.cjs.map +0 -1
- package/dist/state/index.js.map +0 -1
- package/dist/validation/index.cjs +0 -2
- package/dist/validation/index.cjs.map +0 -1
- package/dist/validation/index.js.map +0 -1
package/dist/validation/index.js
CHANGED
|
@@ -1,21 +1,600 @@
|
|
|
1
|
-
|
|
1
|
+
var g = Object.defineProperty;
|
|
2
|
+
var v = (e, t, r) => t in e ? g(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
|
|
3
|
+
var m = (e, t, r) => v(e, typeof t != "symbol" ? t + "" : t, r);
|
|
4
|
+
class s extends Error {
|
|
5
|
+
constructor(t, r) {
|
|
6
|
+
super(t), this.context = r, this.name = "FormsValidationError";
|
|
7
|
+
}
|
|
8
|
+
getFormattedMessage() {
|
|
9
|
+
const { component: t, suggestion: r, example: a, documentation: o } = this.context;
|
|
10
|
+
let i = `❌ [@tachui/forms] ${t} Component Error: ${this.message}
|
|
11
|
+
`;
|
|
12
|
+
return r && (i += `
|
|
13
|
+
💡 Suggestion: ${r}
|
|
14
|
+
`), a && (i += `
|
|
15
|
+
❌ Wrong: ${a.wrong}`, i += `
|
|
16
|
+
✅ Correct: ${a.correct}
|
|
17
|
+
`), o && (i += `
|
|
18
|
+
📚 Documentation: ${o}`), i;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const l = {
|
|
22
|
+
// EmailField Component
|
|
23
|
+
validateEmailField(e) {
|
|
24
|
+
if (e.length === 0)
|
|
25
|
+
throw new s("EmailField component requires a props object with value", {
|
|
26
|
+
component: "EmailField",
|
|
27
|
+
suggestion: "Add value: EmailField({ value: emailSignal })",
|
|
28
|
+
documentation: "https://docs.tachui.dev/forms/components/emailfield",
|
|
29
|
+
example: {
|
|
30
|
+
wrong: "EmailField()",
|
|
31
|
+
correct: "EmailField({ value: emailSignal, onChange: handleChange })"
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const [t] = e;
|
|
35
|
+
if (!t || typeof t != "object")
|
|
36
|
+
throw new s("EmailField requires a props object", {
|
|
37
|
+
component: "EmailField",
|
|
38
|
+
suggestion: "Pass a props object with value property",
|
|
39
|
+
example: {
|
|
40
|
+
wrong: 'EmailField("email@example.com")',
|
|
41
|
+
correct: 'EmailField({ value: "email@example.com" })'
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
if (t.value === void 0)
|
|
45
|
+
throw new s("EmailField value property is required", {
|
|
46
|
+
component: "EmailField",
|
|
47
|
+
suggestion: "Provide a value for the email field",
|
|
48
|
+
example: {
|
|
49
|
+
wrong: 'EmailField({ placeholder: "Email" })',
|
|
50
|
+
correct: 'EmailField({ value: emailSignal, placeholder: "Email" })'
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
// PasswordField Component
|
|
55
|
+
validatePasswordField(e) {
|
|
56
|
+
if (e.length === 0)
|
|
57
|
+
throw new s("PasswordField component requires a props object with value", {
|
|
58
|
+
component: "PasswordField",
|
|
59
|
+
suggestion: "Add value: PasswordField({ value: passwordSignal })",
|
|
60
|
+
documentation: "https://docs.tachui.dev/forms/components/passwordfield",
|
|
61
|
+
example: {
|
|
62
|
+
wrong: "PasswordField()",
|
|
63
|
+
correct: "PasswordField({ value: passwordSignal, onChange: handleChange })"
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
const [t] = e;
|
|
67
|
+
if (!t || typeof t != "object")
|
|
68
|
+
throw new s("PasswordField requires a props object", {
|
|
69
|
+
component: "PasswordField",
|
|
70
|
+
suggestion: "Pass a props object with value property",
|
|
71
|
+
example: {
|
|
72
|
+
wrong: 'PasswordField("password")',
|
|
73
|
+
correct: "PasswordField({ value: passwordSignal })"
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
if (t.value === void 0)
|
|
77
|
+
throw new s("PasswordField value property is required", {
|
|
78
|
+
component: "PasswordField",
|
|
79
|
+
suggestion: "Provide a value for the password field",
|
|
80
|
+
example: {
|
|
81
|
+
wrong: 'PasswordField({ placeholder: "Password" })',
|
|
82
|
+
correct: 'PasswordField({ value: passwordSignal, placeholder: "Password" })'
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
// PhoneField Component
|
|
87
|
+
validatePhoneField(e) {
|
|
88
|
+
if (e.length === 0)
|
|
89
|
+
throw new s("PhoneField component requires a props object with value", {
|
|
90
|
+
component: "PhoneField",
|
|
91
|
+
suggestion: "Add value: PhoneField({ value: phoneSignal })",
|
|
92
|
+
documentation: "https://docs.tachui.dev/forms/components/phonefield",
|
|
93
|
+
example: {
|
|
94
|
+
wrong: "PhoneField()",
|
|
95
|
+
correct: 'PhoneField({ value: phoneSignal, format: "US" })'
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
const [t] = e;
|
|
99
|
+
if (!t || typeof t != "object")
|
|
100
|
+
throw new s("PhoneField requires a props object", {
|
|
101
|
+
component: "PhoneField",
|
|
102
|
+
suggestion: "Pass a props object with value property",
|
|
103
|
+
example: {
|
|
104
|
+
wrong: 'PhoneField("(555) 123-4567")',
|
|
105
|
+
correct: "PhoneField({ value: phoneSignal })"
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
if (t.value === void 0)
|
|
109
|
+
throw new s("PhoneField value property is required", {
|
|
110
|
+
component: "PhoneField",
|
|
111
|
+
suggestion: "Provide a value for the phone field",
|
|
112
|
+
example: {
|
|
113
|
+
wrong: 'PhoneField({ format: "US" })',
|
|
114
|
+
correct: 'PhoneField({ value: phoneSignal, format: "US" })'
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
// NumberField Component
|
|
119
|
+
validateNumberField(e) {
|
|
120
|
+
if (e.length === 0)
|
|
121
|
+
throw new s("NumberField component requires a props object with value", {
|
|
122
|
+
component: "NumberField",
|
|
123
|
+
suggestion: "Add value: NumberField({ value: numberSignal })",
|
|
124
|
+
documentation: "https://docs.tachui.dev/forms/components/numberfield",
|
|
125
|
+
example: {
|
|
126
|
+
wrong: "NumberField()",
|
|
127
|
+
correct: "NumberField({ value: numberSignal, min: 0, max: 100 })"
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
const [t] = e;
|
|
131
|
+
if (!t || typeof t != "object")
|
|
132
|
+
throw new s("NumberField requires a props object", {
|
|
133
|
+
component: "NumberField",
|
|
134
|
+
suggestion: "Pass a props object with value property",
|
|
135
|
+
example: {
|
|
136
|
+
wrong: "NumberField(42)",
|
|
137
|
+
correct: "NumberField({ value: 42 })"
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
if (t.value === void 0)
|
|
141
|
+
throw new s("NumberField value property is required", {
|
|
142
|
+
component: "NumberField",
|
|
143
|
+
suggestion: "Provide a numeric value for the field",
|
|
144
|
+
example: {
|
|
145
|
+
wrong: "NumberField({ min: 0, max: 100 })",
|
|
146
|
+
correct: "NumberField({ value: 42, min: 0, max: 100 })"
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
// CreditCardField Component
|
|
151
|
+
validateCreditCardField(e) {
|
|
152
|
+
if (e.length === 0)
|
|
153
|
+
throw new s("CreditCardField component requires a props object with value", {
|
|
154
|
+
component: "CreditCardField",
|
|
155
|
+
suggestion: "Add value: CreditCardField({ value: cardSignal })",
|
|
156
|
+
documentation: "https://docs.tachui.dev/forms/components/creditcardfield",
|
|
157
|
+
example: {
|
|
158
|
+
wrong: "CreditCardField()",
|
|
159
|
+
correct: "CreditCardField({ value: cardSignal, onChange: handleChange })"
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
const [t] = e;
|
|
163
|
+
if (!t || typeof t != "object")
|
|
164
|
+
throw new s("CreditCardField requires a props object", {
|
|
165
|
+
component: "CreditCardField",
|
|
166
|
+
suggestion: "Pass a props object with value property",
|
|
167
|
+
example: {
|
|
168
|
+
wrong: 'CreditCardField("4111111111111111")',
|
|
169
|
+
correct: "CreditCardField({ value: cardSignal })"
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
if (t.value === void 0)
|
|
173
|
+
throw new s("CreditCardField value property is required", {
|
|
174
|
+
component: "CreditCardField",
|
|
175
|
+
suggestion: "Provide a value for the credit card field",
|
|
176
|
+
example: {
|
|
177
|
+
wrong: 'CreditCardField({ placeholder: "Card Number" })',
|
|
178
|
+
correct: 'CreditCardField({ value: cardSignal, placeholder: "Card Number" })'
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
// Additional Forms components would go here...
|
|
183
|
+
// (SearchField, URLField, TextArea, ColorField, Select, MultiSelect, Checkbox, Radio, etc.)
|
|
184
|
+
};
|
|
185
|
+
function f() {
|
|
186
|
+
return [
|
|
187
|
+
{
|
|
188
|
+
packageName: "forms",
|
|
189
|
+
componentName: "EmailField",
|
|
190
|
+
validate: l.validateEmailField
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
packageName: "forms",
|
|
194
|
+
componentName: "PasswordField",
|
|
195
|
+
validate: l.validatePasswordField
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
packageName: "forms",
|
|
199
|
+
componentName: "PhoneField",
|
|
200
|
+
validate: l.validatePhoneField
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
packageName: "forms",
|
|
204
|
+
componentName: "NumberField",
|
|
205
|
+
validate: l.validateNumberField
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
packageName: "forms",
|
|
209
|
+
componentName: "CreditCardField",
|
|
210
|
+
validate: l.validateCreditCardField
|
|
211
|
+
}
|
|
212
|
+
// Additional validators would be added here for remaining 9+ Forms components
|
|
213
|
+
];
|
|
214
|
+
}
|
|
215
|
+
async function h() {
|
|
216
|
+
try {
|
|
217
|
+
const { registerComponentValidator: e } = await import("@tachui/core/validation"), t = f();
|
|
218
|
+
for (const r of t)
|
|
219
|
+
e(r);
|
|
220
|
+
process.env.NODE_ENV !== "production" && console.info(`🔍 [@tachui/forms] Registered ${t.length} component validators`);
|
|
221
|
+
} catch (e) {
|
|
222
|
+
process.env.NODE_ENV !== "production" && console.warn("⚠️ [@tachui/forms] Could not register validators with Core:", e);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
typeof window < "u" && process.env.NODE_ENV !== "production" && setTimeout(h, 10);
|
|
226
|
+
const u = {
|
|
227
|
+
required: (e) => ({
|
|
228
|
+
valid: e != null && e !== "",
|
|
229
|
+
message: "This field is required",
|
|
230
|
+
code: "REQUIRED"
|
|
231
|
+
}),
|
|
232
|
+
email: (e) => e ? {
|
|
233
|
+
valid: /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e),
|
|
234
|
+
message: "Please enter a valid email address",
|
|
235
|
+
code: "INVALID_EMAIL"
|
|
236
|
+
} : { valid: !0 },
|
|
237
|
+
url: (e) => {
|
|
238
|
+
if (!e) return { valid: !0 };
|
|
239
|
+
try {
|
|
240
|
+
return new URL(e), { valid: !0 };
|
|
241
|
+
} catch {
|
|
242
|
+
return {
|
|
243
|
+
valid: !1,
|
|
244
|
+
message: "Please enter a valid URL",
|
|
245
|
+
code: "INVALID_URL"
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
number: (e) => {
|
|
250
|
+
if (!e) return { valid: !0 };
|
|
251
|
+
const t = Number(e);
|
|
252
|
+
return {
|
|
253
|
+
valid: !Number.isNaN(t) && Number.isFinite(t),
|
|
254
|
+
message: "Please enter a valid number",
|
|
255
|
+
code: "INVALID_NUMBER"
|
|
256
|
+
};
|
|
257
|
+
},
|
|
258
|
+
integer: (e) => {
|
|
259
|
+
if (!e) return { valid: !0 };
|
|
260
|
+
const t = Number(e);
|
|
261
|
+
return {
|
|
262
|
+
valid: !Number.isNaN(t) && Number.isFinite(t) && Number.isInteger(t),
|
|
263
|
+
message: "Please enter a valid integer",
|
|
264
|
+
code: "INVALID_INTEGER"
|
|
265
|
+
};
|
|
266
|
+
},
|
|
267
|
+
min: (e, t) => {
|
|
268
|
+
if (!e) return { valid: !0 };
|
|
269
|
+
const r = Number(e);
|
|
270
|
+
return Number.isNaN(r) ? { valid: !0 } : {
|
|
271
|
+
valid: r >= t.min,
|
|
272
|
+
message: `Value must be at least ${t.min}`,
|
|
273
|
+
code: "MIN_VALUE"
|
|
274
|
+
};
|
|
275
|
+
},
|
|
276
|
+
max: (e, t) => {
|
|
277
|
+
if (!e) return { valid: !0 };
|
|
278
|
+
const r = Number(e);
|
|
279
|
+
return Number.isNaN(r) ? { valid: !0 } : {
|
|
280
|
+
valid: r <= t.max,
|
|
281
|
+
message: `Value must be at most ${t.max}`,
|
|
282
|
+
code: "MAX_VALUE"
|
|
283
|
+
};
|
|
284
|
+
},
|
|
285
|
+
minLength: (e, t) => e ? {
|
|
286
|
+
valid: e.length >= t.minLength,
|
|
287
|
+
message: `Must be at least ${t.minLength} characters`,
|
|
288
|
+
code: "MIN_LENGTH"
|
|
289
|
+
} : { valid: !0 },
|
|
290
|
+
maxLength: (e, t) => e ? {
|
|
291
|
+
valid: e.length <= t.maxLength,
|
|
292
|
+
message: `Must be at most ${t.maxLength} characters`,
|
|
293
|
+
code: "MAX_LENGTH"
|
|
294
|
+
} : { valid: !0 },
|
|
295
|
+
pattern: (e, t) => e ? {
|
|
296
|
+
valid: (typeof t.pattern == "string" ? new RegExp(t.pattern) : t.pattern).test(e),
|
|
297
|
+
message: t.message || "Value does not match required pattern",
|
|
298
|
+
code: "PATTERN_MISMATCH"
|
|
299
|
+
} : { valid: !0 },
|
|
300
|
+
// Additional validation rules expected by tests
|
|
301
|
+
numeric: (e) => {
|
|
302
|
+
if (!e) return { valid: !0 };
|
|
303
|
+
const t = Number(e);
|
|
304
|
+
return {
|
|
305
|
+
valid: !Number.isNaN(t) && Number.isFinite(t),
|
|
306
|
+
message: "Please enter a valid number",
|
|
307
|
+
code: "INVALID_NUMERIC"
|
|
308
|
+
};
|
|
309
|
+
},
|
|
310
|
+
phone: (e) => {
|
|
311
|
+
if (!e) return { valid: !0 };
|
|
312
|
+
const t = /^\+?[\d\s\-().]+$/, r = e.replace(/[\s\-().]/g, "");
|
|
313
|
+
return {
|
|
314
|
+
valid: t.test(e) && r.length >= 10,
|
|
315
|
+
message: "Please enter a valid phone number",
|
|
316
|
+
code: "INVALID_PHONE"
|
|
317
|
+
};
|
|
318
|
+
},
|
|
319
|
+
creditCard: (e) => {
|
|
320
|
+
if (!e) return { valid: !0 };
|
|
321
|
+
const t = e.replace(/\s/g, "");
|
|
322
|
+
let r = 0, a = !1;
|
|
323
|
+
for (let o = t.length - 1; o >= 0; o--) {
|
|
324
|
+
let i = parseInt(t.charAt(o));
|
|
325
|
+
a && (i *= 2, i > 9 && (i = i % 10 + 1)), r += i, a = !a;
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
valid: /^\d{13,19}$/.test(t) && r % 10 === 0,
|
|
329
|
+
message: "Please enter a valid credit card number",
|
|
330
|
+
code: "INVALID_CREDIT_CARD"
|
|
331
|
+
};
|
|
332
|
+
},
|
|
333
|
+
ssn: (e) => {
|
|
334
|
+
if (!e) return { valid: !0 };
|
|
335
|
+
const t = e.replace(/[-\s]/g, "");
|
|
336
|
+
return {
|
|
337
|
+
valid: /^\d{9}$/.test(t),
|
|
338
|
+
message: "Please enter a valid Social Security Number",
|
|
339
|
+
code: "INVALID_SSN"
|
|
340
|
+
};
|
|
341
|
+
},
|
|
342
|
+
postalCode: (e) => {
|
|
343
|
+
if (!e) return { valid: !0 };
|
|
344
|
+
const t = /^\d{5}(-\d{4})?$/, r = /^[A-Z0-9]{3,10}$/i;
|
|
345
|
+
return {
|
|
346
|
+
valid: t.test(e) || r.test(e),
|
|
347
|
+
message: "Please enter a valid postal code",
|
|
348
|
+
code: "INVALID_POSTAL_CODE"
|
|
349
|
+
};
|
|
350
|
+
},
|
|
351
|
+
zipCode: (e) => e ? {
|
|
352
|
+
valid: /^\d{5}(-\d{4})?$/.test(e),
|
|
353
|
+
message: "Please enter a valid ZIP code",
|
|
354
|
+
code: "INVALID_ZIP_CODE"
|
|
355
|
+
} : { valid: !0 },
|
|
356
|
+
date: (e) => {
|
|
357
|
+
if (!e) return { valid: !0 };
|
|
358
|
+
const t = new Date(e);
|
|
359
|
+
return {
|
|
360
|
+
valid: !Number.isNaN(t.getTime()) && !!e.match(/^\d{4}-\d{2}-\d{2}$/),
|
|
361
|
+
message: "Please enter a valid date (YYYY-MM-DD)",
|
|
362
|
+
code: "INVALID_DATE"
|
|
363
|
+
};
|
|
364
|
+
},
|
|
365
|
+
time: (e) => e ? {
|
|
366
|
+
valid: /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/.test(e),
|
|
367
|
+
message: "Please enter a valid time (HH:MM)",
|
|
368
|
+
code: "INVALID_TIME"
|
|
369
|
+
} : { valid: !0 },
|
|
370
|
+
strongPassword: (e) => {
|
|
371
|
+
if (!e) return { valid: !0 };
|
|
372
|
+
const t = /[A-Z]/.test(e), r = /[a-z]/.test(e), a = /\d/.test(e), o = /\W/.test(e), i = e.length >= 8, n = t && r && a && o && i;
|
|
373
|
+
return {
|
|
374
|
+
valid: n,
|
|
375
|
+
message: n ? void 0 : "Password must be at least 8 characters with uppercase, lowercase, number and special character",
|
|
376
|
+
code: "WEAK_PASSWORD"
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
}, c = /* @__PURE__ */ new Map();
|
|
380
|
+
function P(e) {
|
|
381
|
+
c.set(e.name, e);
|
|
382
|
+
}
|
|
383
|
+
function E(e) {
|
|
384
|
+
c.delete(e);
|
|
385
|
+
}
|
|
386
|
+
function C() {
|
|
387
|
+
return [...Object.keys(u), ...c.keys()];
|
|
388
|
+
}
|
|
389
|
+
function p(e, t, r) {
|
|
390
|
+
for (const a of t) {
|
|
391
|
+
let o;
|
|
392
|
+
if (typeof a == "string") {
|
|
393
|
+
const i = u[a];
|
|
394
|
+
if (i)
|
|
395
|
+
o = i(e, r?.[a]);
|
|
396
|
+
else {
|
|
397
|
+
const n = c.get(a);
|
|
398
|
+
if (n)
|
|
399
|
+
o = n.validate(e, n.options);
|
|
400
|
+
else {
|
|
401
|
+
console.warn(`Unknown validation rule: ${a}`);
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
} else if ("validate" in a)
|
|
406
|
+
o = a.validate(e, a.options);
|
|
407
|
+
else {
|
|
408
|
+
const i = u[a.name];
|
|
409
|
+
if (i)
|
|
410
|
+
o = i(e, a.options);
|
|
411
|
+
else {
|
|
412
|
+
console.warn(`Unknown built-in validation rule: ${a.name}`);
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (!o.valid)
|
|
417
|
+
return o;
|
|
418
|
+
}
|
|
419
|
+
return { valid: !0 };
|
|
420
|
+
}
|
|
421
|
+
function V(e, t) {
|
|
422
|
+
return !t?.rules || t.rules.length === 0 ? { valid: !0 } : p(e.value, t.rules);
|
|
423
|
+
}
|
|
424
|
+
async function w(e, t, r) {
|
|
425
|
+
for (const a of t) {
|
|
426
|
+
let o;
|
|
427
|
+
if (typeof a == "string") {
|
|
428
|
+
const n = u[a];
|
|
429
|
+
if (n)
|
|
430
|
+
o = n(e, r?.[a]);
|
|
431
|
+
else {
|
|
432
|
+
const d = c.get(a);
|
|
433
|
+
if (d)
|
|
434
|
+
o = d.validate(e, d.options);
|
|
435
|
+
else
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
} else if ("validate" in a)
|
|
439
|
+
o = a.validate(e, a.options);
|
|
440
|
+
else {
|
|
441
|
+
const n = u[a.name];
|
|
442
|
+
if (n)
|
|
443
|
+
o = n(e, a.options);
|
|
444
|
+
else
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
const i = await Promise.resolve(o);
|
|
448
|
+
if (!i.valid)
|
|
449
|
+
return i;
|
|
450
|
+
}
|
|
451
|
+
return { valid: !0 };
|
|
452
|
+
}
|
|
453
|
+
function I(e, t = 300) {
|
|
454
|
+
let r, a, o;
|
|
455
|
+
return (i) => new Promise((n) => {
|
|
456
|
+
if (i === a && o) {
|
|
457
|
+
Promise.resolve(o).then(n);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
a = i, clearTimeout(r), r = setTimeout(async () => {
|
|
461
|
+
try {
|
|
462
|
+
o = e(i);
|
|
463
|
+
const d = await Promise.resolve(o);
|
|
464
|
+
n(d);
|
|
465
|
+
} catch {
|
|
466
|
+
n({
|
|
467
|
+
valid: !1,
|
|
468
|
+
message: "Validation error occurred",
|
|
469
|
+
code: "VALIDATION_ERROR"
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
}, t);
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
const A = {
|
|
476
|
+
/**
|
|
477
|
+
* Validate that two fields match (e.g., password confirmation)
|
|
478
|
+
*/
|
|
479
|
+
fieldMatch: (e, t, r = "Fields must match") => (a) => a[e] === a[t] ? { valid: !0 } : {
|
|
480
|
+
valid: !1,
|
|
481
|
+
message: r,
|
|
482
|
+
code: "FIELD_MISMATCH"
|
|
483
|
+
},
|
|
484
|
+
/**
|
|
485
|
+
* Validate that at least one field in a group is filled
|
|
486
|
+
*/
|
|
487
|
+
requireOneOf: (e, t = "At least one field is required") => (r) => e.some((o) => {
|
|
488
|
+
const i = r[o];
|
|
489
|
+
return i != null && i !== "";
|
|
490
|
+
}) ? { valid: !0 } : {
|
|
491
|
+
valid: !1,
|
|
492
|
+
message: t,
|
|
493
|
+
code: "REQUIRE_ONE_OF"
|
|
494
|
+
},
|
|
495
|
+
/**
|
|
496
|
+
* Validate that a field is required when another field has a specific value
|
|
497
|
+
*/
|
|
498
|
+
requiredWhen: (e, t, r, a = `${e} is required`) => (o) => {
|
|
499
|
+
if (o[t] === r) {
|
|
500
|
+
const i = o[e];
|
|
501
|
+
return i != null && i !== "" ? { valid: !0 } : {
|
|
502
|
+
valid: !1,
|
|
503
|
+
message: a,
|
|
504
|
+
code: "REQUIRED_WHEN"
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
return { valid: !0 };
|
|
508
|
+
},
|
|
509
|
+
/**
|
|
510
|
+
* Validate date range (start date before end date)
|
|
511
|
+
*/
|
|
512
|
+
dateRange: (e, t, r = "End date must be after start date") => (a) => {
|
|
513
|
+
const o = a[e], i = a[t];
|
|
514
|
+
if (!o || !i) return { valid: !0 };
|
|
515
|
+
const n = new Date(o);
|
|
516
|
+
return new Date(i) >= n ? { valid: !0 } : {
|
|
517
|
+
valid: !1,
|
|
518
|
+
message: r,
|
|
519
|
+
code: "INVALID_DATE_RANGE"
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
}, x = {
|
|
523
|
+
email: ["required", "email"],
|
|
524
|
+
password: ["required", { name: "minLength", options: { minLength: 8 } }],
|
|
525
|
+
phone: [
|
|
526
|
+
"required",
|
|
527
|
+
{ name: "pattern", options: { pattern: /^\+?[\d\s-()]+$/, message: "Invalid phone number" } }
|
|
528
|
+
],
|
|
529
|
+
url: ["required", "url"],
|
|
530
|
+
positiveNumber: ["required", "number", { name: "min", options: { min: 0 } }],
|
|
531
|
+
percentage: [
|
|
532
|
+
"required",
|
|
533
|
+
"number",
|
|
534
|
+
{ name: "min", options: { min: 0 } },
|
|
535
|
+
{ name: "max", options: { max: 100 } }
|
|
536
|
+
]
|
|
537
|
+
};
|
|
538
|
+
class F {
|
|
539
|
+
constructor(t = {}) {
|
|
540
|
+
m(this, "messages", {});
|
|
541
|
+
this.messages = t;
|
|
542
|
+
}
|
|
543
|
+
setMessage(t, r) {
|
|
544
|
+
this.messages[t] = r;
|
|
545
|
+
}
|
|
546
|
+
setMessages(t) {
|
|
547
|
+
this.messages = { ...this.messages, ...t };
|
|
548
|
+
}
|
|
549
|
+
format(t, r) {
|
|
550
|
+
return t.code && this.messages[t.code] ? this.messages[t.code].replace("${field}", r || "field") : t.message || "Validation failed";
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
const R = new F(), L = {
|
|
554
|
+
/**
|
|
555
|
+
* Check if a validation result represents an error
|
|
556
|
+
*/
|
|
557
|
+
isError: (e) => !e.valid,
|
|
558
|
+
/**
|
|
559
|
+
* Check if a validation result is valid
|
|
560
|
+
*/
|
|
561
|
+
isValid: (e) => e.valid,
|
|
562
|
+
/**
|
|
563
|
+
* Combine multiple validation results
|
|
564
|
+
*/
|
|
565
|
+
combineResults: (e) => {
|
|
566
|
+
const t = e.filter((r) => !r.valid);
|
|
567
|
+
return t.length === 0 ? { valid: !0 } : {
|
|
568
|
+
valid: !1,
|
|
569
|
+
message: t.map((r) => r.message).join(", "),
|
|
570
|
+
code: "MULTIPLE_ERRORS"
|
|
571
|
+
};
|
|
572
|
+
},
|
|
573
|
+
/**
|
|
574
|
+
* Create a validation function from a rule configuration
|
|
575
|
+
*/
|
|
576
|
+
createValidator: (e, t) => (r) => p(r, e, t),
|
|
577
|
+
/**
|
|
578
|
+
* Create an async validation function
|
|
579
|
+
*/
|
|
580
|
+
createAsyncValidator: (e, t) => (r) => w(r, e, t)
|
|
581
|
+
};
|
|
2
582
|
export {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
583
|
+
A as CrossFieldValidators,
|
|
584
|
+
l as FormsComponentValidation,
|
|
585
|
+
s as FormsValidationError,
|
|
586
|
+
u as VALIDATION_RULES,
|
|
587
|
+
F as ValidationMessageFormatter,
|
|
588
|
+
x as ValidationPresets,
|
|
589
|
+
L as ValidationUtils,
|
|
590
|
+
I as createDebouncedValidator,
|
|
591
|
+
f as createFormsValidators,
|
|
592
|
+
R as defaultMessageFormatter,
|
|
593
|
+
C as getValidationRules,
|
|
594
|
+
h as registerFormsValidators,
|
|
595
|
+
P as registerValidationRule,
|
|
596
|
+
E as unregisterValidationRule,
|
|
597
|
+
V as validateField,
|
|
598
|
+
p as validateValue,
|
|
599
|
+
w as validateValueAsync
|
|
20
600
|
};
|
|
21
|
-
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tachui/forms",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1-alpha",
|
|
4
4
|
"description": "Form components and validation system for tachUI framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"files": [
|
|
9
|
-
"dist
|
|
9
|
+
"dist"
|
|
10
10
|
],
|
|
11
11
|
"keywords": [
|
|
12
12
|
"tachui",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"license": "MPL-2.0",
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
23
|
-
"url": "https://github.com/tachUI
|
|
23
|
+
"url": "https://github.com/tach-UI/tachUI",
|
|
24
24
|
"directory": "packages/forms"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
@@ -31,13 +31,12 @@
|
|
|
31
31
|
"typescript": "^5.2.0",
|
|
32
32
|
"vite": "^5.0.0",
|
|
33
33
|
"vitest": "^1.6.0",
|
|
34
|
-
"@tachui/core": "0.7.
|
|
34
|
+
"@tachui/core": "0.7.1-alpha"
|
|
35
35
|
},
|
|
36
36
|
"exports": {
|
|
37
37
|
".": {
|
|
38
38
|
"types": "./dist/index.d.ts",
|
|
39
|
-
"import": "./dist/index.js"
|
|
40
|
-
"require": "./dist/index.cjs"
|
|
39
|
+
"import": "./dist/index.js"
|
|
41
40
|
},
|
|
42
41
|
"./components": {
|
|
43
42
|
"types": "./dist/components/index.d.ts",
|
package/dist/Form-ueYEcSg1.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";const a=require("@tachui/core"),k=require("./forms-core-W_JGVLAI.cjs");function v(t,r){return Array.isArray(t)?t.flatMap(o=>C(o,r)):C(t,r)}function C(t,r){if(t==null)return[];if(typeof t=="object"&&"type"in t&&t.type==="component"){const o={...t,props:{...t.props,_formContext:r}},i=t.render.bind(o)();return Array.isArray(i)?i:[i]}return a.defaultChildrenRenderer(t)}const R=t=>{const{onSubmit:r,onChange:o,validation:n={validateOn:"blur",stopOnFirstError:!1,debounceMs:300},initialValues:i={},resetOnSubmit:l=!1,preserveValues:y=!1,children:g,...m}=t,s=k.createFormState(i),[S,u]=a.createSignal("idle"),h=async c=>{if(c&&c.preventDefault(),!!r)try{if(u("idle"),await s.validateForm()){const d=s.watch();await r(d,s.state),u("success"),l&&s.resetForm()}else u("error")}catch(f){u("error"),console.error("Form submission error:",f)}};a.createEffect(()=>{if(o){const c=s.state,f=Object.keys(c.fields).filter(d=>c.fields[d].dirty);if(f.length>0){const d=f[f.length-1],b=c.fields[d];o(d,b.value,b)}}});const x={register:s.register,unregister:s.unregister,setValue:s.setValue,getValue:s.getValue,getError:s.getError,validateField:s.validateField,validation:n,onChange:o};return{type:"component",id:m.id||"form",render:()=>a.h("form",{...m,onsubmit:h,novalidate:!0,"data-tachui-form":!0,"data-form-state":s.state.valid?"valid":"invalid","data-form-submitting":s.state.submitting,"data-submission-result":S()},...v(g,x)),props:t,cleanup:[()=>{y||s.resetForm()}]}},B=t=>{const{title:r,description:o,header:n,footer:i,style:l="automatic",spacing:y=12,collapsible:g=!1,collapsed:m=!1,onToggle:s,accessibilityLabel:S,accessibilityRole:u="group",children:h,...x}=t,[p,c]=a.createSignal(m),f=()=>{const e=!p();c(e),s&&s(e)},d=e=>e?typeof e=="string"?e:typeof e=="function"?e():e:null,b=()=>{const e={marginBottom:"20px",border:"none",padding:"0",margin:"0 0 20px 0"};switch(l){case"grouped":return{...e,backgroundColor:"#ffffff",border:"1px solid #e0e0e0",borderRadius:"12px",overflow:"hidden"};case"inset":return{...e,backgroundColor:"#f8f9fa",border:"1px solid #e9ecef",borderRadius:"8px",margin:"0 16px 20px 16px"};case"sidebar":return{...e,borderLeft:"3px solid #007AFF",paddingLeft:"16px",backgroundColor:"#f8f9fa",borderRadius:"0 8px 8px 0"};case"plain":return e;default:return{...e,backgroundColor:"#ffffff",border:"1px solid #f0f0f0",borderRadius:"8px"}}},w=()=>{const e={fontSize:"16px",fontWeight:"600",color:"#1a1a1a",margin:"0 0 12px 0"};switch(l){case"grouped":case"inset":return{...e,padding:"12px 16px 0 16px",backgroundColor:"#f8f9fa",borderBottom:"1px solid #e9ecef"};case"sidebar":return{...e,fontSize:"14px",textTransform:"uppercase",letterSpacing:"0.5px",color:"#666",marginBottom:"8px"};default:return{...e,padding:"0 0 8px 0"}}},A=()=>{const e={display:"flex",flexDirection:"column",gap:`${y}px`};switch(l){case"grouped":case"inset":return{...e,padding:"16px"};case"sidebar":return{...e,padding:"8px 0"};default:return{...e,padding:"12px"}}},E=()=>{const e={fontSize:"14px",color:"#666",margin:"8px 0 0 0"};switch(l){case"grouped":case"inset":return{...e,padding:"0 16px 12px 16px",backgroundColor:"#f8f9fa",borderTop:"1px solid #e9ecef"};default:return{...e,padding:"0 0 4px 0"}}},O=()=>{const e=r||d(n);if(!e)return[];const F=w();if(g){const I=p()?"▶":"▼";return[a.h("legend",{style:{...F,cursor:"pointer",display:"flex",alignItems:"center",gap:"8px"},onclick:f},a.h("span",{style:{fontSize:"12px",color:"#666"}},a.text(I)),...typeof e=="string"?[a.text(e)]:[e.render()].flat())]}return[a.h("legend",{style:F},...typeof e=="string"?[a.text(e)]:[e.render()].flat())]},V=()=>!o||p()?[]:[a.h("div",{style:{fontSize:"14px",color:"#666",marginBottom:"12px",padding:l==="grouped"||l==="inset"?"0 16px":"0"}},a.text(o))],z=()=>g&&p()?[]:[a.h("div",{style:A()},...v(h,t._formContext))],D=()=>{const e=d(i);return!e||p()?[]:[a.h("div",{style:E()},...typeof e=="string"?[a.text(e)]:[e.render()].flat())]};return{type:"component",id:x.id||"form-section",render:()=>a.h("fieldset",{...x,style:b(),"aria-label":S,role:u,"data-tachui-form-section":!0,"data-collapsible":g,"data-collapsed":p(),"data-style":l},...O(),...V(),...z(),...D()),props:t}},L={createAutoForm:t=>(console.log("Auto form creation:",t),R),validation:{createSchema:t=>({fields:t,validate:async r=>({valid:!0,errors:{}})})},serialize:{toFormData:t=>{const r=new FormData;return Object.entries(t).forEach(([o,n])=>{n!=null&&(n instanceof File||n instanceof Blob?r.append(o,n):Array.isArray(n)?n.forEach((i,l)=>{r.append(`${o}[${l}]`,String(i))}):r.append(o,String(n)))}),r},toURLSearchParams:t=>{const r=new URLSearchParams;return Object.entries(t).forEach(([o,n])=>{n!=null&&(Array.isArray(n)?n.forEach(i=>r.append(o,String(i))):r.append(o,String(n)))}),r},toJSON:t=>JSON.stringify(t,null,2)}};exports.Form=R;exports.FormSection=B;exports.FormUtils=L;
|
|
2
|
-
//# sourceMappingURL=Form-ueYEcSg1.cjs.map
|