|
|
import { useMemo, useRef } from 'react'; |
|
|
import useUpdate from './useUpdate'; |
|
|
import { IHookStateInitAction, IHookStateSetAction, resolveHookState } from './misc/hookState'; |
|
|
|
|
|
export interface ListActions<T> { |
|
|
|
|
|
|
|
|
|
|
|
set: (newList: IHookStateSetAction<T[]>) => void; |
|
|
|
|
|
|
|
|
|
|
|
push: (...items: T[]) => void; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateAt: (index: number, item: T) => void; |
|
|
|
|
|
|
|
|
|
|
|
insertAt: (index: number, item: T) => void; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
update: (predicate: (a: T, b: T) => boolean, newItem: T) => void; |
|
|
|
|
|
|
|
|
|
|
|
updateFirst: (predicate: (a: T, b: T) => boolean, newItem: T) => void; |
|
|
|
|
|
|
|
|
|
|
|
upsert: (predicate: (a: T, b: T) => boolean, newItem: T) => void; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sort: (compareFn?: (a: T, b: T) => number) => void; |
|
|
|
|
|
|
|
|
|
|
|
filter: (callbackFn: (value: T, index?: number, array?: T[]) => boolean, thisArg?: any) => void; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
removeAt: (index: number) => void; |
|
|
|
|
|
|
|
|
|
|
|
remove: (index: number) => void; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clear: () => void; |
|
|
|
|
|
|
|
|
|
|
|
reset: () => void; |
|
|
} |
|
|
|
|
|
function useList<T>(initialList: IHookStateInitAction<T[]> = []): [T[], ListActions<T>] { |
|
|
const list = useRef(resolveHookState(initialList)); |
|
|
const update = useUpdate(); |
|
|
|
|
|
const actions = useMemo<ListActions<T>>(() => { |
|
|
const a = { |
|
|
set: (newList: IHookStateSetAction<T[]>) => { |
|
|
list.current = resolveHookState(newList, list.current); |
|
|
update(); |
|
|
}, |
|
|
|
|
|
push: (...items: T[]) => { |
|
|
items.length && actions.set((curr: T[]) => curr.concat(items)); |
|
|
}, |
|
|
|
|
|
updateAt: (index: number, item: T) => { |
|
|
actions.set((curr: T[]) => { |
|
|
const arr = curr.slice(); |
|
|
|
|
|
arr[index] = item; |
|
|
|
|
|
return arr; |
|
|
}); |
|
|
}, |
|
|
|
|
|
insertAt: (index: number, item: T) => { |
|
|
actions.set((curr: T[]) => { |
|
|
const arr = curr.slice(); |
|
|
|
|
|
index > arr.length ? (arr[index] = item) : arr.splice(index, 0, item); |
|
|
|
|
|
return arr; |
|
|
}); |
|
|
}, |
|
|
|
|
|
update: (predicate: (a: T, b: T) => boolean, newItem: T) => { |
|
|
actions.set((curr: T[]) => curr.map((item) => (predicate(item, newItem) ? newItem : item))); |
|
|
}, |
|
|
|
|
|
updateFirst: (predicate: (a: T, b: T) => boolean, newItem: T) => { |
|
|
const index = list.current.findIndex((item) => predicate(item, newItem)); |
|
|
|
|
|
index >= 0 && actions.updateAt(index, newItem); |
|
|
}, |
|
|
|
|
|
upsert: (predicate: (a: T, b: T) => boolean, newItem: T) => { |
|
|
const index = list.current.findIndex((item) => predicate(item, newItem)); |
|
|
|
|
|
index >= 0 ? actions.updateAt(index, newItem) : actions.push(newItem); |
|
|
}, |
|
|
|
|
|
sort: (compareFn?: (a: T, b: T) => number) => { |
|
|
actions.set((curr: T[]) => curr.slice().sort(compareFn)); |
|
|
}, |
|
|
|
|
|
filter: <S extends T>( |
|
|
callbackFn: (value: T, index: number, array: T[]) => value is S, |
|
|
thisArg?: any |
|
|
) => { |
|
|
actions.set((curr: T[]) => curr.slice().filter(callbackFn, thisArg)); |
|
|
}, |
|
|
|
|
|
removeAt: (index: number) => { |
|
|
actions.set((curr: T[]) => { |
|
|
const arr = curr.slice(); |
|
|
|
|
|
arr.splice(index, 1); |
|
|
|
|
|
return arr; |
|
|
}); |
|
|
}, |
|
|
|
|
|
clear: () => { |
|
|
actions.set([]); |
|
|
}, |
|
|
|
|
|
reset: () => { |
|
|
actions.set(resolveHookState(initialList).slice()); |
|
|
}, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(a as ListActions<T>).remove = a.removeAt; |
|
|
|
|
|
return a as ListActions<T>; |
|
|
}, []); |
|
|
|
|
|
return [list.current, actions]; |
|
|
} |
|
|
|
|
|
export default useList; |
|
|
|