aport-tools 4.4.19 → 4.4.21

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