@ziadshalaby/ngx-zs-component 3.2.5 → 3.2.6
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.
|
@@ -1232,20 +1232,16 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.9", ngImpor
|
|
|
1232
1232
|
}]
|
|
1233
1233
|
}] });
|
|
1234
1234
|
|
|
1235
|
-
// ==============================================
|
|
1236
|
-
// Types
|
|
1237
|
-
// ==============================================
|
|
1238
|
-
// ==============================================
|
|
1239
|
-
// Zform Class
|
|
1240
|
-
// ==============================================
|
|
1241
1235
|
class Form {
|
|
1242
1236
|
fields;
|
|
1243
|
-
|
|
1237
|
+
initialValues;
|
|
1244
1238
|
constructor(initial) {
|
|
1239
|
+
this.initialValues = { ...initial };
|
|
1245
1240
|
this.fields = Object.keys(initial).reduce((acc, key) => {
|
|
1246
1241
|
acc[key] = signal({
|
|
1247
1242
|
value: initial[key],
|
|
1248
1243
|
valid: false,
|
|
1244
|
+
touched: false,
|
|
1249
1245
|
});
|
|
1250
1246
|
return acc;
|
|
1251
1247
|
}, {});
|
|
@@ -1253,8 +1249,9 @@ class Form {
|
|
|
1253
1249
|
// ==============================================
|
|
1254
1250
|
// Field Accessors
|
|
1255
1251
|
// ==============================================
|
|
1256
|
-
set(key, value, valid = true) {
|
|
1257
|
-
this.fields[key]
|
|
1252
|
+
set(key, value, valid = true, touched = true) {
|
|
1253
|
+
const current = this.fields[key]();
|
|
1254
|
+
this.fields[key].set({ value, valid, touched: touched || current.touched });
|
|
1258
1255
|
}
|
|
1259
1256
|
patch(key, partial) {
|
|
1260
1257
|
const current = this.fields[key]();
|
|
@@ -1266,18 +1263,26 @@ class Form {
|
|
|
1266
1263
|
// ==============================================
|
|
1267
1264
|
// Form State & Validation
|
|
1268
1265
|
// ==============================================
|
|
1269
|
-
|
|
1270
|
-
const result = {};
|
|
1266
|
+
markAllTouched() {
|
|
1271
1267
|
for (const key in this.fields) {
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
result[key] = val !== null && val !== undefined && val !== '';
|
|
1275
|
-
}
|
|
1268
|
+
const field = this.fields[key]();
|
|
1269
|
+
this.fields[key].set({ ...field, touched: true });
|
|
1276
1270
|
}
|
|
1277
|
-
return result;
|
|
1278
1271
|
}
|
|
1279
|
-
|
|
1280
|
-
this.
|
|
1272
|
+
allTouched() {
|
|
1273
|
+
return Object.keys(this.fields).every(key => this.fields[key]().touched);
|
|
1274
|
+
}
|
|
1275
|
+
// ==============================================
|
|
1276
|
+
// Reset
|
|
1277
|
+
// ==============================================
|
|
1278
|
+
reset() {
|
|
1279
|
+
for (const key in this.fields) {
|
|
1280
|
+
this.fields[key].set({
|
|
1281
|
+
value: this.initialValues[key],
|
|
1282
|
+
valid: false,
|
|
1283
|
+
touched: false,
|
|
1284
|
+
});
|
|
1285
|
+
}
|
|
1281
1286
|
}
|
|
1282
1287
|
// ==============================================
|
|
1283
1288
|
// Data Extraction & Submission
|
|
@@ -1300,21 +1305,25 @@ class Form {
|
|
|
1300
1305
|
}
|
|
1301
1306
|
return result;
|
|
1302
1307
|
}
|
|
1308
|
+
allFilled() {
|
|
1309
|
+
const result = {};
|
|
1310
|
+
for (const key in this.fields) {
|
|
1311
|
+
if (this.fields.hasOwnProperty(key)) {
|
|
1312
|
+
const val = this.fields[key]().value;
|
|
1313
|
+
result[key] = val !== null && val !== undefined && val !== '';
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
return result;
|
|
1317
|
+
}
|
|
1303
1318
|
submit(callback, allowEmptyFields = [], allowInvalidFields = []) {
|
|
1304
1319
|
this.markAllTouched();
|
|
1305
1320
|
const filled = this.allFilled();
|
|
1306
1321
|
const validations = this.getValidations();
|
|
1307
|
-
// =============================
|
|
1308
|
-
// Check FILL
|
|
1309
|
-
// =============================
|
|
1310
1322
|
const allFilled = Object.keys(filled).every((key) => {
|
|
1311
1323
|
if (allowEmptyFields.includes(key))
|
|
1312
1324
|
return true;
|
|
1313
1325
|
return filled[key];
|
|
1314
1326
|
});
|
|
1315
|
-
// =============================
|
|
1316
|
-
// Check VALID
|
|
1317
|
-
// =============================
|
|
1318
1327
|
const allValid = Object.keys(validations).every((key) => {
|
|
1319
1328
|
if (allowInvalidFields.includes(key))
|
|
1320
1329
|
return true;
|