aport-tools 4.4.30 → 4.4.31
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/dist/forms/FormContext.d.ts +3 -2
- package/dist/index.esm.js +24 -20
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +24 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
@@ -12,12 +12,13 @@ interface FormContextProps {
|
|
12
12
|
/**
|
13
13
|
* A function to update a specific form field value.
|
14
14
|
* It takes the `name` of the form field and the new `value` to be set.
|
15
|
+
* and an optional `firstValue` to track the initial value.
|
15
16
|
* Example usage:
|
16
17
|
* ```ts
|
17
|
-
* setFormValue('email', 'newemail@example.com');
|
18
|
+
* setFormValue('email', 'newemail@example.com', 'oldemail@example.com');
|
18
19
|
* ```
|
19
20
|
*/
|
20
|
-
setFormValue: (name: string, value: any) => void;
|
21
|
+
setFormValue: (name: string, value: any, firstValue?: any) => void;
|
21
22
|
/**
|
22
23
|
* Stores the current form errors as an object.
|
23
24
|
* Each key is a form field name and the value is an array of error messages for that field.
|
package/dist/index.esm.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
/*! aport-tools v4.4.
|
1
|
+
/*! aport-tools v4.4.31 | ISC */
|
2
2
|
import React, { useContext, useState, createContext, useRef, useEffect, useMemo, useCallback } from 'react';
|
3
3
|
import { StyleSheet, Text as Text$1, Animated, View, TouchableOpacity, Image, TextInput, Modal, Pressable, FlatList, Keyboard, Platform, Alert, ActivityIndicator } from 'react-native';
|
4
4
|
import { ThemeContext } from 'aport-themes';
|
@@ -395,40 +395,44 @@ var Form = function Form(_a) {
|
|
395
395
|
var _c = useState({}),
|
396
396
|
errors = _c[0],
|
397
397
|
setErrors = _c[1];
|
398
|
-
var
|
398
|
+
var _d = useState({}),
|
399
|
+
firstValues = _d[0],
|
400
|
+
setFirstValues = _d[1]; // Track firstValues
|
401
|
+
var setFormValue = function setFormValue(name, value, firstValue) {
|
399
402
|
setFormValues(function (prev) {
|
400
403
|
var _a;
|
401
404
|
return __assign(__assign({}, prev), (_a = {}, _a[name] = value, _a));
|
402
405
|
});
|
406
|
+
if (firstValue !== undefined) {
|
407
|
+
setFirstValues(function (prev) {
|
408
|
+
var _a;
|
409
|
+
return __assign(__assign({}, prev), (_a = {}, _a[name] = firstValue, _a));
|
410
|
+
});
|
411
|
+
}
|
403
412
|
};
|
404
413
|
var handleSubmit = function handleSubmit() {
|
405
414
|
return __awaiter(void 0, void 0, void 0, function () {
|
406
|
-
var
|
415
|
+
var modifiedValues, validationErrors;
|
407
416
|
return __generator(this, function (_a) {
|
408
417
|
switch (_a.label) {
|
409
418
|
case 0:
|
410
|
-
|
411
|
-
var
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
acc[key] = currentField.value;
|
417
|
-
}
|
418
|
-
} else {
|
419
|
-
// Include the field if firstValue is not defined
|
420
|
-
acc[key] = currentField.value;
|
419
|
+
modifiedValues = Object.keys(formValues).reduce(function (result, key) {
|
420
|
+
var hasFirstValue = key in firstValues;
|
421
|
+
var isModified = hasFirstValue && formValues[key] !== firstValues[key];
|
422
|
+
var isNewValue = !hasFirstValue;
|
423
|
+
if (isModified || isNewValue) {
|
424
|
+
result[key] = formValues[key];
|
421
425
|
}
|
422
|
-
return
|
426
|
+
return result;
|
423
427
|
}, {});
|
424
|
-
return [4 /*yield*/, onSubmit(
|
428
|
+
return [4 /*yield*/, onSubmit(modifiedValues)];
|
425
429
|
case 1:
|
426
430
|
validationErrors = _a.sent();
|
427
431
|
// Set the validation errors in state
|
428
432
|
setErrors(validationErrors);
|
429
|
-
// Prevent submission if there are
|
433
|
+
// Prevent submission if there are any errors
|
430
434
|
if (Object.keys(validationErrors).length > 0) {
|
431
|
-
return [2 /*return*/]; //
|
435
|
+
return [2 /*return*/]; // Prevent submission
|
432
436
|
}
|
433
437
|
return [2 /*return*/];
|
434
438
|
}
|
@@ -552,12 +556,12 @@ var Input = function Input(_a) {
|
|
552
556
|
isFirstRender.current = false;
|
553
557
|
if (firstValue !== undefined) {
|
554
558
|
setInternalValue(firstValue);
|
555
|
-
setFormValue(name, firstValue);
|
559
|
+
setFormValue(name, firstValue, firstValue); // Pass firstValue here
|
556
560
|
} else {
|
557
561
|
setInternalValue(formValues[name] || "");
|
558
562
|
}
|
559
563
|
}
|
560
|
-
}, [firstValue]);
|
564
|
+
}, [firstValue]);
|
561
565
|
/**
|
562
566
|
* Handles text changes in the input field, applying formatting based on the inputType.
|
563
567
|
*
|