aport-tools 4.4.12 → 4.4.14

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.
@@ -8,6 +8,10 @@ interface InputProps extends TextInputProps {
8
8
  * The unique identifier for the input field, used to manage its state within the form.
9
9
  */
10
10
  name: string;
11
+ /**
12
+ * Optional first value if you want to set values with fetch or dont have empty inputs.
13
+ */
14
+ firstValue?: string;
11
15
  /**
12
16
  * The text label displayed above the input field.
13
17
  */
@@ -11,6 +11,10 @@ interface InputListProps {
11
11
  * @type {string}
12
12
  * @default "Choose value/s"
13
13
  */
14
+ /**
15
+ * Optional first value if you want to set values with fetch or dont have empty inputs.
16
+ */
17
+ firstValue?: any[];
14
18
  placeholder?: string;
15
19
  /**
16
20
  * Custom styles for the component.
@@ -2,6 +2,10 @@ import React from 'react';
2
2
  import { TextInputProps } from 'react-native';
3
3
  interface TextAreaProps extends TextInputProps {
4
4
  name: string;
5
+ /**
6
+ * Optional first value if you want to set values with fetch or dont have empty inputs.
7
+ */
8
+ firstValue?: string;
5
9
  label: string;
6
10
  errors?: string[];
7
11
  }
package/dist/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
- /*! aport-tools v4.4.12 | ISC */
2
- import React, { useContext, useState, createContext, useCallback, useMemo } from 'react';
1
+ /*! aport-tools v4.4.14 | ISC */
2
+ import React, { useContext, useState, createContext, useEffect, useCallback, useMemo } 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';
@@ -517,14 +517,27 @@ var Input = function Input(_a) {
517
517
  var name = _a.name,
518
518
  label = _a.label,
519
519
  inputType = _a.inputType,
520
+ firstValue = _a.firstValue,
520
521
  style = _a.style,
521
- rest = __rest(_a, ["name", "label", "inputType", "style"]);
522
+ rest = __rest(_a, ["name", "label", "inputType", "firstValue", "style"]);
522
523
  var _b = useFormContext(),
523
524
  formValues = _b.formValues,
524
525
  setFormValue = _b.setFormValue,
525
526
  formErrors = _b.errors;
526
527
  var theme = useContext(ThemeContext).theme;
527
528
  var colors = theme.colors;
529
+ var _c = useState(""),
530
+ internalValue = _c[0],
531
+ setInternalValue = _c[1];
532
+ // Initialize the internal value based on `firstValue` or `formValues`
533
+ useEffect(function () {
534
+ if (firstValue) {
535
+ setInternalValue(firstValue);
536
+ setFormValue(name, firstValue);
537
+ } else {
538
+ setInternalValue(formValues[name] || "");
539
+ }
540
+ }, [firstValue, formValues[name]]);
528
541
  /**
529
542
  * Handles text changes in the input field, applying formatting based on the inputType.
530
543
  *
@@ -554,6 +567,7 @@ var Input = function Input(_a) {
554
567
  formattedText = text.replace(/\D/g, "");
555
568
  break;
556
569
  }
570
+ setInternalValue(formattedText);
557
571
  setFormValue(name, formattedText);
558
572
  };
559
573
  return /*#__PURE__*/React.createElement(View, {
@@ -568,7 +582,7 @@ var Input = function Input(_a) {
568
582
  borderColor: formErrors[name] ? colors.error.hex : "#CCC",
569
583
  color: colors.text.hex
570
584
  }, style],
571
- value: formValues[name] || "",
585
+ value: internalValue,
572
586
  onChangeText: handleChange,
573
587
  placeholder: label,
574
588
  placeholderTextColor: colors.placeHolder.hex
@@ -597,15 +611,29 @@ var TextArea = function TextArea(_a) {
597
611
  var name = _a.name,
598
612
  label = _a.label;
599
613
  _a.errors;
600
- var style = _a.style,
601
- rest = __rest(_a, ["name", "label", "errors", "style"]);
614
+ var firstValue = _a.firstValue,
615
+ style = _a.style,
616
+ rest = __rest(_a, ["name", "label", "errors", "firstValue", "style"]);
602
617
  var _b = useFormContext(),
603
618
  formValues = _b.formValues,
604
619
  setFormValue = _b.setFormValue,
605
620
  formErrors = _b.errors;
606
621
  var theme = useContext(ThemeContext).theme;
607
622
  var colors = theme.colors;
623
+ var _c = useState(""),
624
+ internalValue = _c[0],
625
+ setInternalValue = _c[1];
626
+ // Initialize the internal value based on `firstValue` or `formValues`
627
+ useEffect(function () {
628
+ if (firstValue) {
629
+ setInternalValue(firstValue);
630
+ setFormValue(name, firstValue);
631
+ } else {
632
+ setInternalValue(formValues[name] || "");
633
+ }
634
+ }, [firstValue, formValues[name]]);
608
635
  var handleChange = function handleChange(text) {
636
+ setInternalValue(text);
609
637
  setFormValue(name, text);
610
638
  };
611
639
  return /*#__PURE__*/React.createElement(View, {
@@ -620,7 +648,7 @@ var TextArea = function TextArea(_a) {
620
648
  color: colors.text.hex,
621
649
  borderColor: formErrors[name] ? colors.error.hex : "#CCC"
622
650
  }],
623
- value: formValues[name] || '',
651
+ value: internalValue,
624
652
  onChangeText: handleChange,
625
653
  placeholder: label,
626
654
  placeholderTextColor: colors.placeHolder.hex,
@@ -692,27 +720,39 @@ var InputList = function InputList(_a) {
692
720
  _d = _a.disabled,
693
721
  disabled = _d === void 0 ? false : _d,
694
722
  sortBy = _a.sortBy,
695
- _e = _a.separator,
696
- separator = _e === void 0 ? false : _e,
697
- _f = _a.closeOnScroll,
698
- closeOnScroll = _f === void 0 ? false : _f,
699
- _g = _a.closeOnSelect,
700
- closeOnSelect = _g === void 0 ? true : _g,
723
+ _e = _a.firstValue,
724
+ firstValue = _e === void 0 ? [] : _e,
725
+ _f = _a.separator,
726
+ separator = _f === void 0 ? false : _f,
727
+ _g = _a.closeOnScroll,
728
+ closeOnScroll = _g === void 0 ? false : _g,
729
+ _h = _a.closeOnSelect,
730
+ closeOnSelect = _h === void 0 ? true : _h,
701
731
  maxSelection = _a.maxSelection,
702
732
  onChange = _a.onChange;
703
- var _h = useFormContext(),
704
- formValues = _h.formValues,
705
- setFormValue = _h.setFormValue,
706
- formErrors = _h.errors;
707
- var _j = useState(false),
708
- isDropdownVisible = _j[0],
709
- setIsDropdownVisible = _j[1];
710
- var selectedOptions = formValues[name] || (multi ? [] : null);
733
+ var _j = useFormContext();
734
+ _j.formValues;
735
+ var setFormValue = _j.setFormValue,
736
+ formErrors = _j.errors;
737
+ var _k = useState(false),
738
+ isDropdownVisible = _k[0],
739
+ setIsDropdownVisible = _k[1];
711
740
  var sortedOptions = sortBy ? __spreadArray([], options, true).sort(function (a, b) {
712
741
  return a[sortBy] > b[sortBy] ? 1 : -1;
713
742
  }) : options;
714
743
  var theme = useContext(ThemeContext).theme;
715
744
  var colors = theme.colors;
745
+ // Initialize selected options based on firstValue
746
+ var initialSelections = options.filter(function (opt) {
747
+ return firstValue.includes(opt.value);
748
+ });
749
+ var _l = useState(multi ? initialSelections : initialSelections[0] || null),
750
+ selectedOptions = _l[0];
751
+ _l[1];
752
+ // Update form value on mount if firstValue is provided
753
+ useEffect(function () {
754
+ setFormValue(name, multi ? initialSelections : initialSelections[0] || null);
755
+ }, [firstValue]);
716
756
  /**
717
757
  * Handles selection of an option. Adds or removes the option from selectedOptions based on
718
758
  * multi-selection and maxSelection criteria.
@@ -721,17 +761,20 @@ var InputList = function InputList(_a) {
721
761
  var handleSelectOption = function handleSelectOption(option) {
722
762
  var updatedSelections;
723
763
  if (multi) {
724
- var alreadySelected = selectedOptions.some(function (opt) {
764
+ // Ensure selectedOptions is treated as an array
765
+ var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
766
+ var alreadySelected = selectedArray.some(function (opt) {
725
767
  return opt.id === option.id;
726
768
  });
727
- updatedSelections = alreadySelected ? selectedOptions.filter(function (opt) {
769
+ updatedSelections = alreadySelected ? selectedArray.filter(function (opt) {
728
770
  return opt.id !== option.id;
729
- }) : __spreadArray(__spreadArray([], selectedOptions, true), [option], false);
771
+ }) : __spreadArray(__spreadArray([], selectedArray, true), [option], false);
730
772
  if (!alreadySelected && maxSelection && updatedSelections.length >= maxSelection) {
731
773
  setIsDropdownVisible(false);
732
774
  }
733
775
  setFormValue(name, updatedSelections);
734
776
  } else {
777
+ // Handle single-selection case
735
778
  updatedSelections = option;
736
779
  setFormValue(name, option);
737
780
  if (closeOnSelect) setIsDropdownVisible(false);
@@ -741,14 +784,26 @@ var InputList = function InputList(_a) {
741
784
  onChange(updatedSelections);
742
785
  }
743
786
  };
787
+ var isItemDisabled = function isItemDisabled(option) {
788
+ if (!multi) return false; // Disable check only applies for multi-select
789
+ // Ensure selectedOptions is treated as an array
790
+ var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
791
+ return maxSelection && selectedArray.length >= maxSelection && !selectedArray.some(function (opt) {
792
+ return opt.id === option.id;
793
+ });
794
+ };
744
795
  /**
745
- * Renders selected options as a comma-separated string or the placeholder if none selected.
746
- * @returns {string} - The display text for selected options or placeholder.
747
- */
796
+ * Renders selected options as a comma-separated string or the placeholder if none selected.
797
+ * @returns {string} - The display text for selected options or placeholder.
798
+ */
748
799
  var renderSelectedText = function renderSelectedText() {
749
- if (multi) return selectedOptions.map(function (opt) {
750
- return opt.label;
751
- }).join(', ') || placeholder;
800
+ if (multi) {
801
+ // Ensure selectedOptions is treated as an array
802
+ var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
803
+ return selectedArray.map(function (opt) {
804
+ return opt.label;
805
+ }).join(', ') || placeholder;
806
+ }
752
807
  return (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.label) || placeholder;
753
808
  };
754
809
  /**
@@ -767,11 +822,6 @@ var InputList = function InputList(_a) {
767
822
  if (isDropdownVisible) setIsDropdownVisible(false);
768
823
  }, [isDropdownVisible]);
769
824
  // Conditionally render item as disabled if max selection reached and item is unselected
770
- var isItemDisabled = function isItemDisabled(option) {
771
- return multi && maxSelection && selectedOptions.length >= maxSelection && !selectedOptions.some(function (opt) {
772
- return opt.id === option.id;
773
- });
774
- };
775
825
  return /*#__PURE__*/React.createElement(View, {
776
826
  style: [styles$4.container, style]
777
827
  }, /*#__PURE__*/React.createElement(Text$1, {
@@ -809,9 +859,9 @@ var InputList = function InputList(_a) {
809
859
  },
810
860
  renderItem: function renderItem(_a) {
811
861
  var item = _a.item;
812
- var isSelected = multi ? selectedOptions.some(function (opt) {
862
+ var isSelected = multi ? Array.isArray(selectedOptions) && selectedOptions.some(function (opt) {
813
863
  return opt.id === item.id;
814
- }) : (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.id) === item.id;
864
+ }) : selectedOptions && 'id' in selectedOptions && selectedOptions.id === item.id;
815
865
  var isDisabled = isItemDisabled(item);
816
866
  return /*#__PURE__*/React.createElement(TouchableOpacity, {
817
867
  onPress: function onPress() {