@keysdown/form-wrapper 0.0.14 → 1.0.5
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 +44 -4
- 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 +164 -187
- package/dist/form-wrapper.umd.cjs +1 -1
- package/dist/{core → src/core}/Form.d.ts +2 -0
- package/dist/{utils → src/utils}/collections.d.ts +1 -1
- 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}/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,8 +11,9 @@
|
|
|
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
|
-
>
|
|
15
|
-
>
|
|
14
|
+
<p align="center">
|
|
15
|
+
<strong>Bundle size (minified + gzip):</strong> ~1.4 kB
|
|
16
|
+
</p>
|
|
16
17
|
|
|
17
18
|
## Installation
|
|
18
19
|
|
|
@@ -271,6 +272,44 @@ form.reset()
|
|
|
271
272
|
console.log(form.values()) // {username: null}
|
|
272
273
|
```
|
|
273
274
|
|
|
275
|
+
### wasChanged(field)
|
|
276
|
+
|
|
277
|
+
Method used to check if one or more fields have been changed from their original values. When an array is provided, returns `true` if **any** of the fields were changed.
|
|
278
|
+
|
|
279
|
+
```js
|
|
280
|
+
const form = createForm({
|
|
281
|
+
name: null,
|
|
282
|
+
username: null
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
console.log(form.wasChanged('username')) // false
|
|
286
|
+
|
|
287
|
+
form.username = 'keysdown'
|
|
288
|
+
|
|
289
|
+
console.log(form.wasChanged('username')) // true
|
|
290
|
+
|
|
291
|
+
console.log(form.wasChanged(['name', 'username'])) // true (any changed)
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### filled(field)
|
|
295
|
+
|
|
296
|
+
Method used to check if one or more fields are filled (not `null`, not `undefined`, not empty string `''`). When an array is provided, returns `true` only if **all** fields are filled.
|
|
297
|
+
|
|
298
|
+
```js
|
|
299
|
+
const form = createForm({
|
|
300
|
+
name: null,
|
|
301
|
+
username: null
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
form.username = 'keysdown'
|
|
305
|
+
|
|
306
|
+
console.log(form.filled('username')) // true
|
|
307
|
+
|
|
308
|
+
console.log(form.filled('name')) // false
|
|
309
|
+
|
|
310
|
+
console.log(form.filled(['name', 'username'])) // false (name is not filled)
|
|
311
|
+
```
|
|
312
|
+
|
|
274
313
|
### setAwaiting(awaiting = true)
|
|
275
314
|
|
|
276
315
|
Method used to change the form loading state.
|
|
@@ -551,10 +590,11 @@ form.rules.push('username', 'required')
|
|
|
551
590
|
|
|
552
591
|
#### has(key)
|
|
553
592
|
|
|
554
|
-
Checking if the collection has an item with the key.
|
|
593
|
+
Checking if the collection has an item with the key. Accepts a single key or an array of keys. When an array is provided, returns `true` if **any** of the keys exist.
|
|
555
594
|
|
|
556
595
|
```js
|
|
557
596
|
form.errors.has('username')
|
|
597
|
+
form.errors.has(['username', 'email'])
|
|
558
598
|
form.messages.has('username')
|
|
559
599
|
form.rules.has('username')
|
|
560
600
|
```
|
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 Array.isArray(e)?e.some(e=>this.collection.hasOwnProperty(e)):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}wasChanged(e){return Array.isArray(e)?e.some(e=>this[e]!==this.originalValues[e]):this[e]!==this.originalValues[e]}filled(e){return Array.isArray(e)?e.every(e=>this[e]!==null&&this[e]!==void 0&&this[e]!==``):this[e]!==null&&this[e]!==void 0&&this[e]!==``}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 Array.isArray(e)?e.some(e=>this.collection.hasOwnProperty(e)):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}wasChanged(e){return Array.isArray(e)?e.some(e=>this[e]!==this.originalValues[e]):this[e]!==this.originalValues[e]}filled(e){return Array.isArray(e)?e.every(e=>this[e]!==null&&this[e]!==void 0&&this[e]!==``):this[e]!==null&&this[e]!==void 0&&this[e]!==``}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,164 @@
|
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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(t) {
|
|
174
|
-
const s = {};
|
|
175
|
-
return Object.keys(this.originalValues).forEach((i) => {
|
|
176
|
-
(!t || t.includes(i)) && (s[i] = this[i]);
|
|
177
|
-
}), s;
|
|
178
|
-
}
|
|
179
|
-
valuesAsFormData(t) {
|
|
180
|
-
return u(this.values(t));
|
|
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 Array.isArray(e) ? e.some((e) => this.collection.hasOwnProperty(e)) : 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
|
+
wasChanged(e) {
|
|
120
|
+
return Array.isArray(e) ? e.some((e) => this[e] !== this.originalValues[e]) : this[e] !== this.originalValues[e];
|
|
121
|
+
}
|
|
122
|
+
filled(e) {
|
|
123
|
+
return Array.isArray(e) ? e.every((e) => this[e] !== null && this[e] !== void 0 && this[e] !== "") : this[e] !== null && this[e] !== void 0 && this[e] !== "";
|
|
124
|
+
}
|
|
125
|
+
get rules() {
|
|
126
|
+
return this.validation.rules;
|
|
127
|
+
}
|
|
128
|
+
setAwaiting(e = !0) {
|
|
129
|
+
return this.awaiting = e, this;
|
|
130
|
+
}
|
|
131
|
+
validate(e = null) {
|
|
132
|
+
return e ? this.validateField(e) : this.validateForm();
|
|
133
|
+
}
|
|
134
|
+
validateField(e) {
|
|
135
|
+
this.validation.errors.unset(e);
|
|
136
|
+
let t = this.validation.rules.get(e);
|
|
137
|
+
if (t && t.length > 0) {
|
|
138
|
+
let n = t.map((t) => {
|
|
139
|
+
let n = t.split(":"), r = n[0], i = n.length === 2 ? n[1].split(",") : [];
|
|
140
|
+
return r in s ? s[r](this[e], i).catch((t) => {
|
|
141
|
+
let n = this.validation.messages.get(e);
|
|
142
|
+
return n && r in n && this.validation.errors.push(e, n[r]), Promise.reject(t);
|
|
143
|
+
}) : Promise.reject(/* @__PURE__ */ Error(`There is no validation rule called "${r}"`));
|
|
144
|
+
});
|
|
145
|
+
return Promise.all(n).then(() => {});
|
|
146
|
+
}
|
|
147
|
+
return Promise.resolve();
|
|
148
|
+
}
|
|
149
|
+
validateForm() {
|
|
150
|
+
let e = Object.keys(this.originalValues).map((e) => this.validateField(e));
|
|
151
|
+
return Promise.all(e).then(() => Promise.resolve(this)).catch(() => Promise.reject(this));
|
|
152
|
+
}
|
|
153
|
+
values(e) {
|
|
154
|
+
let t = {};
|
|
155
|
+
return Object.keys(this.originalValues).forEach((n) => {
|
|
156
|
+
(!e || e.includes(n)) && (t[n] = this[n]);
|
|
157
|
+
}), t;
|
|
158
|
+
}
|
|
159
|
+
valuesAsFormData(e) {
|
|
160
|
+
return l(this.values(e));
|
|
161
|
+
}
|
|
162
|
+
}, d = (e) => new u(e), f = u;
|
|
163
|
+
//#endregion
|
|
164
|
+
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 Array.isArray(e)?e.some(e=>this.collection.hasOwnProperty(e)):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}wasChanged(e){return Array.isArray(e)?e.some(e=>this[e]!==this.originalValues[e]):this[e]!==this.originalValues[e]}filled(e){return Array.isArray(e)?e.every(e=>this[e]!==null&&this[e]!==void 0&&this[e]!==``):this[e]!==null&&this[e]!==void 0&&this[e]!==``}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});
|
|
@@ -17,6 +17,8 @@ export declare class Form {
|
|
|
17
17
|
removeField(field: string): this;
|
|
18
18
|
removeFields(fields: string[]): this;
|
|
19
19
|
reset(): this;
|
|
20
|
+
wasChanged(field: string | string[]): boolean;
|
|
21
|
+
filled(field: string | string[]): boolean;
|
|
20
22
|
get rules(): import('./Rules').Rules;
|
|
21
23
|
setAwaiting(awaiting?: boolean): this;
|
|
22
24
|
validate(field?: string | null): Promise<this | void>;
|
|
@@ -6,7 +6,7 @@ export declare class Collection<T> {
|
|
|
6
6
|
any(): boolean;
|
|
7
7
|
fill(items: Items<T>): this;
|
|
8
8
|
push(key: string, data: any): this;
|
|
9
|
-
has(key: string): boolean;
|
|
9
|
+
has(key: string | string[]): boolean;
|
|
10
10
|
get(item: string, defaultValue?: T | null): T | null;
|
|
11
11
|
unset(key: string): this;
|
|
12
12
|
clear(): this;
|
package/package.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keysdown/form-wrapper",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.5",
|
|
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
|