aport-tools 4.4.30 → 4.4.32
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/Form.d.ts +1 -1
- package/dist/forms/FormContext.d.ts +4 -2
- package/dist/forms/index.d.ts +1 -1
- package/dist/index.esm.js +44 -21
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +44 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
/*! aport-tools v4.4.
|
1
|
+
/*! aport-tools v4.4.32 | ISC */
|
2
2
|
'use strict';
|
3
3
|
|
4
4
|
var React = require('react');
|
@@ -398,6 +398,18 @@ var styles$9 = reactNative.StyleSheet.create({
|
|
398
398
|
});
|
399
399
|
|
400
400
|
// src/forms/FormContext.tsx
|
401
|
+
// Create a ref to hold the global `setFormValue` function
|
402
|
+
var globalSetFormValueRef = {
|
403
|
+
current: null
|
404
|
+
};
|
405
|
+
// Utility to use `setFormValue` globally
|
406
|
+
var setFormValueGlobal = function setFormValueGlobal(name, value, firstValue) {
|
407
|
+
if (globalSetFormValueRef.current) {
|
408
|
+
globalSetFormValueRef.current(name, value, firstValue);
|
409
|
+
} else {
|
410
|
+
console.warn("setFormValueGlobal was called before the Form was rendered.");
|
411
|
+
}
|
412
|
+
};
|
401
413
|
var FormContext = /*#__PURE__*/React.createContext(undefined);
|
402
414
|
var useFormContext = function useFormContext() {
|
403
415
|
var context = React.useContext(FormContext);
|
@@ -416,40 +428,51 @@ var Form = function Form(_a) {
|
|
416
428
|
var _c = React.useState({}),
|
417
429
|
errors = _c[0],
|
418
430
|
setErrors = _c[1];
|
419
|
-
var
|
431
|
+
var _d = React.useState({}),
|
432
|
+
firstValues = _d[0],
|
433
|
+
setFirstValues = _d[1]; // Track firstValues
|
434
|
+
// Assign the local `setFormValue` to the global ref on first render
|
435
|
+
React.useEffect(function () {
|
436
|
+
globalSetFormValueRef.current = setFormValue;
|
437
|
+
return function () {
|
438
|
+
globalSetFormValueRef.current = null; // Cleanup on unmount
|
439
|
+
};
|
440
|
+
}, []);
|
441
|
+
var setFormValue = function setFormValue(name, value, firstValue) {
|
420
442
|
setFormValues(function (prev) {
|
421
443
|
var _a;
|
422
444
|
return __assign(__assign({}, prev), (_a = {}, _a[name] = value, _a));
|
423
445
|
});
|
446
|
+
if (firstValue !== undefined) {
|
447
|
+
setFirstValues(function (prev) {
|
448
|
+
var _a;
|
449
|
+
return __assign(__assign({}, prev), (_a = {}, _a[name] = firstValue, _a));
|
450
|
+
});
|
451
|
+
}
|
424
452
|
};
|
425
453
|
var handleSubmit = function handleSubmit() {
|
426
454
|
return __awaiter(void 0, void 0, void 0, function () {
|
427
|
-
var
|
455
|
+
var modifiedValues, validationErrors;
|
428
456
|
return __generator(this, function (_a) {
|
429
457
|
switch (_a.label) {
|
430
458
|
case 0:
|
431
|
-
|
432
|
-
var
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
acc[key] = currentField.value;
|
438
|
-
}
|
439
|
-
} else {
|
440
|
-
// Include the field if firstValue is not defined
|
441
|
-
acc[key] = currentField.value;
|
459
|
+
modifiedValues = Object.keys(formValues).reduce(function (result, key) {
|
460
|
+
var hasFirstValue = key in firstValues;
|
461
|
+
var isModified = hasFirstValue && formValues[key] !== firstValues[key];
|
462
|
+
var isNewValue = !hasFirstValue;
|
463
|
+
if (isModified || isNewValue) {
|
464
|
+
result[key] = formValues[key];
|
442
465
|
}
|
443
|
-
return
|
466
|
+
return result;
|
444
467
|
}, {});
|
445
|
-
return [4 /*yield*/, onSubmit(
|
468
|
+
return [4 /*yield*/, onSubmit(modifiedValues)];
|
446
469
|
case 1:
|
447
470
|
validationErrors = _a.sent();
|
448
471
|
// Set the validation errors in state
|
449
472
|
setErrors(validationErrors);
|
450
|
-
// Prevent submission if there are
|
473
|
+
// Prevent submission if there are any errors
|
451
474
|
if (Object.keys(validationErrors).length > 0) {
|
452
|
-
return [2 /*return*/]; //
|
475
|
+
return [2 /*return*/]; // Prevent submission
|
453
476
|
}
|
454
477
|
return [2 /*return*/];
|
455
478
|
}
|
@@ -573,12 +596,12 @@ var Input = function Input(_a) {
|
|
573
596
|
isFirstRender.current = false;
|
574
597
|
if (firstValue !== undefined) {
|
575
598
|
setInternalValue(firstValue);
|
576
|
-
setFormValue(name, firstValue);
|
599
|
+
setFormValue(name, firstValue, firstValue); // Pass firstValue here
|
577
600
|
} else {
|
578
601
|
setInternalValue(formValues[name] || "");
|
579
602
|
}
|
580
603
|
}
|
581
|
-
}, [firstValue]);
|
604
|
+
}, [firstValue]);
|
582
605
|
/**
|
583
606
|
* Handles text changes in the input field, applying formatting based on the inputType.
|
584
607
|
*
|
@@ -1490,5 +1513,6 @@ exports.InputList = InputList;
|
|
1490
1513
|
exports.Label = Label;
|
1491
1514
|
exports.Text = Text;
|
1492
1515
|
exports.TextArea = TextArea;
|
1516
|
+
exports.setFormValueGlobal = setFormValueGlobal;
|
1493
1517
|
exports.useFormContext = useFormContext;
|
1494
1518
|
//# sourceMappingURL=index.js.map
|