aport-tools 4.4.15 → 4.4.17

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,4 +1,4 @@
1
- import React from 'react';
1
+ import React from "react";
2
2
  interface Option {
3
3
  id: number | string;
4
4
  label: string;
@@ -14,7 +14,7 @@ interface InputListProps {
14
14
  /**
15
15
  * Optional first value if you want to set values with fetch or dont have empty inputs.
16
16
  */
17
- firstValue?: any[];
17
+ firstValue?: any[] | any;
18
18
  placeholder?: string;
19
19
  /**
20
20
  * Custom styles for the component.
package/dist/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
- /*! aport-tools v4.4.15 | ISC */
2
- import React, { useContext, useState, createContext, useEffect, useCallback, useMemo } from 'react';
1
+ /*! aport-tools v4.4.17 | ISC */
2
+ import React, { useContext, useState, createContext, 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';
@@ -730,31 +730,34 @@ var InputList = function InputList(_a) {
730
730
  closeOnSelect = _h === void 0 ? true : _h,
731
731
  maxSelection = _a.maxSelection,
732
732
  onChange = _a.onChange;
733
- var _j = useFormContext();
734
- _j.formValues;
735
- var setFormValue = _j.setFormValue,
733
+ var _j = useFormContext(),
734
+ formValues = _j.formValues,
735
+ setFormValue = _j.setFormValue,
736
736
  formErrors = _j.errors;
737
737
  var _k = useState(false),
738
738
  isDropdownVisible = _k[0],
739
739
  setIsDropdownVisible = _k[1];
740
- var sortedOptions = sortBy ? __spreadArray([], options, true).sort(function (a, b) {
741
- return a[sortBy] > b[sortBy] ? 1 : -1;
742
- }) : options;
740
+ var selectedOptions = firstValue || formValues[name] || (multi ? [] : null);
741
+ var sortedOptions = useMemo(function () {
742
+ return sortBy ? __spreadArray([], options, true).sort(function (a, b) {
743
+ return a[sortBy] > b[sortBy] ? 1 : -1;
744
+ }) : options;
745
+ }, [options, sortBy]);
743
746
  var theme = useContext(ThemeContext).theme;
744
747
  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
- setSelectedOptions = _l[1];
752
- // Update form value on mount if firstValue is provided
748
+ var _l = useState([]);
749
+ _l[0];
750
+ var setInternalValue = _l[1];
751
+ // Initialize the internal value based on `firstValue` or `formValues`
753
752
  useEffect(function () {
754
- if (Array.isArray(firstValue) && firstValue.length > 0) {
755
- setSelectedOptions(firstValue);
753
+ var selectedValue = formValues[name];
754
+ if (firstValue) {
755
+ setInternalValue(firstValue);
756
+ setFormValue(name, firstValue);
757
+ } else {
758
+ setInternalValue(selectedValue || (multi ? [] : null));
756
759
  }
757
- }, [firstValue, setSelectedOptions]); // Dependencies are properly set.
760
+ }, [firstValue, formValues[name]]);
758
761
  /**
759
762
  * Handles selection of an option. Adds or removes the option from selectedOptions based on
760
763
  * multi-selection and maxSelection criteria.
@@ -763,49 +766,35 @@ var InputList = function InputList(_a) {
763
766
  var handleSelectOption = function handleSelectOption(option) {
764
767
  var updatedSelections;
765
768
  if (multi) {
766
- // Ensure selectedOptions is treated as an array
767
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
768
- var alreadySelected = selectedArray.some(function (opt) {
769
+ var alreadySelected = (selectedOptions || []).some(function (opt) {
769
770
  return opt.id === option.id;
770
771
  });
771
- updatedSelections = alreadySelected ? selectedArray.filter(function (opt) {
772
+ updatedSelections = alreadySelected ? (selectedOptions || []).filter(function (opt) {
772
773
  return opt.id !== option.id;
773
- }) : __spreadArray(__spreadArray([], selectedArray, true), [option], false);
774
+ }) : __spreadArray(__spreadArray([], selectedOptions || [], true), [option], false);
774
775
  if (!alreadySelected && maxSelection && updatedSelections.length >= maxSelection) {
775
776
  setIsDropdownVisible(false);
776
777
  }
778
+ setInternalValue(updatedSelections);
777
779
  setFormValue(name, updatedSelections);
778
780
  } else {
779
- // Handle single-selection case
780
781
  updatedSelections = option;
782
+ setInternalValue(option);
781
783
  setFormValue(name, option);
782
784
  if (closeOnSelect) setIsDropdownVisible(false);
783
785
  }
784
- // Trigger onChange callback with the updated selection
785
786
  if (onChange) {
786
787
  onChange(updatedSelections);
787
788
  }
788
789
  };
789
- var isItemDisabled = function isItemDisabled(option) {
790
- if (!multi) return false; // Disable check only applies for multi-select
791
- // Ensure selectedOptions is treated as an array
792
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
793
- return maxSelection && selectedArray.length >= maxSelection && !selectedArray.some(function (opt) {
794
- return opt.id === option.id;
795
- });
796
- };
797
790
  /**
798
- * Renders selected options as a comma-separated string or the placeholder if none selected.
799
- * @returns {string} - The display text for selected options or placeholder.
800
- */
791
+ * Renders selected options as a comma-separated string or the placeholder if none selected.
792
+ * @returns {string} - The display text for selected options or placeholder.
793
+ */
801
794
  var renderSelectedText = function renderSelectedText() {
802
- if (multi) {
803
- // Ensure selectedOptions is treated as an array
804
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
805
- return selectedArray.map(function (opt) {
806
- return opt.label;
807
- }).join(', ') || placeholder;
808
- }
795
+ if (multi) return (selectedOptions || []).map(function (opt) {
796
+ return opt.label;
797
+ }).join(", ") || placeholder;
809
798
  return (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.label) || placeholder;
810
799
  };
811
800
  /**
@@ -824,6 +813,11 @@ var InputList = function InputList(_a) {
824
813
  if (isDropdownVisible) setIsDropdownVisible(false);
825
814
  }, [isDropdownVisible]);
826
815
  // Conditionally render item as disabled if max selection reached and item is unselected
816
+ var isItemDisabled = function isItemDisabled(option) {
817
+ return multi && maxSelection && selectedOptions.length >= maxSelection && !selectedOptions.some(function (opt) {
818
+ return opt.id === option.id;
819
+ });
820
+ };
827
821
  return /*#__PURE__*/React.createElement(View, {
828
822
  style: [styles$4.container, style]
829
823
  }, /*#__PURE__*/React.createElement(Text$1, {
@@ -861,9 +855,9 @@ var InputList = function InputList(_a) {
861
855
  },
862
856
  renderItem: function renderItem(_a) {
863
857
  var item = _a.item;
864
- var isSelected = multi ? Array.isArray(selectedOptions) && selectedOptions.some(function (opt) {
858
+ var isSelected = multi ? selectedOptions.some(function (opt) {
865
859
  return opt.id === item.id;
866
- }) : selectedOptions && 'id' in selectedOptions && selectedOptions.id === item.id;
860
+ }) : (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.id) === item.id;
867
861
  var isDisabled = isItemDisabled(item);
868
862
  return /*#__PURE__*/React.createElement(TouchableOpacity, {
869
863
  onPress: function onPress() {
@@ -901,12 +895,12 @@ var styles$4 = StyleSheet.create({
901
895
  borderRadius: 5
902
896
  },
903
897
  dropdownContainer: {
904
- position: 'absolute',
905
- top: '30%',
898
+ position: "absolute",
899
+ top: "30%",
906
900
  // Center the dropdown vertically
907
- alignSelf: 'center',
908
- width: '90%',
909
- backgroundColor: '#fff',
901
+ alignSelf: "center",
902
+ width: "90%",
903
+ backgroundColor: "#fff",
910
904
  borderRadius: 8,
911
905
  elevation: 5,
912
906
  paddingVertical: 10
@@ -915,16 +909,16 @@ var styles$4 = StyleSheet.create({
915
909
  padding: 12
916
910
  },
917
911
  disabledText: {
918
- color: '#999'
912
+ color: "#999"
919
913
  },
920
914
  separator: {
921
915
  height: 1,
922
- backgroundColor: '#ddd',
916
+ backgroundColor: "#ddd",
923
917
  marginHorizontal: 8
924
918
  },
925
919
  overlay: {
926
920
  flex: 1,
927
- backgroundColor: 'rgba(0,0,0,0.3)'
921
+ backgroundColor: "rgba(0,0,0,0.3)"
928
922
  }
929
923
  });
930
924