aport-tools 4.4.29 → 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 +27 -33
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +26 -32
- 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,5 +1,5 @@
|
|
1
|
-
/*! aport-tools v4.4.
|
2
|
-
import React, { useContext, useState,
|
1
|
+
/*! aport-tools v4.4.31 | ISC */
|
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';
|
5
5
|
import * as ImagePicker from 'expo-image-picker';
|
@@ -395,66 +395,60 @@ var Form = function Form(_a) {
|
|
395
395
|
var _c = useState({}),
|
396
396
|
errors = _c[0],
|
397
397
|
setErrors = _c[1];
|
398
|
-
|
399
|
-
|
400
|
-
|
398
|
+
var _d = useState({}),
|
399
|
+
firstValues = _d[0],
|
400
|
+
setFirstValues = _d[1]; // Track firstValues
|
401
|
+
var setFormValue = function setFormValue(name, value, firstValue) {
|
401
402
|
setFormValues(function (prev) {
|
402
403
|
var _a;
|
403
404
|
return __assign(__assign({}, prev), (_a = {}, _a[name] = value, _a));
|
404
405
|
});
|
405
|
-
if (
|
406
|
-
|
406
|
+
if (firstValue !== undefined) {
|
407
|
+
setFirstValues(function (prev) {
|
408
|
+
var _a;
|
409
|
+
return __assign(__assign({}, prev), (_a = {}, _a[name] = firstValue, _a));
|
410
|
+
});
|
407
411
|
}
|
408
412
|
};
|
409
413
|
var handleSubmit = function handleSubmit() {
|
410
414
|
return __awaiter(void 0, void 0, void 0, function () {
|
411
|
-
var
|
415
|
+
var modifiedValues, validationErrors;
|
412
416
|
return __generator(this, function (_a) {
|
413
417
|
switch (_a.label) {
|
414
418
|
case 0:
|
415
|
-
|
416
|
-
var
|
417
|
-
var
|
418
|
-
|
419
|
-
if (
|
420
|
-
|
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
|
}
|
435
439
|
});
|
436
440
|
});
|
437
441
|
};
|
438
|
-
var handleFormSubmit = function handleFormSubmit() {
|
442
|
+
var handleFormSubmit = function handleFormSubmit(formValues) {
|
439
443
|
return __awaiter(void 0, void 0, void 0, function () {
|
440
|
-
var
|
444
|
+
var validationErrors;
|
441
445
|
return __generator(this, function (_a) {
|
442
446
|
switch (_a.label) {
|
443
447
|
case 0:
|
444
|
-
|
445
|
-
var currentField = formValues[key];
|
446
|
-
var initialField = currentField === null || currentField === void 0 ? void 0 : currentField.firstValue; // Get initial value
|
447
|
-
if (currentField.value !== initialField) {
|
448
|
-
acc[key] = currentField.value; // Include only modified fields
|
449
|
-
}
|
450
|
-
return acc;
|
451
|
-
}, {});
|
452
|
-
return [4 /*yield*/, onSubmit(modifiedFields)];
|
448
|
+
return [4 /*yield*/, onSubmit(formValues)];
|
453
449
|
case 1:
|
454
450
|
validationErrors = _a.sent();
|
455
|
-
// Set the validation errors in state
|
456
451
|
setErrors(validationErrors);
|
457
|
-
// Return validation errors to allow further actions if needed
|
458
452
|
return [2 /*return*/, validationErrors];
|
459
453
|
}
|
460
454
|
});
|
@@ -562,12 +556,12 @@ var Input = function Input(_a) {
|
|
562
556
|
isFirstRender.current = false;
|
563
557
|
if (firstValue !== undefined) {
|
564
558
|
setInternalValue(firstValue);
|
565
|
-
setFormValue(name, firstValue);
|
559
|
+
setFormValue(name, firstValue, firstValue); // Pass firstValue here
|
566
560
|
} else {
|
567
561
|
setInternalValue(formValues[name] || "");
|
568
562
|
}
|
569
563
|
}
|
570
|
-
}, [firstValue]);
|
564
|
+
}, [firstValue]);
|
571
565
|
/**
|
572
566
|
* Handles text changes in the input field, applying formatting based on the inputType.
|
573
567
|
*
|