1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | var tslib_1 = require("tslib");
|
---|
4 | var react_1 = require("react");
|
---|
5 | var useUpdate_1 = tslib_1.__importDefault(require("./useUpdate"));
|
---|
6 | var hookState_1 = require("./misc/hookState");
|
---|
7 | function useList(initialList) {
|
---|
8 | if (initialList === void 0) { initialList = []; }
|
---|
9 | var list = react_1.useRef(hookState_1.resolveHookState(initialList));
|
---|
10 | var update = useUpdate_1.default();
|
---|
11 | var actions = react_1.useMemo(function () {
|
---|
12 | var a = {
|
---|
13 | set: function (newList) {
|
---|
14 | list.current = hookState_1.resolveHookState(newList, list.current);
|
---|
15 | update();
|
---|
16 | },
|
---|
17 | push: function () {
|
---|
18 | var items = [];
|
---|
19 | for (var _i = 0; _i < arguments.length; _i++) {
|
---|
20 | items[_i] = arguments[_i];
|
---|
21 | }
|
---|
22 | items.length && actions.set(function (curr) { return curr.concat(items); });
|
---|
23 | },
|
---|
24 | updateAt: function (index, item) {
|
---|
25 | actions.set(function (curr) {
|
---|
26 | var arr = curr.slice();
|
---|
27 | arr[index] = item;
|
---|
28 | return arr;
|
---|
29 | });
|
---|
30 | },
|
---|
31 | insertAt: function (index, item) {
|
---|
32 | actions.set(function (curr) {
|
---|
33 | var arr = curr.slice();
|
---|
34 | index > arr.length ? (arr[index] = item) : arr.splice(index, 0, item);
|
---|
35 | return arr;
|
---|
36 | });
|
---|
37 | },
|
---|
38 | update: function (predicate, newItem) {
|
---|
39 | actions.set(function (curr) { return curr.map(function (item) { return (predicate(item, newItem) ? newItem : item); }); });
|
---|
40 | },
|
---|
41 | updateFirst: function (predicate, newItem) {
|
---|
42 | var index = list.current.findIndex(function (item) { return predicate(item, newItem); });
|
---|
43 | index >= 0 && actions.updateAt(index, newItem);
|
---|
44 | },
|
---|
45 | upsert: function (predicate, newItem) {
|
---|
46 | var index = list.current.findIndex(function (item) { return predicate(item, newItem); });
|
---|
47 | index >= 0 ? actions.updateAt(index, newItem) : actions.push(newItem);
|
---|
48 | },
|
---|
49 | sort: function (compareFn) {
|
---|
50 | actions.set(function (curr) { return curr.slice().sort(compareFn); });
|
---|
51 | },
|
---|
52 | filter: function (callbackFn, thisArg) {
|
---|
53 | actions.set(function (curr) { return curr.slice().filter(callbackFn, thisArg); });
|
---|
54 | },
|
---|
55 | removeAt: function (index) {
|
---|
56 | actions.set(function (curr) {
|
---|
57 | var arr = curr.slice();
|
---|
58 | arr.splice(index, 1);
|
---|
59 | return arr;
|
---|
60 | });
|
---|
61 | },
|
---|
62 | clear: function () {
|
---|
63 | actions.set([]);
|
---|
64 | },
|
---|
65 | reset: function () {
|
---|
66 | actions.set(hookState_1.resolveHookState(initialList).slice());
|
---|
67 | },
|
---|
68 | };
|
---|
69 | /**
|
---|
70 | * @deprecated Use removeAt method instead
|
---|
71 | */
|
---|
72 | a.remove = a.removeAt;
|
---|
73 | return a;
|
---|
74 | }, []);
|
---|
75 | return [list.current, actions];
|
---|
76 | }
|
---|
77 | exports.default = useList;
|
---|