aport-tools 4.4.18 → 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.18 | ISC */
1
+ /*! aport-tools v4.4.20 | ISC */
2
2
  'use strict';
3
3
 
4
4
  var React = require('react');
@@ -751,99 +751,95 @@ var InputList = function InputList(_a) {
751
751
  closeOnSelect = _h === void 0 ? true : _h,
752
752
  maxSelection = _a.maxSelection,
753
753
  onChange = _a.onChange;
754
- var _j = useFormContext(),
755
- formValues = _j.formValues,
756
- setFormValue = _j.setFormValue,
754
+ var _j = useFormContext();
755
+ _j.formValues;
756
+ var setFormValue = _j.setFormValue,
757
757
  formErrors = _j.errors;
758
758
  var _k = React.useState(false),
759
759
  isDropdownVisible = _k[0],
760
760
  setIsDropdownVisible = _k[1];
761
- var selectedOptions = formValues[name] || (multi ? [] : null);
762
- var sortedOptions = sortBy ? __spreadArray([], options, true).sort(function (a, b) {
763
- return a[sortBy] > b[sortBy] ? 1 : -1;
764
- }) : 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]);
765
767
  var theme = React.useContext(aportThemes.ThemeContext).theme;
766
768
  var colors = theme.colors;
767
- var _l = React.useState();
768
- _l[0];
769
- var setInternalValue = _l[1];
770
- // Initialize the internal value based on `firstValue` or `formValues`
769
+ // Initialize selected options based on firstValue
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
777
+ var _l = React.useState(multi ? initialSelections : initialSelections[0] || null),
778
+ selectedOptions = _l[0],
779
+ setSelectedOptions = _l[1];
780
+ // Update form value on mount if firstValue is provided
781
+ // Update form value and internal state on mount or `firstValue` change
771
782
  React.useEffect(function () {
772
- if (firstValue) {
773
- setInternalValue(firstValue);
774
- setFormValue(name, firstValue);
775
- } else {
776
- setInternalValue(formValues[name] || "");
783
+ if (firstValue && selectedOptions !== firstValue) {
784
+ setSelectedOptions(multi ? initialSelections : initialSelections[0] || null);
785
+ setFormValue(name, multi ? initialSelections : initialSelections[0] || null);
777
786
  }
778
- }, [firstValue, formValues[name]]);
779
- /**
780
- * Handles selection of an option. Adds or removes the option from selectedOptions based on
781
- * multi-selection and maxSelection criteria.
782
- * @param {Option} option - The selected option object.
783
- */
787
+ }, [firstValue, initialSelections, multi, name, selectedOptions, setFormValue]);
788
+ // Handle option selection
784
789
  var handleSelectOption = function handleSelectOption(option) {
785
790
  var updatedSelections;
786
791
  if (multi) {
787
- // Ensure selectedOptions is treated as an array
788
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
789
- var alreadySelected = selectedArray.some(function (opt) {
792
+ var alreadySelected = Array.isArray(selectedOptions) && selectedOptions.some(function (opt) {
790
793
  return opt.id === option.id;
791
794
  });
792
- updatedSelections = alreadySelected ? selectedArray.filter(function (opt) {
795
+ updatedSelections = alreadySelected ? selectedOptions.filter(function (opt) {
793
796
  return opt.id !== option.id;
794
- }) : __spreadArray(__spreadArray([], selectedArray, true), [option], false);
797
+ }) : Array.isArray(selectedOptions) ? __spreadArray(__spreadArray([], selectedOptions, true), [option], false) : [option];
795
798
  if (!alreadySelected && maxSelection && updatedSelections.length >= maxSelection) {
796
799
  setIsDropdownVisible(false);
797
800
  }
798
- setFormValue(name, updatedSelections);
799
801
  } else {
800
- // Handle single-selection case
801
802
  updatedSelections = option;
802
- setFormValue(name, option);
803
803
  if (closeOnSelect) setIsDropdownVisible(false);
804
804
  }
805
- // Trigger onChange callback with the updated selection
805
+ setSelectedOptions(updatedSelections);
806
+ setFormValue(name, updatedSelections);
806
807
  if (onChange) {
807
808
  onChange(updatedSelections);
808
809
  }
809
810
  };
811
+ // Check if an option should be disabled
810
812
  var isItemDisabled = function isItemDisabled(option) {
811
- if (!multi) return false; // Disable check only applies for multi-select
812
- // Ensure selectedOptions is treated as an array
813
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
814
- 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) {
815
815
  return opt.id === option.id;
816
816
  });
817
817
  };
818
+ // Render selected text
818
819
  var renderSelectedText = function renderSelectedText() {
819
820
  if (multi) {
820
- // Ensure selectedOptions is treated as an array
821
821
  var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
822
822
  return selectedArray.map(function (opt) {
823
823
  return opt.label;
824
- }).join(', ') || placeholder;
824
+ }).join(", ") || placeholder;
825
825
  }
826
826
  return (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.label) || placeholder;
827
827
  };
828
- /**
829
- * Renders selected options as a comma-separated string or the placeholder if none selected.
830
- * @returns {string} - The display text for selected options or placeholder.
831
- */
832
- /**
833
- * Toggles dropdown visibility. Disables toggle if the component is disabled.
834
- */
828
+ // Toggle dropdown visibility
835
829
  var toggleDropdown = function toggleDropdown() {
836
830
  if (!disabled) {
837
831
  setIsDropdownVisible(!isDropdownVisible);
838
832
  if (!isDropdownVisible) reactNative.Keyboard.dismiss();
839
833
  }
840
834
  };
841
- /**
842
- * Closes the dropdown when pressing outside.
843
- */
835
+ // Close dropdown
844
836
  var handleCloseDropdown = React.useCallback(function () {
845
837
  if (isDropdownVisible) setIsDropdownVisible(false);
846
838
  }, [isDropdownVisible]);
839
+ /**
840
+ * Renders selected options as a comma-separated string or the placeholder if none selected.
841
+ * @returns {string} - The display text for selected options or placeholder.
842
+ */
847
843
  // Conditionally render item as disabled if max selection reached and item is unselected
848
844
  return /*#__PURE__*/React.createElement(reactNative.View, {
849
845
  style: [styles$4.container, style]