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.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! aport-tools v4.4.19 | ISC */
1
+ /*! aport-tools v4.4.20 | ISC */
2
2
  'use strict';
3
3
 
4
4
  var React = require('react');
@@ -758,72 +758,81 @@ var InputList = function InputList(_a) {
758
758
  var _k = React.useState(false),
759
759
  isDropdownVisible = _k[0],
760
760
  setIsDropdownVisible = _k[1];
761
- var sortedOptions = sortBy ? __spreadArray([], options, true).sort(function (a, b) {
762
- return a[sortBy] > b[sortBy] ? 1 : -1;
763
- }) : options;
761
+ // Memoize sorted options for performance
762
+ var sortedOptions = React.useMemo(function () {
763
+ return sortBy ? __spreadArray([], options, true).sort(function (a, b) {
764
+ return a[sortBy] > b[sortBy] ? 1 : -1;
765
+ }) : options;
766
+ }, [options, sortBy]);
764
767
  var theme = React.useContext(aportThemes.ThemeContext).theme;
765
768
  var colors = theme.colors;
766
769
  // Initialize selected options based on firstValue
767
- var initialSelections = options.filter(function (opt) {
768
- return firstValue.includes(opt.value);
769
- });
770
+ // Filter initial selections from options based on `firstValue`
771
+ var initialSelections = React.useMemo(function () {
772
+ return options.filter(function (opt) {
773
+ return firstValue.includes(opt.value);
774
+ });
775
+ }, [options, firstValue]);
776
+ // State for selected options
770
777
  var _l = React.useState(multi ? initialSelections : initialSelections[0] || null),
771
- selectedOptions = _l[0];
772
- _l[1];
778
+ selectedOptions = _l[0],
779
+ setSelectedOptions = _l[1];
773
780
  // Update form value on mount if firstValue is provided
781
+ // Update form value and internal state on mount or `firstValue` change
774
782
  React.useEffect(function () {
775
- setFormValue(name, multi ? initialSelections : initialSelections[0] || null);
776
- }, [firstValue]);
783
+ if (firstValue && selectedOptions !== firstValue) {
784
+ setSelectedOptions(multi ? initialSelections : initialSelections[0] || null);
785
+ setFormValue(name, multi ? initialSelections : initialSelections[0] || null);
786
+ }
787
+ }, [firstValue, initialSelections, multi, name, selectedOptions, setFormValue]);
788
+ // Handle option selection
777
789
  var handleSelectOption = function handleSelectOption(option) {
778
790
  var updatedSelections;
779
791
  if (multi) {
780
- // Ensure selectedOptions is treated as an array
781
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
782
- var alreadySelected = selectedArray.some(function (opt) {
792
+ var alreadySelected = Array.isArray(selectedOptions) && selectedOptions.some(function (opt) {
783
793
  return opt.id === option.id;
784
794
  });
785
- updatedSelections = alreadySelected ? selectedArray.filter(function (opt) {
795
+ updatedSelections = alreadySelected ? selectedOptions.filter(function (opt) {
786
796
  return opt.id !== option.id;
787
- }) : __spreadArray(__spreadArray([], selectedArray, true), [option], false);
797
+ }) : Array.isArray(selectedOptions) ? __spreadArray(__spreadArray([], selectedOptions, true), [option], false) : [option];
788
798
  if (!alreadySelected && maxSelection && updatedSelections.length >= maxSelection) {
789
799
  setIsDropdownVisible(false);
790
800
  }
791
- setFormValue(name, updatedSelections);
792
801
  } else {
793
- // Handle single-selection case
794
802
  updatedSelections = option;
795
- setFormValue(name, option);
796
803
  if (closeOnSelect) setIsDropdownVisible(false);
797
804
  }
798
- // Trigger onChange callback with the updated selection
805
+ setSelectedOptions(updatedSelections);
806
+ setFormValue(name, updatedSelections);
799
807
  if (onChange) {
800
808
  onChange(updatedSelections);
801
809
  }
802
810
  };
811
+ // Check if an option should be disabled
803
812
  var isItemDisabled = function isItemDisabled(option) {
804
- if (!multi) return false; // Disable check only applies for multi-select
805
- // Ensure selectedOptions is treated as an array
806
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
807
- return maxSelection && selectedArray.length >= maxSelection && !selectedArray.some(function (opt) {
813
+ if (!multi) return false;
814
+ return maxSelection && Array.isArray(selectedOptions) && selectedOptions.length >= maxSelection && !selectedOptions.some(function (opt) {
808
815
  return opt.id === option.id;
809
816
  });
810
817
  };
818
+ // Render selected text
811
819
  var renderSelectedText = function renderSelectedText() {
812
820
  if (multi) {
813
- // Ensure selectedOptions is treated as an array
814
821
  var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
815
822
  return selectedArray.map(function (opt) {
816
823
  return opt.label;
817
- }).join(', ') || placeholder;
824
+ }).join(", ") || placeholder;
818
825
  }
819
826
  return (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.label) || placeholder;
820
827
  };
828
+ // Toggle dropdown visibility
821
829
  var toggleDropdown = function toggleDropdown() {
822
830
  if (!disabled) {
823
831
  setIsDropdownVisible(!isDropdownVisible);
824
832
  if (!isDropdownVisible) reactNative.Keyboard.dismiss();
825
833
  }
826
834
  };
835
+ // Close dropdown
827
836
  var handleCloseDropdown = React.useCallback(function () {
828
837
  if (isDropdownVisible) setIsDropdownVisible(false);
829
838
  }, [isDropdownVisible]);