aport-tools 4.4.27 → 4.4.29

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/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! aport-tools v4.4.27 | ISC */
1
+ /*! aport-tools v4.4.29 | ISC */
2
2
  'use strict';
3
3
 
4
4
  var React = require('react');
@@ -416,42 +416,66 @@ var Form = function Form(_a) {
416
416
  var _c = React.useState({}),
417
417
  errors = _c[0],
418
418
  setErrors = _c[1];
419
+ // Ref to store the initial form values (firstValue).
420
+ var initialFormValues = React.useRef({});
419
421
  var setFormValue = function setFormValue(name, value) {
420
422
  setFormValues(function (prev) {
421
423
  var _a;
422
424
  return __assign(__assign({}, prev), (_a = {}, _a[name] = value, _a));
423
425
  });
426
+ if (!initialFormValues.current[name]) {
427
+ initialFormValues.current[name] = value; // Set the initial value once.
428
+ }
424
429
  };
425
430
  var handleSubmit = function handleSubmit() {
426
431
  return __awaiter(void 0, void 0, void 0, function () {
427
- var validationErrors;
432
+ var modifiedFields, validationErrors;
428
433
  return __generator(this, function (_a) {
429
434
  switch (_a.label) {
430
435
  case 0:
431
- return [4 /*yield*/, onSubmit(formValues)];
436
+ modifiedFields = Object.keys(formValues).reduce(function (acc, key) {
437
+ var currentField = formValues[key]; // currentField is FormField
438
+ var initialField = currentField.firstValue; // Get initial value
439
+ // Compare current value with initial value
440
+ if (currentField.value !== initialField) {
441
+ acc[key] = currentField.value; // Include only modified fields
442
+ }
443
+ return acc;
444
+ }, {});
445
+ return [4 /*yield*/, onSubmit(modifiedFields)];
432
446
  case 1:
433
447
  validationErrors = _a.sent();
434
448
  // Set the validation errors in state
435
449
  setErrors(validationErrors);
436
- // Prevent submission if there are any errors
450
+ // Prevent submission if there are validation errors
437
451
  if (Object.keys(validationErrors).length > 0) {
438
- return [2 /*return*/]; // Prevent submission
452
+ return [2 /*return*/]; // Exit early if errors are present
439
453
  }
440
454
  return [2 /*return*/];
441
455
  }
442
456
  });
443
457
  });
444
458
  };
445
- var handleFormSubmit = function handleFormSubmit(formValues) {
459
+ var handleFormSubmit = function handleFormSubmit() {
446
460
  return __awaiter(void 0, void 0, void 0, function () {
447
- var validationErrors;
461
+ var modifiedFields, validationErrors;
448
462
  return __generator(this, function (_a) {
449
463
  switch (_a.label) {
450
464
  case 0:
451
- return [4 /*yield*/, onSubmit(formValues)];
465
+ modifiedFields = Object.keys(formValues).reduce(function (acc, key) {
466
+ var currentField = formValues[key];
467
+ var initialField = currentField === null || currentField === void 0 ? void 0 : currentField.firstValue; // Get initial value
468
+ if (currentField.value !== initialField) {
469
+ acc[key] = currentField.value; // Include only modified fields
470
+ }
471
+ return acc;
472
+ }, {});
473
+ return [4 /*yield*/, onSubmit(modifiedFields)];
452
474
  case 1:
453
475
  validationErrors = _a.sent();
476
+ // Set the validation errors in state
454
477
  setErrors(validationErrors);
478
+ // Return validation errors to allow further actions if needed
455
479
  return [2 /*return*/, validationErrors];
456
480
  }
457
481
  });
@@ -1036,38 +1060,33 @@ var InputCheck = function InputCheck(_a) {
1036
1060
  max = _a.max,
1037
1061
  _c = _a.rowAmount,
1038
1062
  rowAmount = _c === void 0 ? 3 : _c,
1039
- _d = _a.firstValue,
1040
- firstValue = _d === void 0 ? [] : _d,
1041
- _e = _a.iconPosition,
1042
- iconPosition = _e === void 0 ? "row" : _e,
1043
- _f = _a.disabled,
1044
- disabled = _f === void 0 ? false : _f;
1045
- var _g = useFormContext(),
1046
- formValues = _g.formValues,
1047
- setFormValue = _g.setFormValue,
1048
- formErrors = _g.errors;
1049
- var _h = React.useState(formValues[name] || []),
1050
- selectedValues = _h[0],
1051
- setSelectedValues = _h[1];
1063
+ firstValue = _a.firstValue,
1064
+ _d = _a.iconPosition,
1065
+ iconPosition = _d === void 0 ? "row" : _d,
1066
+ _e = _a.disabled,
1067
+ disabled = _e === void 0 ? false : _e;
1068
+ var _f = useFormContext();
1069
+ _f.formValues;
1070
+ var setFormValue = _f.setFormValue,
1071
+ formErrors = _f.errors;
1072
+ var _g = React.useState([]),
1073
+ selectedValues = _g[0],
1074
+ setSelectedValues = _g[1];
1052
1075
  var theme = React.useContext(aportThemes.ThemeContext).theme;
1053
1076
  var colors = theme.colors;
1054
- // Sync firstValue with selectedValues on mount or when firstValue changes
1077
+ var isFirstRender = React.useRef(true);
1078
+ // Initialize selectedValues on first render if firstValue is provided
1055
1079
  React.useEffect(function () {
1056
- var initialSelectedValues = options.filter(function (option) {
1057
- return firstValue.some(function (fv) {
1058
- return fv.value === option.value;
1080
+ if (isFirstRender.current && firstValue) {
1081
+ var initialSelectedValues = options.filter(function (option) {
1082
+ return firstValue.some(function (fv) {
1083
+ return fv.value === option.value;
1084
+ });
1059
1085
  });
1060
- });
1061
- var formattedValues = initialSelectedValues.map(function (_a) {
1062
- var id = _a.id,
1063
- value = _a.value;
1064
- return {
1065
- id: id,
1066
- value: value
1067
- };
1068
- });
1069
- setSelectedValues(formattedValues);
1070
- setFormValue(name, formattedValues); // Update form context
1086
+ setSelectedValues(initialSelectedValues);
1087
+ setFormValue(name, initialSelectedValues); // Update form context
1088
+ isFirstRender.current = false; // Prevent subsequent updates
1089
+ }
1071
1090
  }, [firstValue, name, options, setFormValue]);
1072
1091
  var handleSelect = function handleSelect(id, value) {
1073
1092
  if (disabled) return;