UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

108 lines 5.03 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useRecordSelection = void 0; var react_1 = require("react"); var store_1 = require("../../store/index.cjs"); /** * Get the list of selected items for a resource, and callbacks to change the selection * * @param args.resource The resource name, e.g. 'posts' * @param args.storeKey The key to use to store selected items. Pass false to disable synchronization with the store. * @param args.disableSyncWithStore Controls the selection synchronization with the store * * @returns {Object} Destructure as [selectedIds, { select, unselect, toggle, clearSelection }]. */ var useRecordSelection = function (args) { var _a = args.resource, resource = _a === void 0 ? '' : _a, storeKey = args.storeKey, disableSyncWithStore = args.disableSyncWithStore; var finalStoreKey = "".concat(storeKey || resource, ".selectedIds"); var _b = (0, react_1.useState)(defaultIds), localSelectionStore = _b[0], setLocalSelectionStore = _b[1]; // As we can't conditionally call a hook, if the disableSyncWithStore is true, // we'll ignore the useStore values later on and won't call set functions either. var _c = (0, store_1.useStore)(finalStoreKey, defaultIds), selectionStore = _c[0], setSelectionStore = _c[1]; var _d = (0, store_1.useStore)("".concat(resource, ".selectedIds.storeKeys"), defaultStoreKeys), storeKeys = _d[0], setStoreKeys = _d[1]; (0, react_1.useEffect)(function addStoreKeyToStore() { if (!disableSyncWithStore && storeKey) { setStoreKeys(function (storeKeys) { if (!storeKeys.includes(finalStoreKey)) { return __spreadArray(__spreadArray([], storeKeys, true), [finalStoreKey], false); } else { return storeKeys; } }); } }, [disableSyncWithStore, finalStoreKey, setStoreKeys, storeKey]); var _e = (0, store_1.useStoreContext)(), getItem = _e.getItem, setItem = _e.setItem; var ids = disableSyncWithStore ? localSelectionStore : selectionStore; var setStore = (0, react_1.useMemo)(function () { return disableSyncWithStore ? setLocalSelectionStore : setSelectionStore; }, [disableSyncWithStore, setSelectionStore]); var selectionModifiers = (0, react_1.useMemo)(function () { return ({ select: function (idsToSelect) { if (!idsToSelect) return; setStore(idsToSelect); }, unselect: function (idsToRemove, fromAllStoreKeys) { if (!idsToRemove || idsToRemove.length === 0) return; setStore(function (ids) { return ids.filter(function (id) { return !idsToRemove.includes(id); }); }); if (!disableSyncWithStore && fromAllStoreKeys) { storeKeys .filter(function (storeKey) { return storeKey !== finalStoreKey; }) .forEach(function (storeKey) { var ids = getItem(storeKey); if (ids) { setItem(storeKey, ids.filter(function (id) { return !idsToRemove.includes(id); })); } }); } }, toggle: function (id) { if (typeof id === 'undefined') return; setStore(function (ids) { if (!Array.isArray(ids)) return __spreadArray([], ids, true); var index = ids.indexOf(id); var hasId = index > -1; return hasId ? __spreadArray(__spreadArray([], ids.slice(0, index), true), ids.slice(index + 1), true) : __spreadArray(__spreadArray([], ids, true), [id], false); }); }, clearSelection: function (fromAllStoreKeys) { setStore(defaultIds); if (!disableSyncWithStore && fromAllStoreKeys) { storeKeys .filter(function (storeKey) { return storeKey !== finalStoreKey; }) .forEach(function (storeKey) { var ids = getItem(storeKey); if (ids) { setItem(storeKey, defaultIds); } }); } }, }); }, [ disableSyncWithStore, finalStoreKey, getItem, setItem, setStore, storeKeys, ]); return [ids, selectionModifiers]; }; exports.useRecordSelection = useRecordSelection; var defaultIds = []; var defaultStoreKeys = []; //# sourceMappingURL=useRecordSelection.js.map