aport-tools 4.4.19 → 4.4.20

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.esm.js CHANGED
@@ -1,5 +1,5 @@
1
- /*! aport-tools v4.4.19 | ISC */
2
- import React, { useContext, useState, createContext, useEffect, useCallback, useMemo } from 'react';
1
+ /*! aport-tools v4.4.20 | 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';
@@ -737,72 +737,81 @@ var InputList = function InputList(_a) {
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
+ // Memoize sorted options for performance
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
748
  // Initialize selected options based on firstValue
746
- var initialSelections = options.filter(function (opt) {
747
- return firstValue.includes(opt.value);
748
- });
749
+ // Filter initial selections from options based on `firstValue`
750
+ var initialSelections = useMemo(function () {
751
+ return options.filter(function (opt) {
752
+ return firstValue.includes(opt.value);
753
+ });
754
+ }, [options, firstValue]);
755
+ // State for selected options
749
756
  var _l = useState(multi ? initialSelections : initialSelections[0] || null),
750
- selectedOptions = _l[0];
751
- _l[1];
757
+ selectedOptions = _l[0],
758
+ setSelectedOptions = _l[1];
752
759
  // Update form value on mount if firstValue is provided
760
+ // Update form value and internal state on mount or `firstValue` change
753
761
  useEffect(function () {
754
- setFormValue(name, multi ? initialSelections : initialSelections[0] || null);
755
- }, [firstValue]);
762
+ if (firstValue && selectedOptions !== firstValue) {
763
+ setSelectedOptions(multi ? initialSelections : initialSelections[0] || null);
764
+ setFormValue(name, multi ? initialSelections : initialSelections[0] || null);
765
+ }
766
+ }, [firstValue, initialSelections, multi, name, selectedOptions, setFormValue]);
767
+ // Handle option selection
756
768
  var handleSelectOption = function handleSelectOption(option) {
757
769
  var updatedSelections;
758
770
  if (multi) {
759
- // Ensure selectedOptions is treated as an array
760
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
761
- var alreadySelected = selectedArray.some(function (opt) {
771
+ var alreadySelected = Array.isArray(selectedOptions) && selectedOptions.some(function (opt) {
762
772
  return opt.id === option.id;
763
773
  });
764
- updatedSelections = alreadySelected ? selectedArray.filter(function (opt) {
774
+ updatedSelections = alreadySelected ? selectedOptions.filter(function (opt) {
765
775
  return opt.id !== option.id;
766
- }) : __spreadArray(__spreadArray([], selectedArray, true), [option], false);
776
+ }) : Array.isArray(selectedOptions) ? __spreadArray(__spreadArray([], selectedOptions, true), [option], false) : [option];
767
777
  if (!alreadySelected && maxSelection && updatedSelections.length >= maxSelection) {
768
778
  setIsDropdownVisible(false);
769
779
  }
770
- setFormValue(name, updatedSelections);
771
780
  } else {
772
- // Handle single-selection case
773
781
  updatedSelections = option;
774
- setFormValue(name, option);
775
782
  if (closeOnSelect) setIsDropdownVisible(false);
776
783
  }
777
- // Trigger onChange callback with the updated selection
784
+ setSelectedOptions(updatedSelections);
785
+ setFormValue(name, updatedSelections);
778
786
  if (onChange) {
779
787
  onChange(updatedSelections);
780
788
  }
781
789
  };
790
+ // Check if an option should be disabled
782
791
  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) {
792
+ if (!multi) return false;
793
+ return maxSelection && Array.isArray(selectedOptions) && selectedOptions.length >= maxSelection && !selectedOptions.some(function (opt) {
787
794
  return opt.id === option.id;
788
795
  });
789
796
  };
797
+ // Render selected text
790
798
  var renderSelectedText = function renderSelectedText() {
791
799
  if (multi) {
792
- // Ensure selectedOptions is treated as an array
793
800
  var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
794
801
  return selectedArray.map(function (opt) {
795
802
  return opt.label;
796
- }).join(', ') || placeholder;
803
+ }).join(", ") || placeholder;
797
804
  }
798
805
  return (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.label) || placeholder;
799
806
  };
807
+ // Toggle dropdown visibility
800
808
  var toggleDropdown = function toggleDropdown() {
801
809
  if (!disabled) {
802
810
  setIsDropdownVisible(!isDropdownVisible);
803
811
  if (!isDropdownVisible) Keyboard.dismiss();
804
812
  }
805
813
  };
814
+ // Close dropdown
806
815
  var handleCloseDropdown = useCallback(function () {
807
816
  if (isDropdownVisible) setIsDropdownVisible(false);
808
817
  }, [isDropdownVisible]);