@keysdown/form-wrapper 0.0.13 → 1.0.2
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 +36 -9
- package/dist/form-wrapper.cjs +1 -1
- package/dist/form-wrapper.d.ts +3 -3
- package/dist/form-wrapper.iife.js +1 -1
- package/dist/form-wrapper.js +158 -187
- package/dist/form-wrapper.umd.cjs +1 -1
- package/dist/{core → src/core}/Form.d.ts +2 -2
- package/dist/{utils → src/utils}/validations.d.ts +1 -1
- package/package.json +19 -6
- /package/dist/{core → src/core}/Errors.d.ts +0 -0
- /package/dist/{core → src/core}/Messages.d.ts +0 -0
- /package/dist/{core → src/core}/Rules.d.ts +0 -0
- /package/dist/{core → src/core}/Validation.d.ts +0 -0
- /package/dist/{main.d.ts → src/main.d.ts} +0 -0
- /package/dist/{types → src/types}/collections.d.ts +0 -0
- /package/dist/{types → src/types}/fields.d.ts +0 -0
- /package/dist/{types → src/types}/messages.d.ts +0 -0
- /package/dist/{types → src/types}/rules.d.ts +0 -0
- /package/dist/{types → src/types}/validations.d.ts +0 -0
- /package/dist/{types → src/types}/values.d.ts +0 -0
- /package/dist/{utils → src/utils}/collections.d.ts +0 -0
- /package/dist/{utils → src/utils}/fields.d.ts +0 -0
- /package/dist/{utils → src/utils}/helpers.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<h1 align="center">
|
|
2
|
-
Form Wrapper
|
|
2
|
+
Form Wrapper
|
|
3
3
|
</h1>
|
|
4
4
|
|
|
5
5
|
<p align="center">
|
|
@@ -11,9 +11,6 @@
|
|
|
11
11
|
|
|
12
12
|
> A package that allows you to easily manage forms, with Form Wrapper it is possible to perform validations with error messages, in addition to managing the state of the forms.
|
|
13
13
|
|
|
14
|
-
> ![NOTE]
|
|
15
|
-
> This package is currently in Beta and is being tested in live applications, feel free to help in the development and maturation of the package.
|
|
16
|
-
|
|
17
14
|
## Installation
|
|
18
15
|
|
|
19
16
|
```shell
|
|
@@ -296,7 +293,7 @@ console.log(form.awaiting) // false
|
|
|
296
293
|
Method used to validate the entire form or a specific field.
|
|
297
294
|
|
|
298
295
|
```js
|
|
299
|
-
|
|
296
|
+
const form = createForm({
|
|
300
297
|
username: {
|
|
301
298
|
value: null,
|
|
302
299
|
rules: ['required'],
|
|
@@ -330,7 +327,7 @@ form.validate()
|
|
|
330
327
|
Method used to validate a specific field.
|
|
331
328
|
|
|
332
329
|
```js
|
|
333
|
-
|
|
330
|
+
const form = createForm({
|
|
334
331
|
username: {
|
|
335
332
|
value: null,
|
|
336
333
|
rules: ['required'],
|
|
@@ -364,7 +361,7 @@ form.validate('username')
|
|
|
364
361
|
Method used to validate the entire form.
|
|
365
362
|
|
|
366
363
|
```js
|
|
367
|
-
|
|
364
|
+
const form = createForm({
|
|
368
365
|
username: {
|
|
369
366
|
value: null,
|
|
370
367
|
rules: ['required'],
|
|
@@ -400,7 +397,7 @@ form.validate()
|
|
|
400
397
|
Method used to access all form values in json format.
|
|
401
398
|
|
|
402
399
|
```js
|
|
403
|
-
|
|
400
|
+
const form = createForm({
|
|
404
401
|
username: null
|
|
405
402
|
})
|
|
406
403
|
|
|
@@ -409,12 +406,27 @@ form.username = 'keysdown'
|
|
|
409
406
|
axios.post('some-api', form.values())
|
|
410
407
|
```
|
|
411
408
|
|
|
409
|
+
#### Filtering values
|
|
410
|
+
|
|
411
|
+
It is also possible to filter all accessed form values.
|
|
412
|
+
|
|
413
|
+
```js
|
|
414
|
+
const form = createForm({
|
|
415
|
+
first_name: null,
|
|
416
|
+
last_name: null
|
|
417
|
+
})
|
|
418
|
+
|
|
419
|
+
form.first_name = 'keysdown'
|
|
420
|
+
|
|
421
|
+
axios.post('some-api', form.values(['first_name']))
|
|
422
|
+
```
|
|
423
|
+
|
|
412
424
|
### valuesAsFormData()
|
|
413
425
|
|
|
414
426
|
Method used to access all form values as form data.
|
|
415
427
|
|
|
416
428
|
```js
|
|
417
|
-
|
|
429
|
+
const form = createForm({
|
|
418
430
|
username: null
|
|
419
431
|
})
|
|
420
432
|
|
|
@@ -427,6 +439,21 @@ axios.post('some-api', form.valuesAsFormData(), {
|
|
|
427
439
|
})
|
|
428
440
|
```
|
|
429
441
|
|
|
442
|
+
#### Filtering values
|
|
443
|
+
|
|
444
|
+
It is also possible to filter all accessed form values.
|
|
445
|
+
|
|
446
|
+
```js
|
|
447
|
+
const form = createForm({
|
|
448
|
+
first_name: null,
|
|
449
|
+
last_name: null
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
form.first_name = 'keysdown'
|
|
453
|
+
|
|
454
|
+
axios.post('some-api', form.valuesAsFormData(['first_name']))
|
|
455
|
+
```
|
|
456
|
+
|
|
430
457
|
## Validation
|
|
431
458
|
|
|
432
459
|
There is a property in the form object dedicated to validations, you can access everything related to validations through the `validation` property:
|
package/dist/form-wrapper.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var e=e=>typeof e==`string`?e.split(`|`):e,t=t=>({validation:{rules:t.validation?.rules?e(t.validation.rules):[],messages:t.validation?.messages??{}},value:t.value??null}),n=class{collection={};all(){return this.collection}first(e){let t=this.get(e);if(t){let e=Array.isArray(t)?t[0]:t;if(e)return String(e)}return null}any(){return Object.keys(this.collection).length>0}fill(e){return this.collection=e,this}push(e,t){return this.collection={...this.collection,[e]:t},this}has(e){return this.collection.hasOwnProperty(e)}get(e,t=null){return this.has(e)?this.collection[e]:t}unset(e){return this.has(e)&&delete this.collection[e],this}clear(){return this.collection={},this}},r=class extends n{},i=class extends n{},a=class extends n{push(e,t){let n=this.get(e)||[];return this.collection={...this.collection,[e]:[...n,t]},this}get(e){return super.get(e,[])}},o=class{errors=new a;messages=new r;rules=new i},s={required:e=>new Promise((t,n)=>{if(e==null)return n();String(e).replace(/\s/g,``).length>0?t(e):n()})},c=e=>typeof e==`object`&&!!e&&!Array.isArray(e),l=(e,t,n=null)=>{let r=t||new FormData;return Object.keys(e).forEach(t=>{let i=e[t];if(t=n?`${n}[${t}]`:t,!([void 0,null].indexOf(i)>-1)){if(c(i)&&!(i instanceof File)||Array.isArray(i)){l(i,r,t);return}r.append(t,i)}}),r},u=class{awaiting=!1;originalValues={};validation=new o;constructor(e){this.addFields(e)}addField(e,n){if(typeof n==`object`&&n&&Object.prototype.toString.call(n)===`[object Object]`&&`value`in n){let r=t(n);this[e]=r.value,this.originalValues[e]=r.value,this.validation.messages.push(e,r.validation.messages),this.validation.rules.push(e,r.validation.rules)}else this[e]=n,this.originalValues[e]=n;return this}addFields(e){return Object.keys(e).forEach(t=>{this.addField(t,e[t])}),this}get errors(){return this.validation.errors}fill(e,t=!1){return Object.keys(e).forEach(n=>{let r=e[n];t&&(this.originalValues[n]=r),this[n]=r}),this}get messages(){return this.validation.messages}removeField(e){return delete this[e],delete this.originalValues[e],this.validation.errors.unset(e),this.validation.messages.unset(e),this.validation.rules.unset(e),this}removeFields(e){return e.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(e=>{this[e]=this.originalValues[e]}),this}get rules(){return this.validation.rules}setAwaiting(e=!0){return this.awaiting=e,this}validate(e=null){return e?this.validateField(e):this.validateForm()}validateField(e){this.validation.errors.unset(e);let t=this.validation.rules.get(e);if(t&&t.length>0){let n=t.map(t=>{let n=t.split(`:`),r=n[0],i=n.length===2?n[1].split(`,`):[];return r in s?s[r](this[e],i).catch(t=>{let n=this.validation.messages.get(e);return n&&r in n&&this.validation.errors.push(e,n[r]),Promise.reject(t)}):Promise.reject(Error(`There is no validation rule called "${r}"`))});return Promise.all(n).then(()=>{})}return Promise.resolve()}validateForm(){let e=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(e).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(e){let t={};return Object.keys(this.originalValues).forEach(n=>{(!e||e.includes(n))&&(t[n]=this[n])}),t}valuesAsFormData(e){return l(this.values(e))}},d=e=>new u(e),f=u;exports.createForm=d,exports.default=f;
|
package/dist/form-wrapper.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var FormWrapper=function(
|
|
1
|
+
var FormWrapper=(function(e){Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var t=e=>typeof e==`string`?e.split(`|`):e,n=e=>({validation:{rules:e.validation?.rules?t(e.validation.rules):[],messages:e.validation?.messages??{}},value:e.value??null}),r=class{collection={};all(){return this.collection}first(e){let t=this.get(e);if(t){let e=Array.isArray(t)?t[0]:t;if(e)return String(e)}return null}any(){return Object.keys(this.collection).length>0}fill(e){return this.collection=e,this}push(e,t){return this.collection={...this.collection,[e]:t},this}has(e){return this.collection.hasOwnProperty(e)}get(e,t=null){return this.has(e)?this.collection[e]:t}unset(e){return this.has(e)&&delete this.collection[e],this}clear(){return this.collection={},this}},i=class extends r{},a=class extends r{},o=class extends r{push(e,t){let n=this.get(e)||[];return this.collection={...this.collection,[e]:[...n,t]},this}get(e){return super.get(e,[])}},s=class{errors=new o;messages=new i;rules=new a},c={required:e=>new Promise((t,n)=>{if(e==null)return n();String(e).replace(/\s/g,``).length>0?t(e):n()})},l=e=>typeof e==`object`&&!!e&&!Array.isArray(e),u=(e,t,n=null)=>{let r=t||new FormData;return Object.keys(e).forEach(t=>{let i=e[t];if(t=n?`${n}[${t}]`:t,!([void 0,null].indexOf(i)>-1)){if(l(i)&&!(i instanceof File)||Array.isArray(i)){u(i,r,t);return}r.append(t,i)}}),r},d=class{awaiting=!1;originalValues={};validation=new s;constructor(e){this.addFields(e)}addField(e,t){if(typeof t==`object`&&t&&Object.prototype.toString.call(t)===`[object Object]`&&`value`in t){let r=n(t);this[e]=r.value,this.originalValues[e]=r.value,this.validation.messages.push(e,r.validation.messages),this.validation.rules.push(e,r.validation.rules)}else this[e]=t,this.originalValues[e]=t;return this}addFields(e){return Object.keys(e).forEach(t=>{this.addField(t,e[t])}),this}get errors(){return this.validation.errors}fill(e,t=!1){return Object.keys(e).forEach(n=>{let r=e[n];t&&(this.originalValues[n]=r),this[n]=r}),this}get messages(){return this.validation.messages}removeField(e){return delete this[e],delete this.originalValues[e],this.validation.errors.unset(e),this.validation.messages.unset(e),this.validation.rules.unset(e),this}removeFields(e){return e.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(e=>{this[e]=this.originalValues[e]}),this}get rules(){return this.validation.rules}setAwaiting(e=!0){return this.awaiting=e,this}validate(e=null){return e?this.validateField(e):this.validateForm()}validateField(e){this.validation.errors.unset(e);let t=this.validation.rules.get(e);if(t&&t.length>0){let n=t.map(t=>{let n=t.split(`:`),r=n[0],i=n.length===2?n[1].split(`,`):[];return r in c?c[r](this[e],i).catch(t=>{let n=this.validation.messages.get(e);return n&&r in n&&this.validation.errors.push(e,n[r]),Promise.reject(t)}):Promise.reject(Error(`There is no validation rule called "${r}"`))});return Promise.all(n).then(()=>{})}return Promise.resolve()}validateForm(){let e=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(e).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(e){let t={};return Object.keys(this.originalValues).forEach(n=>{(!e||e.includes(n))&&(t[n]=this[n])}),t}valuesAsFormData(e){return u(this.values(e))}},f=e=>new d(e),p=d;return e.createForm=f,e.default=p,e})({});
|
package/dist/form-wrapper.js
CHANGED
|
@@ -1,187 +1,158 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}) : Promise.reject(new Error(`There is no validation rule called "${r}"`));
|
|
160
|
-
}
|
|
161
|
-
);
|
|
162
|
-
return Promise.all(i).then(() => {
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
return Promise.resolve();
|
|
166
|
-
}
|
|
167
|
-
validateForm() {
|
|
168
|
-
const t = Object.keys(this.originalValues).map(
|
|
169
|
-
(s) => this.validateField(s)
|
|
170
|
-
);
|
|
171
|
-
return Promise.all(t).then(() => Promise.resolve(this)).catch(() => Promise.reject(this));
|
|
172
|
-
}
|
|
173
|
-
values() {
|
|
174
|
-
const t = {};
|
|
175
|
-
return Object.keys(this.originalValues).forEach((s) => {
|
|
176
|
-
t[s] = this[s];
|
|
177
|
-
}), t;
|
|
178
|
-
}
|
|
179
|
-
valuesAsFormData() {
|
|
180
|
-
return u(this.values());
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
const A = (e) => new O(e);
|
|
184
|
-
export {
|
|
185
|
-
A as createForm,
|
|
186
|
-
O as default
|
|
187
|
-
};
|
|
1
|
+
//#region src/utils/fields.ts
|
|
2
|
+
var e = (e) => typeof e == "string" ? e.split("|") : e, t = (t) => ({
|
|
3
|
+
validation: {
|
|
4
|
+
rules: t.validation?.rules ? e(t.validation.rules) : [],
|
|
5
|
+
messages: t.validation?.messages ?? {}
|
|
6
|
+
},
|
|
7
|
+
value: t.value ?? null
|
|
8
|
+
}), n = class {
|
|
9
|
+
collection = {};
|
|
10
|
+
all() {
|
|
11
|
+
return this.collection;
|
|
12
|
+
}
|
|
13
|
+
first(e) {
|
|
14
|
+
let t = this.get(e);
|
|
15
|
+
if (t) {
|
|
16
|
+
let e = Array.isArray(t) ? t[0] : t;
|
|
17
|
+
if (e) return String(e);
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
any() {
|
|
22
|
+
return Object.keys(this.collection).length > 0;
|
|
23
|
+
}
|
|
24
|
+
fill(e) {
|
|
25
|
+
return this.collection = e, this;
|
|
26
|
+
}
|
|
27
|
+
push(e, t) {
|
|
28
|
+
return this.collection = {
|
|
29
|
+
...this.collection,
|
|
30
|
+
[e]: t
|
|
31
|
+
}, this;
|
|
32
|
+
}
|
|
33
|
+
has(e) {
|
|
34
|
+
return this.collection.hasOwnProperty(e);
|
|
35
|
+
}
|
|
36
|
+
get(e, t = null) {
|
|
37
|
+
return this.has(e) ? this.collection[e] : t;
|
|
38
|
+
}
|
|
39
|
+
unset(e) {
|
|
40
|
+
return this.has(e) && delete this.collection[e], this;
|
|
41
|
+
}
|
|
42
|
+
clear() {
|
|
43
|
+
return this.collection = {}, this;
|
|
44
|
+
}
|
|
45
|
+
}, r = class extends n {}, i = class extends n {}, a = class extends n {
|
|
46
|
+
push(e, t) {
|
|
47
|
+
let n = this.get(e) || [];
|
|
48
|
+
return this.collection = {
|
|
49
|
+
...this.collection,
|
|
50
|
+
[e]: [...n, t]
|
|
51
|
+
}, this;
|
|
52
|
+
}
|
|
53
|
+
get(e) {
|
|
54
|
+
return super.get(e, []);
|
|
55
|
+
}
|
|
56
|
+
}, o = class {
|
|
57
|
+
errors = new a();
|
|
58
|
+
messages = new r();
|
|
59
|
+
rules = new i();
|
|
60
|
+
}, s = { required: (e) => new Promise((t, n) => {
|
|
61
|
+
if (e == null) return n();
|
|
62
|
+
String(e).replace(/\s/g, "").length > 0 ? t(e) : n();
|
|
63
|
+
}) }, c = (e) => typeof e == "object" && !!e && !Array.isArray(e), l = (e, t, n = null) => {
|
|
64
|
+
let r = t || new FormData();
|
|
65
|
+
return Object.keys(e).forEach((t) => {
|
|
66
|
+
let i = e[t];
|
|
67
|
+
if (t = n ? `${n}[${t}]` : t, !([void 0, null].indexOf(i) > -1)) {
|
|
68
|
+
if (c(i) && !(i instanceof File) || Array.isArray(i)) {
|
|
69
|
+
l(i, r, t);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
r.append(t, i);
|
|
73
|
+
}
|
|
74
|
+
}), r;
|
|
75
|
+
}, u = class {
|
|
76
|
+
awaiting = !1;
|
|
77
|
+
originalValues = {};
|
|
78
|
+
validation = new o();
|
|
79
|
+
constructor(e) {
|
|
80
|
+
this.addFields(e);
|
|
81
|
+
}
|
|
82
|
+
addField(e, n) {
|
|
83
|
+
if (typeof n == "object" && n && Object.prototype.toString.call(n) === "[object Object]" && "value" in n) {
|
|
84
|
+
let r = t(n);
|
|
85
|
+
this[e] = r.value, this.originalValues[e] = r.value, this.validation.messages.push(e, r.validation.messages), this.validation.rules.push(e, r.validation.rules);
|
|
86
|
+
} else this[e] = n, this.originalValues[e] = n;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
addFields(e) {
|
|
90
|
+
return Object.keys(e).forEach((t) => {
|
|
91
|
+
this.addField(t, e[t]);
|
|
92
|
+
}), this;
|
|
93
|
+
}
|
|
94
|
+
get errors() {
|
|
95
|
+
return this.validation.errors;
|
|
96
|
+
}
|
|
97
|
+
fill(e, t = !1) {
|
|
98
|
+
return Object.keys(e).forEach((n) => {
|
|
99
|
+
let r = e[n];
|
|
100
|
+
t && (this.originalValues[n] = r), this[n] = r;
|
|
101
|
+
}), this;
|
|
102
|
+
}
|
|
103
|
+
get messages() {
|
|
104
|
+
return this.validation.messages;
|
|
105
|
+
}
|
|
106
|
+
removeField(e) {
|
|
107
|
+
return delete this[e], delete this.originalValues[e], this.validation.errors.unset(e), this.validation.messages.unset(e), this.validation.rules.unset(e), this;
|
|
108
|
+
}
|
|
109
|
+
removeFields(e) {
|
|
110
|
+
return e.forEach((e) => {
|
|
111
|
+
this.removeField(e);
|
|
112
|
+
}), this;
|
|
113
|
+
}
|
|
114
|
+
reset() {
|
|
115
|
+
return this.validation.errors.clear(), Object.keys(this.originalValues).forEach((e) => {
|
|
116
|
+
this[e] = this.originalValues[e];
|
|
117
|
+
}), this;
|
|
118
|
+
}
|
|
119
|
+
get rules() {
|
|
120
|
+
return this.validation.rules;
|
|
121
|
+
}
|
|
122
|
+
setAwaiting(e = !0) {
|
|
123
|
+
return this.awaiting = e, this;
|
|
124
|
+
}
|
|
125
|
+
validate(e = null) {
|
|
126
|
+
return e ? this.validateField(e) : this.validateForm();
|
|
127
|
+
}
|
|
128
|
+
validateField(e) {
|
|
129
|
+
this.validation.errors.unset(e);
|
|
130
|
+
let t = this.validation.rules.get(e);
|
|
131
|
+
if (t && t.length > 0) {
|
|
132
|
+
let n = t.map((t) => {
|
|
133
|
+
let n = t.split(":"), r = n[0], i = n.length === 2 ? n[1].split(",") : [];
|
|
134
|
+
return r in s ? s[r](this[e], i).catch((t) => {
|
|
135
|
+
let n = this.validation.messages.get(e);
|
|
136
|
+
return n && r in n && this.validation.errors.push(e, n[r]), Promise.reject(t);
|
|
137
|
+
}) : Promise.reject(/* @__PURE__ */ Error(`There is no validation rule called "${r}"`));
|
|
138
|
+
});
|
|
139
|
+
return Promise.all(n).then(() => {});
|
|
140
|
+
}
|
|
141
|
+
return Promise.resolve();
|
|
142
|
+
}
|
|
143
|
+
validateForm() {
|
|
144
|
+
let e = Object.keys(this.originalValues).map((e) => this.validateField(e));
|
|
145
|
+
return Promise.all(e).then(() => Promise.resolve(this)).catch(() => Promise.reject(this));
|
|
146
|
+
}
|
|
147
|
+
values(e) {
|
|
148
|
+
let t = {};
|
|
149
|
+
return Object.keys(this.originalValues).forEach((n) => {
|
|
150
|
+
(!e || e.includes(n)) && (t[n] = this[n]);
|
|
151
|
+
}), t;
|
|
152
|
+
}
|
|
153
|
+
valuesAsFormData(e) {
|
|
154
|
+
return l(this.values(e));
|
|
155
|
+
}
|
|
156
|
+
}, d = (e) => new u(e), f = u;
|
|
157
|
+
//#endregion
|
|
158
|
+
export { d as createForm, f as default };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.FormWrapper={}))})(this,function(e){Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var t=e=>typeof e==`string`?e.split(`|`):e,n=e=>({validation:{rules:e.validation?.rules?t(e.validation.rules):[],messages:e.validation?.messages??{}},value:e.value??null}),r=class{collection={};all(){return this.collection}first(e){let t=this.get(e);if(t){let e=Array.isArray(t)?t[0]:t;if(e)return String(e)}return null}any(){return Object.keys(this.collection).length>0}fill(e){return this.collection=e,this}push(e,t){return this.collection={...this.collection,[e]:t},this}has(e){return this.collection.hasOwnProperty(e)}get(e,t=null){return this.has(e)?this.collection[e]:t}unset(e){return this.has(e)&&delete this.collection[e],this}clear(){return this.collection={},this}},i=class extends r{},a=class extends r{},o=class extends r{push(e,t){let n=this.get(e)||[];return this.collection={...this.collection,[e]:[...n,t]},this}get(e){return super.get(e,[])}},s=class{errors=new o;messages=new i;rules=new a},c={required:e=>new Promise((t,n)=>{if(e==null)return n();String(e).replace(/\s/g,``).length>0?t(e):n()})},l=e=>typeof e==`object`&&!!e&&!Array.isArray(e),u=(e,t,n=null)=>{let r=t||new FormData;return Object.keys(e).forEach(t=>{let i=e[t];if(t=n?`${n}[${t}]`:t,!([void 0,null].indexOf(i)>-1)){if(l(i)&&!(i instanceof File)||Array.isArray(i)){u(i,r,t);return}r.append(t,i)}}),r},d=class{awaiting=!1;originalValues={};validation=new s;constructor(e){this.addFields(e)}addField(e,t){if(typeof t==`object`&&t&&Object.prototype.toString.call(t)===`[object Object]`&&`value`in t){let r=n(t);this[e]=r.value,this.originalValues[e]=r.value,this.validation.messages.push(e,r.validation.messages),this.validation.rules.push(e,r.validation.rules)}else this[e]=t,this.originalValues[e]=t;return this}addFields(e){return Object.keys(e).forEach(t=>{this.addField(t,e[t])}),this}get errors(){return this.validation.errors}fill(e,t=!1){return Object.keys(e).forEach(n=>{let r=e[n];t&&(this.originalValues[n]=r),this[n]=r}),this}get messages(){return this.validation.messages}removeField(e){return delete this[e],delete this.originalValues[e],this.validation.errors.unset(e),this.validation.messages.unset(e),this.validation.rules.unset(e),this}removeFields(e){return e.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(e=>{this[e]=this.originalValues[e]}),this}get rules(){return this.validation.rules}setAwaiting(e=!0){return this.awaiting=e,this}validate(e=null){return e?this.validateField(e):this.validateForm()}validateField(e){this.validation.errors.unset(e);let t=this.validation.rules.get(e);if(t&&t.length>0){let n=t.map(t=>{let n=t.split(`:`),r=n[0],i=n.length===2?n[1].split(`,`):[];return r in c?c[r](this[e],i).catch(t=>{let n=this.validation.messages.get(e);return n&&r in n&&this.validation.errors.push(e,n[r]),Promise.reject(t)}):Promise.reject(Error(`There is no validation rule called "${r}"`))});return Promise.all(n).then(()=>{})}return Promise.resolve()}validateForm(){let e=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(e).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(e){let t={};return Object.keys(this.originalValues).forEach(n=>{(!e||e.includes(n))&&(t[n]=this[n])}),t}valuesAsFormData(e){return u(this.values(e))}},f=e=>new d(e),p=d;e.createForm=f,e.default=p});
|
|
@@ -22,6 +22,6 @@ export declare class Form {
|
|
|
22
22
|
validate(field?: string | null): Promise<this | void>;
|
|
23
23
|
validateField(field: string): Promise<void>;
|
|
24
24
|
validateForm(): Promise<this>;
|
|
25
|
-
values(): Values;
|
|
26
|
-
valuesAsFormData(): FormData;
|
|
25
|
+
values(only?: string[]): Values;
|
|
26
|
+
valuesAsFormData(only?: string[]): FormData;
|
|
27
27
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keysdown/form-wrapper",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/keysdown/form-wrapper.git"
|
|
7
|
+
},
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public",
|
|
10
|
+
"registry": "https://registry.npmjs.org"
|
|
11
|
+
},
|
|
4
12
|
"type": "module",
|
|
5
13
|
"files": [
|
|
6
14
|
"dist/*"
|
|
@@ -18,12 +26,17 @@
|
|
|
18
26
|
"scripts": {
|
|
19
27
|
"dev": "vite",
|
|
20
28
|
"build": "tsc && vite build",
|
|
21
|
-
"preview": "vite preview"
|
|
29
|
+
"preview": "vite preview",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"test:watch": "vitest",
|
|
32
|
+
"test:coverage": "vitest run --coverage"
|
|
22
33
|
},
|
|
23
34
|
"devDependencies": {
|
|
24
|
-
"@types/node": "^
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"vite
|
|
35
|
+
"@types/node": "^25.7.0",
|
|
36
|
+
"@vitest/coverage-v8": "^4.1.6",
|
|
37
|
+
"typescript": "^6.0.3",
|
|
38
|
+
"vite": "^8.0.12",
|
|
39
|
+
"vite-plugin-dts": "^5.0.0",
|
|
40
|
+
"vitest": "^4.1.6"
|
|
28
41
|
}
|
|
29
42
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|