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.esm.js +45 -49
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +44 -48
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
/*! aport-tools v4.4.
|
2
|
-
import React, { useContext, useState, createContext, useEffect,
|
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';
|
@@ -730,99 +730,95 @@ var InputList = function InputList(_a) {
|
|
730
730
|
closeOnSelect = _h === void 0 ? true : _h,
|
731
731
|
maxSelection = _a.maxSelection,
|
732
732
|
onChange = _a.onChange;
|
733
|
-
var _j = useFormContext()
|
734
|
-
|
735
|
-
setFormValue = _j.setFormValue,
|
733
|
+
var _j = useFormContext();
|
734
|
+
_j.formValues;
|
735
|
+
var setFormValue = _j.setFormValue,
|
736
736
|
formErrors = _j.errors;
|
737
737
|
var _k = useState(false),
|
738
738
|
isDropdownVisible = _k[0],
|
739
739
|
setIsDropdownVisible = _k[1];
|
740
|
-
|
741
|
-
var sortedOptions =
|
742
|
-
return
|
743
|
-
|
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]);
|
744
746
|
var theme = useContext(ThemeContext).theme;
|
745
747
|
var colors = theme.colors;
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
748
|
+
// Initialize selected options based on firstValue
|
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
|
756
|
+
var _l = useState(multi ? initialSelections : initialSelections[0] || null),
|
757
|
+
selectedOptions = _l[0],
|
758
|
+
setSelectedOptions = _l[1];
|
759
|
+
// Update form value on mount if firstValue is provided
|
760
|
+
// Update form value and internal state on mount or `firstValue` change
|
750
761
|
useEffect(function () {
|
751
|
-
if (firstValue) {
|
752
|
-
|
753
|
-
setFormValue(name,
|
754
|
-
} else {
|
755
|
-
setInternalValue(formValues[name] || "");
|
762
|
+
if (firstValue && selectedOptions !== firstValue) {
|
763
|
+
setSelectedOptions(multi ? initialSelections : initialSelections[0] || null);
|
764
|
+
setFormValue(name, multi ? initialSelections : initialSelections[0] || null);
|
756
765
|
}
|
757
|
-
}, [firstValue,
|
758
|
-
|
759
|
-
* Handles selection of an option. Adds or removes the option from selectedOptions based on
|
760
|
-
* multi-selection and maxSelection criteria.
|
761
|
-
* @param {Option} option - The selected option object.
|
762
|
-
*/
|
766
|
+
}, [firstValue, initialSelections, multi, name, selectedOptions, setFormValue]);
|
767
|
+
// Handle option selection
|
763
768
|
var handleSelectOption = function handleSelectOption(option) {
|
764
769
|
var updatedSelections;
|
765
770
|
if (multi) {
|
766
|
-
|
767
|
-
var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
|
768
|
-
var alreadySelected = selectedArray.some(function (opt) {
|
771
|
+
var alreadySelected = Array.isArray(selectedOptions) && selectedOptions.some(function (opt) {
|
769
772
|
return opt.id === option.id;
|
770
773
|
});
|
771
|
-
updatedSelections = alreadySelected ?
|
774
|
+
updatedSelections = alreadySelected ? selectedOptions.filter(function (opt) {
|
772
775
|
return opt.id !== option.id;
|
773
|
-
}) : __spreadArray(__spreadArray([],
|
776
|
+
}) : Array.isArray(selectedOptions) ? __spreadArray(__spreadArray([], selectedOptions, true), [option], false) : [option];
|
774
777
|
if (!alreadySelected && maxSelection && updatedSelections.length >= maxSelection) {
|
775
778
|
setIsDropdownVisible(false);
|
776
779
|
}
|
777
|
-
setFormValue(name, updatedSelections);
|
778
780
|
} else {
|
779
|
-
// Handle single-selection case
|
780
781
|
updatedSelections = option;
|
781
|
-
setFormValue(name, option);
|
782
782
|
if (closeOnSelect) setIsDropdownVisible(false);
|
783
783
|
}
|
784
|
-
|
784
|
+
setSelectedOptions(updatedSelections);
|
785
|
+
setFormValue(name, updatedSelections);
|
785
786
|
if (onChange) {
|
786
787
|
onChange(updatedSelections);
|
787
788
|
}
|
788
789
|
};
|
790
|
+
// Check if an option should be disabled
|
789
791
|
var isItemDisabled = function isItemDisabled(option) {
|
790
|
-
if (!multi) return false;
|
791
|
-
|
792
|
-
var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
|
793
|
-
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) {
|
794
794
|
return opt.id === option.id;
|
795
795
|
});
|
796
796
|
};
|
797
|
+
// Render selected text
|
797
798
|
var renderSelectedText = function renderSelectedText() {
|
798
799
|
if (multi) {
|
799
|
-
// Ensure selectedOptions is treated as an array
|
800
800
|
var selectedArray = Array.isArray(selectedOptions) ? selectedOptions : [];
|
801
801
|
return selectedArray.map(function (opt) {
|
802
802
|
return opt.label;
|
803
|
-
}).join(
|
803
|
+
}).join(", ") || placeholder;
|
804
804
|
}
|
805
805
|
return (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.label) || placeholder;
|
806
806
|
};
|
807
|
-
|
808
|
-
* Renders selected options as a comma-separated string or the placeholder if none selected.
|
809
|
-
* @returns {string} - The display text for selected options or placeholder.
|
810
|
-
*/
|
811
|
-
/**
|
812
|
-
* Toggles dropdown visibility. Disables toggle if the component is disabled.
|
813
|
-
*/
|
807
|
+
// Toggle dropdown visibility
|
814
808
|
var toggleDropdown = function toggleDropdown() {
|
815
809
|
if (!disabled) {
|
816
810
|
setIsDropdownVisible(!isDropdownVisible);
|
817
811
|
if (!isDropdownVisible) Keyboard.dismiss();
|
818
812
|
}
|
819
813
|
};
|
820
|
-
|
821
|
-
* Closes the dropdown when pressing outside.
|
822
|
-
*/
|
814
|
+
// Close dropdown
|
823
815
|
var handleCloseDropdown = useCallback(function () {
|
824
816
|
if (isDropdownVisible) setIsDropdownVisible(false);
|
825
817
|
}, [isDropdownVisible]);
|
818
|
+
/**
|
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
|
+
*/
|
826
822
|
// Conditionally render item as disabled if max selection reached and item is unselected
|
827
823
|
return /*#__PURE__*/React.createElement(View, {
|
828
824
|
style: [styles$4.container, style]
|