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.
@@ -1 +1 @@
1
- export { Form, useFormContext } from './FormContext';
1
+ export { Form, useFormContext, setFormValueGlobal } from './FormContext';
@@ -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.
@@ -44,6 +45,7 @@ interface FormContextProps {
44
45
  handleSubmit: () => void;
45
46
  handleFormSubmit: (formValues: Record<string, any>) => Promise<Record<string, string[]>>;
46
47
  }
48
+ export declare const setFormValueGlobal: (name: string, value: any, firstValue?: any) => void;
47
49
  export declare const useFormContext: () => FormContextProps;
48
50
  interface StepperProps {
49
51
  /**
@@ -1,5 +1,5 @@
1
1
  export { Input } from './Input';
2
- export { Form, useFormContext } from './FormContext';
2
+ export { Form, useFormContext, setFormValueGlobal } from './FormContext';
3
3
  export { default as TextArea } from './TextArea';
4
4
  export { default as Label } from './Label';
5
5
  export { default as ErrorList } from './ErrorList';
package/dist/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! aport-tools v4.4.30 | ISC */
1
+ /*! aport-tools v4.4.32 | 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';
@@ -377,6 +377,18 @@ var styles$9 = StyleSheet.create({
377
377
  });
378
378
 
379
379
  // src/forms/FormContext.tsx
380
+ // Create a ref to hold the global `setFormValue` function
381
+ var globalSetFormValueRef = {
382
+ current: null
383
+ };
384
+ // Utility to use `setFormValue` globally
385
+ var setFormValueGlobal = function setFormValueGlobal(name, value, firstValue) {
386
+ if (globalSetFormValueRef.current) {
387
+ globalSetFormValueRef.current(name, value, firstValue);
388
+ } else {
389
+ console.warn("setFormValueGlobal was called before the Form was rendered.");
390
+ }
391
+ };
380
392
  var FormContext = /*#__PURE__*/createContext(undefined);
381
393
  var useFormContext = function useFormContext() {
382
394
  var context = useContext(FormContext);
@@ -395,40 +407,51 @@ var Form = function Form(_a) {
395
407
  var _c = useState({}),
396
408
  errors = _c[0],
397
409
  setErrors = _c[1];
398
- var setFormValue = function setFormValue(name, value) {
410
+ var _d = useState({}),
411
+ firstValues = _d[0],
412
+ setFirstValues = _d[1]; // Track firstValues
413
+ // Assign the local `setFormValue` to the global ref on first render
414
+ React.useEffect(function () {
415
+ globalSetFormValueRef.current = setFormValue;
416
+ return function () {
417
+ globalSetFormValueRef.current = null; // Cleanup on unmount
418
+ };
419
+ }, []);
420
+ var setFormValue = function setFormValue(name, value, firstValue) {
399
421
  setFormValues(function (prev) {
400
422
  var _a;
401
423
  return __assign(__assign({}, prev), (_a = {}, _a[name] = value, _a));
402
424
  });
425
+ if (firstValue !== undefined) {
426
+ setFirstValues(function (prev) {
427
+ var _a;
428
+ return __assign(__assign({}, prev), (_a = {}, _a[name] = firstValue, _a));
429
+ });
430
+ }
403
431
  };
404
432
  var handleSubmit = function handleSubmit() {
405
433
  return __awaiter(void 0, void 0, void 0, function () {
406
- var modifiedFields, validationErrors;
434
+ var modifiedValues, validationErrors;
407
435
  return __generator(this, function (_a) {
408
436
  switch (_a.label) {
409
437
  case 0:
410
- modifiedFields = Object.keys(formValues).reduce(function (acc, key) {
411
- var currentField = formValues[key];
412
- // Check if firstValue exists
413
- if ((currentField === null || currentField === void 0 ? void 0 : currentField.firstValue) !== undefined) {
414
- // Only include the field if the value has changed
415
- if (currentField.value !== currentField.firstValue) {
416
- acc[key] = currentField.value;
417
- }
418
- } else {
419
- // Include the field if firstValue is not defined
420
- acc[key] = currentField.value;
438
+ modifiedValues = Object.keys(formValues).reduce(function (result, key) {
439
+ var hasFirstValue = key in firstValues;
440
+ var isModified = hasFirstValue && formValues[key] !== firstValues[key];
441
+ var isNewValue = !hasFirstValue;
442
+ if (isModified || isNewValue) {
443
+ result[key] = formValues[key];
421
444
  }
422
- return acc;
445
+ return result;
423
446
  }, {});
424
- return [4 /*yield*/, onSubmit(modifiedFields)];
447
+ return [4 /*yield*/, onSubmit(modifiedValues)];
425
448
  case 1:
426
449
  validationErrors = _a.sent();
427
450
  // Set the validation errors in state
428
451
  setErrors(validationErrors);
429
- // Prevent submission if there are validation errors
452
+ // Prevent submission if there are any errors
430
453
  if (Object.keys(validationErrors).length > 0) {
431
- return [2 /*return*/]; // Exit early if errors are present
454
+ return [2 /*return*/]; // Prevent submission
432
455
  }
433
456
  return [2 /*return*/];
434
457
  }
@@ -552,12 +575,12 @@ var Input = function Input(_a) {
552
575
  isFirstRender.current = false;
553
576
  if (firstValue !== undefined) {
554
577
  setInternalValue(firstValue);
555
- setFormValue(name, firstValue);
578
+ setFormValue(name, firstValue, firstValue); // Pass firstValue here
556
579
  } else {
557
580
  setInternalValue(formValues[name] || "");
558
581
  }
559
582
  }
560
- }, [firstValue]); // Only re-run if `firstValue` changes
583
+ }, [firstValue]);
561
584
  /**
562
585
  * Handles text changes in the input field, applying formatting based on the inputType.
563
586
  *
@@ -1458,5 +1481,5 @@ var styles = StyleSheet.create({
1458
1481
  }
1459
1482
  });
1460
1483
 
1461
- export { Button, Card, ErrorList, Form, Input, InputAttach, InputCheck, InputList, Label, Text, TextArea, useFormContext };
1484
+ export { Button, Card, ErrorList, Form, Input, InputAttach, InputCheck, InputList, Label, Text, TextArea, setFormValueGlobal, useFormContext };
1462
1485
  //# sourceMappingURL=index.esm.js.map