aport-tools 4.4.17 → 4.4.19

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[] | any;
17
+ firstValue?: 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.17 | ISC */
2
- import React, { useContext, useState, createContext, useEffect, useMemo, useCallback } from 'react';
1
+ /*! aport-tools v4.4.19 | 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';
@@ -730,94 +730,87 @@ 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
- formValues = _j.formValues,
735
- setFormValue = _j.setFormValue,
733
+ var _j = useFormContext();
734
+ _j.formValues;
735
+ var 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 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]);
740
+ var sortedOptions = sortBy ? __spreadArray([], options, true).sort(function (a, b) {
741
+ return a[sortBy] > b[sortBy] ? 1 : -1;
742
+ }) : options;
746
743
  var theme = useContext(ThemeContext).theme;
747
744
  var colors = theme.colors;
748
- var _l = useState([]);
749
- _l[0];
750
- var setInternalValue = _l[1];
751
- // Initialize the internal value based on `firstValue` or `formValues`
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
752
753
  useEffect(function () {
753
- var selectedValue = formValues[name];
754
- if (firstValue) {
755
- setInternalValue(firstValue);
756
- setFormValue(name, firstValue);
757
- } else {
758
- setInternalValue(selectedValue || (multi ? [] : null));
759
- }
760
- }, [firstValue, formValues[name]]);
761
- /**
762
- * Handles selection of an option. Adds or removes the option from selectedOptions based on
763
- * multi-selection and maxSelection criteria.
764
- * @param {Option} option - The selected option object.
765
- */
754
+ setFormValue(name, multi ? initialSelections : initialSelections[0] || null);
755
+ }, [firstValue]);
766
756
  var handleSelectOption = function handleSelectOption(option) {
767
757
  var updatedSelections;
768
758
  if (multi) {
769
- var alreadySelected = (selectedOptions || []).some(function (opt) {
759
+ // Ensure selectedOptions is treated as an array
760
+ var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
761
+ var alreadySelected = selectedArray.some(function (opt) {
770
762
  return opt.id === option.id;
771
763
  });
772
- updatedSelections = alreadySelected ? (selectedOptions || []).filter(function (opt) {
764
+ updatedSelections = alreadySelected ? selectedArray.filter(function (opt) {
773
765
  return opt.id !== option.id;
774
- }) : __spreadArray(__spreadArray([], selectedOptions || [], true), [option], false);
766
+ }) : __spreadArray(__spreadArray([], selectedArray, true), [option], false);
775
767
  if (!alreadySelected && maxSelection && updatedSelections.length >= maxSelection) {
776
768
  setIsDropdownVisible(false);
777
769
  }
778
- setInternalValue(updatedSelections);
779
770
  setFormValue(name, updatedSelections);
780
771
  } else {
772
+ // Handle single-selection case
781
773
  updatedSelections = option;
782
- setInternalValue(option);
783
774
  setFormValue(name, option);
784
775
  if (closeOnSelect) setIsDropdownVisible(false);
785
776
  }
777
+ // Trigger onChange callback with the updated selection
786
778
  if (onChange) {
787
779
  onChange(updatedSelections);
788
780
  }
789
781
  };
790
- /**
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
- */
782
+ var isItemDisabled = function isItemDisabled(option) {
783
+ if (!multi) return false; // Disable check only applies for multi-select
784
+ // Ensure selectedOptions is treated as an array
785
+ var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
786
+ return maxSelection && selectedArray.length >= maxSelection && !selectedArray.some(function (opt) {
787
+ return opt.id === option.id;
788
+ });
789
+ };
794
790
  var renderSelectedText = function renderSelectedText() {
795
- if (multi) return (selectedOptions || []).map(function (opt) {
796
- return opt.label;
797
- }).join(", ") || placeholder;
791
+ if (multi) {
792
+ // Ensure selectedOptions is treated as an array
793
+ var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
794
+ return selectedArray.map(function (opt) {
795
+ return opt.label;
796
+ }).join(', ') || placeholder;
797
+ }
798
798
  return (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.label) || placeholder;
799
799
  };
800
- /**
801
- * Toggles dropdown visibility. Disables toggle if the component is disabled.
802
- */
803
800
  var toggleDropdown = function toggleDropdown() {
804
801
  if (!disabled) {
805
802
  setIsDropdownVisible(!isDropdownVisible);
806
803
  if (!isDropdownVisible) Keyboard.dismiss();
807
804
  }
808
805
  };
809
- /**
810
- * Closes the dropdown when pressing outside.
811
- */
812
806
  var handleCloseDropdown = useCallback(function () {
813
807
  if (isDropdownVisible) setIsDropdownVisible(false);
814
808
  }, [isDropdownVisible]);
809
+ /**
810
+ * Renders selected options as a comma-separated string or the placeholder if none selected.
811
+ * @returns {string} - The display text for selected options or placeholder.
812
+ */
815
813
  // 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
- };
821
814
  return /*#__PURE__*/React.createElement(View, {
822
815
  style: [styles$4.container, style]
823
816
  }, /*#__PURE__*/React.createElement(Text$1, {
@@ -855,9 +848,9 @@ var InputList = function InputList(_a) {
855
848
  },
856
849
  renderItem: function renderItem(_a) {
857
850
  var item = _a.item;
858
- var isSelected = multi ? selectedOptions.some(function (opt) {
851
+ var isSelected = multi ? Array.isArray(selectedOptions) && selectedOptions.some(function (opt) {
859
852
  return opt.id === item.id;
860
- }) : (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.id) === item.id;
853
+ }) : selectedOptions && 'id' in selectedOptions && selectedOptions.id === item.id;
861
854
  var isDisabled = isItemDisabled(item);
862
855
  return /*#__PURE__*/React.createElement(TouchableOpacity, {
863
856
  onPress: function onPress() {
@@ -895,12 +888,12 @@ var styles$4 = StyleSheet.create({
895
888
  borderRadius: 5
896
889
  },
897
890
  dropdownContainer: {
898
- position: "absolute",
899
- top: "30%",
891
+ position: 'absolute',
892
+ top: '30%',
900
893
  // Center the dropdown vertically
901
- alignSelf: "center",
902
- width: "90%",
903
- backgroundColor: "#fff",
894
+ alignSelf: 'center',
895
+ width: '90%',
896
+ backgroundColor: '#fff',
904
897
  borderRadius: 8,
905
898
  elevation: 5,
906
899
  paddingVertical: 10
@@ -909,16 +902,16 @@ var styles$4 = StyleSheet.create({
909
902
  padding: 12
910
903
  },
911
904
  disabledText: {
912
- color: "#999"
905
+ color: '#999'
913
906
  },
914
907
  separator: {
915
908
  height: 1,
916
- backgroundColor: "#ddd",
909
+ backgroundColor: '#ddd',
917
910
  marginHorizontal: 8
918
911
  },
919
912
  overlay: {
920
913
  flex: 1,
921
- backgroundColor: "rgba(0,0,0,0.3)"
914
+ backgroundColor: 'rgba(0,0,0,0.3)'
922
915
  }
923
916
  });
924
917