78 lines
3.1 KiB
JavaScript
78 lines
3.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var tslib_1 = require("tslib");
|
|
var react_1 = require("react");
|
|
var useUpdate_1 = tslib_1.__importDefault(require("./useUpdate"));
|
|
var hookState_1 = require("./misc/hookState");
|
|
function useList(initialList) {
|
|
if (initialList === void 0) { initialList = []; }
|
|
var list = react_1.useRef(hookState_1.resolveHookState(initialList));
|
|
var update = useUpdate_1.default();
|
|
var actions = react_1.useMemo(function () {
|
|
var a = {
|
|
set: function (newList) {
|
|
list.current = hookState_1.resolveHookState(newList, list.current);
|
|
update();
|
|
},
|
|
push: function () {
|
|
var items = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
items[_i] = arguments[_i];
|
|
}
|
|
items.length && actions.set(function (curr) { return curr.concat(items); });
|
|
},
|
|
updateAt: function (index, item) {
|
|
actions.set(function (curr) {
|
|
var arr = curr.slice();
|
|
arr[index] = item;
|
|
return arr;
|
|
});
|
|
},
|
|
insertAt: function (index, item) {
|
|
actions.set(function (curr) {
|
|
var arr = curr.slice();
|
|
index > arr.length ? (arr[index] = item) : arr.splice(index, 0, item);
|
|
return arr;
|
|
});
|
|
},
|
|
update: function (predicate, newItem) {
|
|
actions.set(function (curr) { return curr.map(function (item) { return (predicate(item, newItem) ? newItem : item); }); });
|
|
},
|
|
updateFirst: function (predicate, newItem) {
|
|
var index = list.current.findIndex(function (item) { return predicate(item, newItem); });
|
|
index >= 0 && actions.updateAt(index, newItem);
|
|
},
|
|
upsert: function (predicate, newItem) {
|
|
var index = list.current.findIndex(function (item) { return predicate(item, newItem); });
|
|
index >= 0 ? actions.updateAt(index, newItem) : actions.push(newItem);
|
|
},
|
|
sort: function (compareFn) {
|
|
actions.set(function (curr) { return curr.slice().sort(compareFn); });
|
|
},
|
|
filter: function (callbackFn, thisArg) {
|
|
actions.set(function (curr) { return curr.slice().filter(callbackFn, thisArg); });
|
|
},
|
|
removeAt: function (index) {
|
|
actions.set(function (curr) {
|
|
var arr = curr.slice();
|
|
arr.splice(index, 1);
|
|
return arr;
|
|
});
|
|
},
|
|
clear: function () {
|
|
actions.set([]);
|
|
},
|
|
reset: function () {
|
|
actions.set(hookState_1.resolveHookState(initialList).slice());
|
|
},
|
|
};
|
|
/**
|
|
* @deprecated Use removeAt method instead
|
|
*/
|
|
a.remove = a.removeAt;
|
|
return a;
|
|
}, []);
|
|
return [list.current, actions];
|
|
}
|
|
exports.default = useList;
|