aport-tools 4.4.15 → 4.4.17

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.15 | ISC */
1
+ /*! aport-tools v4.4.17 | ISC */
2
2
  'use strict';
3
3
 
4
4
  var React = require('react');
@@ -751,31 +751,34 @@ 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
- _j.formValues;
756
- var setFormValue = _j.setFormValue,
754
+ var _j = useFormContext(),
755
+ formValues = _j.formValues,
756
+ 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 sortedOptions = sortBy ? __spreadArray([], options, true).sort(function (a, b) {
762
- return a[sortBy] > b[sortBy] ? 1 : -1;
763
- }) : options;
761
+ var selectedOptions = firstValue || formValues[name] || (multi ? [] : null);
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
- // Initialize selected options based on firstValue
767
- var initialSelections = options.filter(function (opt) {
768
- return firstValue.includes(opt.value);
769
- });
770
- var _l = React.useState(multi ? initialSelections : initialSelections[0] || null),
771
- selectedOptions = _l[0],
772
- setSelectedOptions = _l[1];
773
- // Update form value on mount if firstValue is provided
769
+ var _l = React.useState([]);
770
+ _l[0];
771
+ var setInternalValue = _l[1];
772
+ // Initialize the internal value based on `firstValue` or `formValues`
774
773
  React.useEffect(function () {
775
- if (Array.isArray(firstValue) && firstValue.length > 0) {
776
- setSelectedOptions(firstValue);
774
+ var selectedValue = formValues[name];
775
+ if (firstValue) {
776
+ setInternalValue(firstValue);
777
+ setFormValue(name, firstValue);
778
+ } else {
779
+ setInternalValue(selectedValue || (multi ? [] : null));
777
780
  }
778
- }, [firstValue, setSelectedOptions]); // Dependencies are properly set.
781
+ }, [firstValue, formValues[name]]);
779
782
  /**
780
783
  * Handles selection of an option. Adds or removes the option from selectedOptions based on
781
784
  * multi-selection and maxSelection criteria.
@@ -784,49 +787,35 @@ var InputList = function InputList(_a) {
784
787
  var handleSelectOption = function handleSelectOption(option) {
785
788
  var updatedSelections;
786
789
  if (multi) {
787
- // Ensure selectedOptions is treated as an array
788
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
789
- var alreadySelected = selectedArray.some(function (opt) {
790
+ var alreadySelected = (selectedOptions || []).some(function (opt) {
790
791
  return opt.id === option.id;
791
792
  });
792
- updatedSelections = alreadySelected ? selectedArray.filter(function (opt) {
793
+ updatedSelections = alreadySelected ? (selectedOptions || []).filter(function (opt) {
793
794
  return opt.id !== option.id;
794
- }) : __spreadArray(__spreadArray([], selectedArray, true), [option], false);
795
+ }) : __spreadArray(__spreadArray([], selectedOptions || [], true), [option], false);
795
796
  if (!alreadySelected && maxSelection && updatedSelections.length >= maxSelection) {
796
797
  setIsDropdownVisible(false);
797
798
  }
799
+ setInternalValue(updatedSelections);
798
800
  setFormValue(name, updatedSelections);
799
801
  } else {
800
- // Handle single-selection case
801
802
  updatedSelections = option;
803
+ setInternalValue(option);
802
804
  setFormValue(name, option);
803
805
  if (closeOnSelect) setIsDropdownVisible(false);
804
806
  }
805
- // Trigger onChange callback with the updated selection
806
807
  if (onChange) {
807
808
  onChange(updatedSelections);
808
809
  }
809
810
  };
810
- 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) {
815
- return opt.id === option.id;
816
- });
817
- };
818
811
  /**
819
- * Renders selected options as a comma-separated string or the placeholder if none selected.
820
- * @returns {string} - The display text for selected options or placeholder.
821
- */
812
+ * Renders selected options as a comma-separated string or the placeholder if none selected.
813
+ * @returns {string} - The display text for selected options or placeholder.
814
+ */
822
815
  var renderSelectedText = function renderSelectedText() {
823
- if (multi) {
824
- // Ensure selectedOptions is treated as an array
825
- var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
826
- return selectedArray.map(function (opt) {
827
- return opt.label;
828
- }).join(', ') || placeholder;
829
- }
816
+ if (multi) return (selectedOptions || []).map(function (opt) {
817
+ return opt.label;
818
+ }).join(", ") || placeholder;
830
819
  return (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.label) || placeholder;
831
820
  };
832
821
  /**
@@ -845,6 +834,11 @@ var InputList = function InputList(_a) {
845
834
  if (isDropdownVisible) setIsDropdownVisible(false);
846
835
  }, [isDropdownVisible]);
847
836
  // Conditionally render item as disabled if max selection reached and item is unselected
837
+ var isItemDisabled = function isItemDisabled(option) {
838
+ return multi && maxSelection && selectedOptions.length >= maxSelection && !selectedOptions.some(function (opt) {
839
+ return opt.id === option.id;
840
+ });
841
+ };
848
842
  return /*#__PURE__*/React.createElement(reactNative.View, {
849
843
  style: [styles$4.container, style]
850
844
  }, /*#__PURE__*/React.createElement(reactNative.Text, {
@@ -882,9 +876,9 @@ var InputList = function InputList(_a) {
882
876
  },
883
877
  renderItem: function renderItem(_a) {
884
878
  var item = _a.item;
885
- var isSelected = multi ? Array.isArray(selectedOptions) && selectedOptions.some(function (opt) {
879
+ var isSelected = multi ? selectedOptions.some(function (opt) {
886
880
  return opt.id === item.id;
887
- }) : selectedOptions && 'id' in selectedOptions && selectedOptions.id === item.id;
881
+ }) : (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.id) === item.id;
888
882
  var isDisabled = isItemDisabled(item);
889
883
  return /*#__PURE__*/React.createElement(reactNative.TouchableOpacity, {
890
884
  onPress: function onPress() {
@@ -922,12 +916,12 @@ var styles$4 = reactNative.StyleSheet.create({
922
916
  borderRadius: 5
923
917
  },
924
918
  dropdownContainer: {
925
- position: 'absolute',
926
- top: '30%',
919
+ position: "absolute",
920
+ top: "30%",
927
921
  // Center the dropdown vertically
928
- alignSelf: 'center',
929
- width: '90%',
930
- backgroundColor: '#fff',
922
+ alignSelf: "center",
923
+ width: "90%",
924
+ backgroundColor: "#fff",
931
925
  borderRadius: 8,
932
926
  elevation: 5,
933
927
  paddingVertical: 10
@@ -936,16 +930,16 @@ var styles$4 = reactNative.StyleSheet.create({
936
930
  padding: 12
937
931
  },
938
932
  disabledText: {
939
- color: '#999'
933
+ color: "#999"
940
934
  },
941
935
  separator: {
942
936
  height: 1,
943
- backgroundColor: '#ddd',
937
+ backgroundColor: "#ddd",
944
938
  marginHorizontal: 8
945
939
  },
946
940
  overlay: {
947
941
  flex: 1,
948
- backgroundColor: 'rgba(0,0,0,0.3)'
942
+ backgroundColor: "rgba(0,0,0,0.3)"
949
943
  }
950
944
  });
951
945