react-native-input-select 1.3.18 → 2.1.0
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/README.md +40 -95
- package/lib/commonjs/components/CheckBox/index.js +19 -13
- package/lib/commonjs/components/CheckBox/index.js.map +1 -1
- package/lib/commonjs/components/CustomModal/index.js +12 -16
- package/lib/commonjs/components/CustomModal/index.js.map +1 -1
- package/lib/commonjs/components/Dropdown/Dropdown.js +3 -2
- package/lib/commonjs/components/Dropdown/Dropdown.js.map +1 -1
- package/lib/commonjs/components/Dropdown/DropdownListItem.js +2 -11
- package/lib/commonjs/components/Dropdown/DropdownListItem.js.map +1 -1
- package/lib/commonjs/components/Dropdown/DropdownSelectedItemsView.js +14 -14
- package/lib/commonjs/components/Dropdown/DropdownSelectedItemsView.js.map +1 -1
- package/lib/commonjs/components/Input/index.js.map +1 -1
- package/lib/commonjs/components/{Dropdown → List}/DropdownFlatList.js +7 -28
- package/lib/commonjs/components/List/DropdownFlatList.js.map +1 -0
- package/lib/commonjs/components/{Dropdown → List}/DropdownSectionList.js +6 -24
- package/lib/commonjs/components/List/DropdownSectionList.js.map +1 -0
- package/lib/commonjs/hooks/index.js +61 -0
- package/lib/commonjs/hooks/index.js.map +1 -0
- package/lib/commonjs/hooks/use-index-of-selected-item.js +49 -0
- package/lib/commonjs/hooks/use-index-of-selected-item.js.map +1 -0
- package/lib/commonjs/hooks/use-modal.js +37 -0
- package/lib/commonjs/hooks/use-modal.js.map +1 -0
- package/lib/commonjs/hooks/use-search.js +58 -0
- package/lib/commonjs/hooks/use-search.js.map +1 -0
- package/lib/commonjs/hooks/use-select-all.js +70 -0
- package/lib/commonjs/hooks/use-select-all.js.map +1 -0
- package/lib/commonjs/hooks/use-selection-handler.js +62 -0
- package/lib/commonjs/hooks/use-selection-handler.js.map +1 -0
- package/lib/commonjs/index.js +120 -260
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/index.js +40 -5
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/module/components/CheckBox/index.js +19 -13
- package/lib/module/components/CheckBox/index.js.map +1 -1
- package/lib/module/components/CustomModal/index.js +12 -15
- package/lib/module/components/CustomModal/index.js.map +1 -1
- package/lib/module/components/Dropdown/Dropdown.js +3 -2
- package/lib/module/components/Dropdown/Dropdown.js.map +1 -1
- package/lib/module/components/Dropdown/DropdownListItem.js +2 -11
- package/lib/module/components/Dropdown/DropdownListItem.js.map +1 -1
- package/lib/module/components/Dropdown/DropdownSelectedItemsView.js +14 -14
- package/lib/module/components/Dropdown/DropdownSelectedItemsView.js.map +1 -1
- package/lib/module/components/Input/index.js.map +1 -1
- package/lib/module/components/{Dropdown → List}/DropdownFlatList.js +7 -28
- package/lib/module/components/List/DropdownFlatList.js.map +1 -0
- package/lib/module/components/{Dropdown → List}/DropdownSectionList.js +6 -24
- package/lib/module/components/List/DropdownSectionList.js.map +1 -0
- package/lib/module/hooks/index.js +6 -0
- package/lib/module/hooks/index.js.map +1 -0
- package/lib/module/hooks/use-index-of-selected-item.js +42 -0
- package/lib/module/hooks/use-index-of-selected-item.js.map +1 -0
- package/lib/module/hooks/use-modal.js +30 -0
- package/lib/module/hooks/use-modal.js.map +1 -0
- package/lib/module/hooks/use-search.js +51 -0
- package/lib/module/hooks/use-search.js.map +1 -0
- package/lib/module/hooks/use-select-all.js +63 -0
- package/lib/module/hooks/use-select-all.js.map +1 -0
- package/lib/module/hooks/use-selection-handler.js +55 -0
- package/lib/module/hooks/use-selection-handler.js.map +1 -0
- package/lib/module/index.js +123 -262
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/index.js +36 -4
- package/lib/module/utils/index.js.map +1 -1
- package/lib/typescript/src/components/CheckBox/checkbox.types.d.ts +4 -2
- package/lib/typescript/src/components/CheckBox/checkbox.types.d.ts.map +1 -1
- package/lib/typescript/src/components/CheckBox/index.d.ts +2 -2
- package/lib/typescript/src/components/CheckBox/index.d.ts.map +1 -1
- package/lib/typescript/src/components/CustomModal/index.d.ts +3 -1
- package/lib/typescript/src/components/CustomModal/index.d.ts.map +1 -1
- package/lib/typescript/src/components/Dropdown/Dropdown.d.ts +1 -1
- package/lib/typescript/src/components/Dropdown/Dropdown.d.ts.map +1 -1
- package/lib/typescript/src/components/Dropdown/DropdownListItem.d.ts +1 -1
- package/lib/typescript/src/components/Dropdown/DropdownListItem.d.ts.map +1 -1
- package/lib/typescript/src/components/Dropdown/DropdownSelectedItemsView.d.ts +1 -1
- package/lib/typescript/src/components/Input/index.d.ts +5 -1
- package/lib/typescript/src/components/Input/index.d.ts.map +1 -1
- package/lib/typescript/src/components/List/DropdownFlatList.d.ts +6 -0
- package/lib/typescript/src/components/List/DropdownFlatList.d.ts.map +1 -0
- package/lib/typescript/src/components/List/DropdownSectionList.d.ts +4 -0
- package/lib/typescript/src/components/List/DropdownSectionList.d.ts.map +1 -0
- package/lib/typescript/src/hooks/index.d.ts +6 -0
- package/lib/typescript/src/hooks/index.d.ts.map +1 -0
- package/lib/typescript/src/hooks/use-index-of-selected-item.d.ts +23 -0
- package/lib/typescript/src/hooks/use-index-of-selected-item.d.ts.map +1 -0
- package/lib/typescript/src/hooks/use-modal.d.ts +13 -0
- package/lib/typescript/src/hooks/use-modal.d.ts.map +1 -0
- package/lib/typescript/src/hooks/use-search.d.ts +16 -0
- package/lib/typescript/src/hooks/use-search.d.ts.map +1 -0
- package/lib/typescript/src/hooks/use-select-all.d.ts +18 -0
- package/lib/typescript/src/hooks/use-select-all.d.ts.map +1 -0
- package/lib/typescript/src/hooks/use-selection-handler.d.ts +19 -0
- package/lib/typescript/src/hooks/use-selection-handler.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +19 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/types/index.types.d.ts +33 -53
- package/lib/typescript/src/types/index.types.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +17 -3
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/package.json +21 -7
- package/src/components/CheckBox/checkbox.types.ts +2 -2
- package/src/components/CheckBox/index.tsx +23 -47
- package/src/components/CustomModal/index.tsx +15 -23
- package/src/components/Dropdown/Dropdown.tsx +3 -2
- package/src/components/Dropdown/DropdownListItem.tsx +1 -14
- package/src/components/Dropdown/DropdownSelectedItemsView.tsx +13 -13
- package/src/components/Input/index.tsx +13 -2
- package/src/components/{Dropdown → List}/DropdownFlatList.tsx +11 -23
- package/src/components/{Dropdown → List}/DropdownSectionList.tsx +10 -22
- package/src/hooks/index.ts +5 -0
- package/src/hooks/use-index-of-selected-item.ts +49 -0
- package/src/hooks/use-modal.ts +40 -0
- package/src/hooks/use-search.ts +95 -0
- package/src/hooks/use-select-all.ts +79 -0
- package/src/hooks/use-selection-handler.ts +81 -0
- package/src/index.tsx +277 -443
- package/src/types/index.types.ts +41 -56
- package/src/utils/index.ts +60 -3
- package/lib/commonjs/components/Dropdown/DropdownFlatList.js.map +0 -1
- package/lib/commonjs/components/Dropdown/DropdownSectionList.js.map +0 -1
- package/lib/module/components/Dropdown/DropdownFlatList.js.map +0 -1
- package/lib/module/components/Dropdown/DropdownSectionList.js.map +0 -1
- package/lib/typescript/src/components/Dropdown/DropdownFlatList.d.ts +0 -6
- package/lib/typescript/src/components/Dropdown/DropdownFlatList.d.ts.map +0 -1
- package/lib/typescript/src/components/Dropdown/DropdownSectionList.d.ts +0 -4
- package/lib/typescript/src/components/Dropdown/DropdownSectionList.d.ts.map +0 -1
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import { escapeRegExp, isSectionList } from '../utils';
|
|
3
|
+
export const useSearch = ({
|
|
4
|
+
initialOptions,
|
|
5
|
+
optionLabel,
|
|
6
|
+
optionValue,
|
|
7
|
+
searchCallback
|
|
8
|
+
}) => {
|
|
9
|
+
const [searchValue, setSearchValue] = useState('');
|
|
10
|
+
const [filteredOptions, setFilteredOptions] = useState(initialOptions);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
setFilteredOptions(initialOptions);
|
|
13
|
+
return () => {};
|
|
14
|
+
}, [initialOptions]);
|
|
15
|
+
const searchFlatList = useCallback((flatList, regexFilter) => {
|
|
16
|
+
return flatList.filter(item => {
|
|
17
|
+
return item[optionLabel]?.toString().toLowerCase().search(regexFilter) !== -1 || item[optionValue]?.toString().toLowerCase().search(regexFilter) !== -1;
|
|
18
|
+
});
|
|
19
|
+
}, [optionLabel, optionValue]);
|
|
20
|
+
const searchSectionList = useCallback((sectionList, regexFilter) => {
|
|
21
|
+
return sectionList.map(listItem => {
|
|
22
|
+
// A section list is the combination of several flat lists
|
|
23
|
+
const filteredData = searchFlatList(listItem.data, regexFilter);
|
|
24
|
+
return {
|
|
25
|
+
...listItem,
|
|
26
|
+
data: filteredData
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
}, [searchFlatList]);
|
|
30
|
+
const isSection = isSectionList(initialOptions);
|
|
31
|
+
const onSearch = useCallback(value => {
|
|
32
|
+
searchCallback?.(value);
|
|
33
|
+
const searchText = escapeRegExp(value).toLowerCase().trim();
|
|
34
|
+
const regexFilter = new RegExp(searchText, 'i');
|
|
35
|
+
const searchResults = isSection ? searchSectionList(initialOptions, regexFilter) : searchFlatList(initialOptions, regexFilter);
|
|
36
|
+
setFilteredOptions(searchResults);
|
|
37
|
+
}, [initialOptions, isSection, searchCallback, searchFlatList, searchSectionList]);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (searchValue) {
|
|
40
|
+
onSearch(searchValue);
|
|
41
|
+
}
|
|
42
|
+
}, [onSearch, searchValue]);
|
|
43
|
+
return {
|
|
44
|
+
searchValue,
|
|
45
|
+
setSearchValue,
|
|
46
|
+
filteredOptions,
|
|
47
|
+
setFilteredOptions,
|
|
48
|
+
isSectionList: isSection
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=use-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useState","useCallback","useEffect","escapeRegExp","isSectionList","useSearch","initialOptions","optionLabel","optionValue","searchCallback","searchValue","setSearchValue","filteredOptions","setFilteredOptions","searchFlatList","flatList","regexFilter","filter","item","toString","toLowerCase","search","searchSectionList","sectionList","map","listItem","filteredData","data","isSection","onSearch","value","searchText","trim","RegExp","searchResults"],"sourceRoot":"../../../src","sources":["hooks/use-search.ts"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,WAAW,EAAEC,SAAS,QAAQ,OAAO;AAOxD,SAASC,YAAY,EAAEC,aAAa,QAAQ,UAAU;AAStD,OAAO,MAAMC,SAAS,GAAGA,CAAC;EACxBC,cAAc;EACdC,WAAW;EACXC,WAAW;EACXC;AACc,CAAC,KAAK;EACpB,MAAM,CAACC,WAAW,EAAEC,cAAc,CAAC,GAAGX,QAAQ,CAAS,EAAE,CAAC;EAC1D,MAAM,CAACY,eAAe,EAAEC,kBAAkB,CAAC,GAAGb,QAAQ,CAEpDM,cAAc,CAAC;EAEjBJ,SAAS,CAAC,MAAM;IACdW,kBAAkB,CAACP,cAAc,CAAC;IAClC,OAAO,MAAM,CAAC,CAAC;EACjB,CAAC,EAAE,CAACA,cAAc,CAAC,CAAC;EAEpB,MAAMQ,cAAc,GAAGb,WAAW,CAChC,CAACc,QAAmB,EAAEC,WAAmB,KAAK;IAC5C,OAAOD,QAAQ,CAACE,MAAM,CAAEC,IAAmB,IAAK;MAC9C,OACEA,IAAI,CAACX,WAAW,CAAC,EAAEY,QAAQ,CAAC,CAAC,CAACC,WAAW,CAAC,CAAC,CAACC,MAAM,CAACL,WAAW,CAAC,KAC7D,CAAC,CAAC,IACJE,IAAI,CAACV,WAAW,CAAC,EAAEW,QAAQ,CAAC,CAAC,CAACC,WAAW,CAAC,CAAC,CAACC,MAAM,CAACL,WAAW,CAAC,KAAK,CAAC,CAAC;IAE1E,CAAC,CAAC;EACJ,CAAC,EACD,CAACT,WAAW,EAAEC,WAAW,CAC3B,CAAC;EAED,MAAMc,iBAAiB,GAAGr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|
+
import { removeDisabledItems } from '../utils';
|
|
3
|
+
export const useSelectAll = ({
|
|
4
|
+
options,
|
|
5
|
+
selectedItems,
|
|
6
|
+
isMultiple,
|
|
7
|
+
onValueChange,
|
|
8
|
+
listControls,
|
|
9
|
+
optionValue
|
|
10
|
+
}) => {
|
|
11
|
+
const [selectAll, setSelectAll] = useState(false);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @description Handle "Select All" logic
|
|
15
|
+
*/
|
|
16
|
+
const handleSelectAll = useCallback(() => {
|
|
17
|
+
setSelectAll(prevVal => {
|
|
18
|
+
let selectedValues = [];
|
|
19
|
+
|
|
20
|
+
// Remove disabled items from selection
|
|
21
|
+
const filteredOptions = removeDisabledItems(options);
|
|
22
|
+
|
|
23
|
+
// if everything has not been selected, select all the values in the list
|
|
24
|
+
if (!prevVal) {
|
|
25
|
+
selectedValues = filteredOptions.map(obj => obj[optionValue]);
|
|
26
|
+
}
|
|
27
|
+
onValueChange(selectedValues); // Send selected values to parent
|
|
28
|
+
return !prevVal;
|
|
29
|
+
});
|
|
30
|
+
if (typeof listControls?.selectAllCallback === 'function' && !selectAll) {
|
|
31
|
+
listControls.selectAllCallback();
|
|
32
|
+
}
|
|
33
|
+
if (typeof listControls?.unselectAllCallback === 'function' && selectAll) {
|
|
34
|
+
listControls.unselectAllCallback();
|
|
35
|
+
}
|
|
36
|
+
}, [options, optionValue, listControls, selectAll, onValueChange]);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Check if all items are selected
|
|
40
|
+
*/
|
|
41
|
+
const checkSelectAll = useCallback(() => {
|
|
42
|
+
if (removeDisabledItems(options)?.length === selectedItems?.length) {
|
|
43
|
+
setSelectAll(true);
|
|
44
|
+
} else {
|
|
45
|
+
setSelectAll(false);
|
|
46
|
+
}
|
|
47
|
+
}, [options, selectedItems]);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* if the user decides to select the options one by one, this hook
|
|
51
|
+
* runs to check if everything has been selected so that the `selectAll checkbox` can be selected
|
|
52
|
+
*/
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (isMultiple) {
|
|
55
|
+
checkSelectAll();
|
|
56
|
+
}
|
|
57
|
+
}, [checkSelectAll, isMultiple, selectedItems]);
|
|
58
|
+
return {
|
|
59
|
+
selectAll,
|
|
60
|
+
handleSelectAll
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=use-select-all.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallback","useEffect","useState","removeDisabledItems","useSelectAll","options","selectedItems","isMultiple","onValueChange","listControls","optionValue","selectAll","setSelectAll","handleSelectAll","prevVal","selectedValues","filteredOptions","map","obj","selectAllCallback","unselectAllCallback","checkSelectAll","length"],"sourceRoot":"../../../src","sources":["hooks/use-select-all.ts"],"mappings":"AAAA,SAASA,WAAW,EAAEC,SAAS,EAAEC,QAAQ,QAAQ,OAAO;AAExD,SAASC,mBAAmB,QAAQ,UAAU;AAc9C,OAAO,MAAMC,YAAY,GAAGA,CAAC;EAC3BC,OAAO;EACPC,aAAa;EACbC,UAAU;EACVC,aAAa;EACbC,YAAY;EACZC;AACsB,CAAC,KAAK;EAC5B,MAAM,CAACC,SAAS,EAAEC,YAAY,CAAC,GAAGV,QAAQ,CAAU,KAAK,CAAC;;EAE1D;AACF;AACA;EACE,MAAMW,eAAe,GAAGb,WAAW,CAAC,MAAM;IACxCY,YAAY,CAAEE,OAAO,IAAK;MACxB,IAAIC,cAA+B,GAAG,EAAE;;MAExC;MACA,MAAMC,eAAe,GAAGb,mBAAmB,CAACE,OAAO,CAAC;;MAEpD;MACA,IAAI,CAACS,OAAO,EAAE;QACZC,cAAc,GAAGC,eAAe,CAACC,GAAG,CACjCC,GAAG,IAAKA,GAAG,CAACR,WAAW,CAC1B,CAAoB;MACtB;MAEAF,aAAa,CAACO,cAAc,CAAC,CAAC,CAAC;MAC/B,OAAO,CAACD,OAAO;IACjB,CAAC,CAAC;IAEF,IAAI,OAAOL,YAAY,EAAEU,iBAAiB,KAAK,UAAU,IAAI,CAACR,SAAS,EAAE;MACvEF,YAAY,CAACU,iBAAiB,CAAC,CAAC;IAClC;IAEA,IAAI,OAAOV,YAAY,EAAEW,mBAAmB,KAAK,UAAU,IAAIT,SAAS,EAAE;MACxEF,YAAY,CAACW,mBAAmB,CAAC,CAAC;IACpC;EACF,CAAC,EAAE,CAACf,OAAO,EAAEK,WAAW,EAAED,YAAY,EAAEE,SAAS,EAAEH,aAAa,CAAC,CAAC;;EAElE;AACF;AACA;EACE,MAAMa,cAAc,GAAGrB,WAAW,CAAC,MAAM;IACvC,IAAIG,mBAAmB,CAACE,OAAO,CAAC,EAAEiB,MAAM,KAAKhB,aAAa,EAAEgB,MAAM,EAAE;MAClEV,YAAY,CAAC,IAAI,CAAC;IACpB,CAAC,MAAM;MACLA,YAAY,CAAC,KAAK,CAAC;IACrB;EACF,CAAC,EAAE,CAACP,OAAO,EAAEC,aAAa,CAAC,CAAC;;EAE5B;AACF;AACA;AACA;EACEL,SAAS,CAAC,MAAM;IACd,IAAIM,UAAU,EAAE;MACdc,cAAc,CAAC,CAAC;IAClB;EACF,CAAC,EAAE,CAACA,cAAc,EAAEd,UAAU,EAAED,aAAa,CAAC,CAAC;EAE/C,OAAO;IAAEK,SAAS;IAAEE;EAAgB,CAAC;AACvC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useState, useCallback } from 'react';
|
|
2
|
+
export const useSelectionHandler = ({
|
|
3
|
+
initialSelectedValue,
|
|
4
|
+
isMultiple,
|
|
5
|
+
maxSelectableItems,
|
|
6
|
+
onValueChange,
|
|
7
|
+
closeModal,
|
|
8
|
+
autoCloseOnSelect
|
|
9
|
+
}) => {
|
|
10
|
+
// Initialize state based on whether it's multiple selection or not
|
|
11
|
+
const [selectedItem, setSelectedItem] = useState(isMultiple ? '' : initialSelectedValue);
|
|
12
|
+
const [selectedItems, setSelectedItems] = useState(isMultiple ? initialSelectedValue : []);
|
|
13
|
+
const handleSingleSelection = useCallback(value => {
|
|
14
|
+
if (selectedItem === value) {
|
|
15
|
+
setSelectedItem('');
|
|
16
|
+
onValueChange(''); // Send null to parent when deselected
|
|
17
|
+
} else {
|
|
18
|
+
setSelectedItem(value);
|
|
19
|
+
onValueChange(value); // Send selected value to parent
|
|
20
|
+
|
|
21
|
+
if (autoCloseOnSelect) {
|
|
22
|
+
closeModal(); // close modal upon selection
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}, [selectedItem, onValueChange, autoCloseOnSelect, closeModal]);
|
|
26
|
+
const handleMultipleSelections = useCallback(value => {
|
|
27
|
+
setSelectedItems(prevVal => {
|
|
28
|
+
let selectedValues = [...prevVal];
|
|
29
|
+
if (selectedValues.includes(value)) {
|
|
30
|
+
// Remove item
|
|
31
|
+
selectedValues = selectedValues.filter(item => item !== value);
|
|
32
|
+
} else {
|
|
33
|
+
// Add item
|
|
34
|
+
if (maxSelectableItems && selectedValues.length >= maxSelectableItems) {
|
|
35
|
+
return selectedValues;
|
|
36
|
+
}
|
|
37
|
+
selectedValues.push(value);
|
|
38
|
+
}
|
|
39
|
+
onValueChange(selectedValues); // Send selected values to parent
|
|
40
|
+
return selectedValues;
|
|
41
|
+
});
|
|
42
|
+
}, [maxSelectableItems, onValueChange]);
|
|
43
|
+
|
|
44
|
+
// Return the relevant state and handlers
|
|
45
|
+
return {
|
|
46
|
+
selectedItem,
|
|
47
|
+
selectedItems,
|
|
48
|
+
handleSingleSelection,
|
|
49
|
+
handleMultipleSelections,
|
|
50
|
+
setSelectedItems,
|
|
51
|
+
// Expose for potential manual control
|
|
52
|
+
setSelectedItem // Expose for potential manual control
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=use-selection-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useState","useCallback","useSelectionHandler","initialSelectedValue","isMultiple","maxSelectableItems","onValueChange","closeModal","autoCloseOnSelect","selectedItem","setSelectedItem","selectedItems","setSelectedItems","handleSingleSelection","value","handleMultipleSelections","prevVal","selectedValues","includes","filter","item","length","push"],"sourceRoot":"../../../src","sources":["hooks/use-selection-handler.ts"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,WAAW,QAAQ,OAAO;AAY7C,OAAO,MAAMC,mBAAmB,GAAGA,CAAC;EAClCC,oBAAoB;EACpBC,UAAU;EACVC,kBAAkB;EAClBC,aAAa;EACbC,UAAU;EACVC;AACwB,CAAC,KAAK;EAC9B;EACA,MAAM,CAACC,YAAY,EAAEC,eAAe,CAAC,GAAGV,QAAQ,CAC9CI,UAAU,GAAG,EAAE,GAAID,oBACrB,CAAC;EACD,MAAM,CAACQ,aAAa,EAAEC,gBAAgB,CAAC,GAAGZ,QAAQ,CAChDI,UAAU,GAAID,oBAAoB,GAAuB,EAC3D,CAAC;EAED,MAAMU,qBAAqB,GAAGZ,WAAW,CACtCa,KAAoB,IAAK;IACxB,IAAIL,YAAY,KAAKK,KAAK,EAAE;MAC1BJ,eAAe,CAAC,EAAE,CAAC;MACnBJ,aAAa,CAAC,EAAE,CAAC,CAAC,CAAC;IACrB,CAAC,MAAM;MACLI,eAAe,CAACI,KAAK,CAAC;MACtBR,aAAa,CAACQ,KAAK,CAAC,CAAC,CAAC;;MAEtB,IAAIN,iBAAiB,EAAE;QACrBD,UAAU,CAAC,CAAC,CAAC,CAAC;MAChB;IACF;EACF,CAAC,EACD,CAACE,YAAY,EAAEH,aAAa,EAAEE,iBAAiB,EAAED,UAAU,CAC7D,CAAC;EAED,MAAMQ,wBAAwB,GAAGd,WAAW,CACzCa,KAAoB,IAAK;IACxBF,gBAAgB,CAAEI,OAAO,IAAK;MAC5B,IAAIC,cAAc,GAAG,CAAC,GAAGD,OAAO,CAAC;MAEjC,IAAIC,cAAc,CAACC,QAAQ,CAACJ,KAAK,CAAC,EAAE;QAClC;QACAG,cAAc,GAAGA,cAAc,CAACE,MAAM,CAAEC,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