source: imaps-frontend/node_modules/react-use/esm/useList.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.9 KB
Line 
1import { useMemo, useRef } from 'react';
2import useUpdate from './useUpdate';
3import { resolveHookState } from './misc/hookState';
4function useList(initialList) {
5 if (initialList === void 0) { initialList = []; }
6 var list = useRef(resolveHookState(initialList));
7 var update = useUpdate();
8 var actions = useMemo(function () {
9 var a = {
10 set: function (newList) {
11 list.current = resolveHookState(newList, list.current);
12 update();
13 },
14 push: function () {
15 var items = [];
16 for (var _i = 0; _i < arguments.length; _i++) {
17 items[_i] = arguments[_i];
18 }
19 items.length && actions.set(function (curr) { return curr.concat(items); });
20 },
21 updateAt: function (index, item) {
22 actions.set(function (curr) {
23 var arr = curr.slice();
24 arr[index] = item;
25 return arr;
26 });
27 },
28 insertAt: function (index, item) {
29 actions.set(function (curr) {
30 var arr = curr.slice();
31 index > arr.length ? (arr[index] = item) : arr.splice(index, 0, item);
32 return arr;
33 });
34 },
35 update: function (predicate, newItem) {
36 actions.set(function (curr) { return curr.map(function (item) { return (predicate(item, newItem) ? newItem : item); }); });
37 },
38 updateFirst: function (predicate, newItem) {
39 var index = list.current.findIndex(function (item) { return predicate(item, newItem); });
40 index >= 0 && actions.updateAt(index, newItem);
41 },
42 upsert: function (predicate, newItem) {
43 var index = list.current.findIndex(function (item) { return predicate(item, newItem); });
44 index >= 0 ? actions.updateAt(index, newItem) : actions.push(newItem);
45 },
46 sort: function (compareFn) {
47 actions.set(function (curr) { return curr.slice().sort(compareFn); });
48 },
49 filter: function (callbackFn, thisArg) {
50 actions.set(function (curr) { return curr.slice().filter(callbackFn, thisArg); });
51 },
52 removeAt: function (index) {
53 actions.set(function (curr) {
54 var arr = curr.slice();
55 arr.splice(index, 1);
56 return arr;
57 });
58 },
59 clear: function () {
60 actions.set([]);
61 },
62 reset: function () {
63 actions.set(resolveHookState(initialList).slice());
64 },
65 };
66 /**
67 * @deprecated Use removeAt method instead
68 */
69 a.remove = a.removeAt;
70 return a;
71 }, []);
72 return [list.current, actions];
73}
74export default useList;
Note: See TracBrowser for help on using the repository browser.