|
| 1 | +import { useMemo, useRef } from 'react'; |
| 2 | +import useUpdate from './useUpdate'; |
| 3 | +import { |
| 4 | + IHookStateInitAction, |
| 5 | + IHookStateSetAction, |
| 6 | + resolveHookState, |
| 7 | +} from './misc/hookState'; |
| 8 | + |
| 9 | +export interface ListActions<T> { |
| 10 | + /** |
| 11 | + * @description Set new list instead old one |
| 12 | + */ |
| 13 | + set: (newList: IHookStateSetAction<T[]>) => void; |
| 14 | + /** |
| 15 | + * @description Add item(s) at the end of list |
| 16 | + */ |
| 17 | + push: (...items: T[]) => void; |
| 18 | + |
| 19 | + /** |
| 20 | + * @description Replace item at given position. If item at given position not exists it will be set. |
| 21 | + */ |
| 22 | + updateAt: (index: number, item: T) => void; |
| 23 | + /** |
| 24 | + * @description Insert item at given position, all items to the right will be shifted. |
| 25 | + */ |
| 26 | + insertAt: (index: number, item: T) => void; |
| 27 | + |
| 28 | + /** |
| 29 | + * @description Replace all items that matches predicate with given one. |
| 30 | + */ |
| 31 | + update: (predicate: (a: T, b: T) => boolean, newItem: T) => void; |
| 32 | + /** |
| 33 | + * @description Replace first item matching predicate with given one. |
| 34 | + */ |
| 35 | + updateFirst: (predicate: (a: T, b: T) => boolean, newItem: T) => void; |
| 36 | + /** |
| 37 | + * @description Like `updateFirst` bit in case of predicate miss - pushes item to the list |
| 38 | + */ |
| 39 | + upsert: (predicate: (a: T, b: T) => boolean, newItem: T) => void; |
| 40 | + |
| 41 | + /** |
| 42 | + * @description Sort list with given sorting function |
| 43 | + */ |
| 44 | + sort: (compareFn?: (a: T, b: T) => number) => void; |
| 45 | + /** |
| 46 | + * @description Same as native Array's method |
| 47 | + */ |
| 48 | + filter: ( |
| 49 | + callbackFn: (value: T, index?: number, array?: T[]) => boolean, |
| 50 | + thisArg?: any |
| 51 | + ) => void; |
| 52 | + |
| 53 | + /** |
| 54 | + * @description Removes item at given position. All items to the right from removed will be shifted. |
| 55 | + */ |
| 56 | + removeAt: (index: number) => void; |
| 57 | + /** |
| 58 | + * @deprecated Use removeAt method instead |
| 59 | + */ |
| 60 | + remove: (index: number) => void; |
| 61 | + |
| 62 | + /** |
| 63 | + * @description Make the list empty |
| 64 | + */ |
| 65 | + clear: () => void; |
| 66 | + /** |
| 67 | + * @description Reset list to initial value |
| 68 | + */ |
| 69 | + reset: () => void; |
| 70 | +} |
| 71 | + |
| 72 | +export function useList<T>( |
| 73 | + initialList: IHookStateInitAction<T[]> = [] |
| 74 | +): [T[], ListActions<T>] { |
| 75 | + const list = useRef(resolveHookState(initialList)); |
| 76 | + const update = useUpdate(); |
| 77 | + |
| 78 | + const actions = useMemo<ListActions<T>>(() => { |
| 79 | + const a = { |
| 80 | + set: (newList: IHookStateSetAction<T[]>) => { |
| 81 | + list.current = resolveHookState(newList, list.current); |
| 82 | + update(); |
| 83 | + }, |
| 84 | + |
| 85 | + push: (...items: T[]) => { |
| 86 | + items.length && actions.set((curr: T[]) => curr.concat(items)); |
| 87 | + }, |
| 88 | + |
| 89 | + updateAt: (index: number, item: T) => { |
| 90 | + actions.set((curr: T[]) => { |
| 91 | + const arr = curr.slice(); |
| 92 | + |
| 93 | + arr[index] = item; |
| 94 | + |
| 95 | + return arr; |
| 96 | + }); |
| 97 | + }, |
| 98 | + |
| 99 | + insertAt: (index: number, item: T) => { |
| 100 | + actions.set((curr: T[]) => { |
| 101 | + const arr = curr.slice(); |
| 102 | + |
| 103 | + index > arr.length ? (arr[index] = item) : arr.splice(index, 0, item); |
| 104 | + |
| 105 | + return arr; |
| 106 | + }); |
| 107 | + }, |
| 108 | + |
| 109 | + update: (predicate: (a: T, b: T) => boolean, newItem: T) => { |
| 110 | + actions.set((curr: T[]) => |
| 111 | + curr.map(item => (predicate(item, newItem) ? newItem : item)) |
| 112 | + ); |
| 113 | + }, |
| 114 | + |
| 115 | + updateFirst: (predicate: (a: T, b: T) => boolean, newItem: T) => { |
| 116 | + const index = list.current.findIndex(item => predicate(item, newItem)); |
| 117 | + |
| 118 | + index >= 0 && actions.updateAt(index, newItem); |
| 119 | + }, |
| 120 | + |
| 121 | + upsert: (predicate: (a: T, b: T) => boolean, newItem: T) => { |
| 122 | + const index = list.current.findIndex(item => predicate(item, newItem)); |
| 123 | + |
| 124 | + index >= 0 ? actions.updateAt(index, newItem) : actions.push(newItem); |
| 125 | + }, |
| 126 | + |
| 127 | + sort: (compareFn?: (a: T, b: T) => number) => { |
| 128 | + actions.set((curr: T[]) => curr.slice().sort(compareFn)); |
| 129 | + }, |
| 130 | + |
| 131 | + filter: <S extends T>( |
| 132 | + callbackFn: (value: T, index: number, array: T[]) => value is S, |
| 133 | + thisArg?: any |
| 134 | + ) => { |
| 135 | + actions.set((curr: T[]) => curr.slice().filter(callbackFn, thisArg)); |
| 136 | + }, |
| 137 | + |
| 138 | + removeAt: (index: number) => { |
| 139 | + actions.set((curr: T[]) => { |
| 140 | + const arr = curr.slice(); |
| 141 | + |
| 142 | + arr.splice(index, 1); |
| 143 | + |
| 144 | + return arr; |
| 145 | + }); |
| 146 | + }, |
| 147 | + |
| 148 | + clear: () => { |
| 149 | + actions.set([]); |
| 150 | + }, |
| 151 | + |
| 152 | + reset: () => { |
| 153 | + actions.set(resolveHookState(initialList).slice()); |
| 154 | + }, |
| 155 | + }; |
| 156 | + |
| 157 | + return a as ListActions<T>; |
| 158 | + /* eslint-disable react-hooks/exhaustive-deps */ |
| 159 | + }, []); |
| 160 | + |
| 161 | + return [list.current, actions]; |
| 162 | +} |
0 commit comments