UNPKG

@td-design/rn-hooks

Version:

从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率

188 lines (185 loc) 5.67 kB
import { __read, __spreadArray } from '../_virtual/_tslib.js'; import { useRef, useCallback, useState } from 'react'; import useMemoizedFn from '../useMemoizedFn/index.js'; function useDynamicList(initialList) { if (initialList === void 0) { initialList = []; } var counterRef = useRef(-1); var keyListRef = useRef([]); var setKey = useCallback(function (index) { counterRef.current += 1; keyListRef.current.splice(index, 0, counterRef.current); }, []); var _a = __read(useState(function () { initialList.forEach(function (_, index) { setKey(index); }); return initialList; }), 2), list = _a[0], setList = _a[1]; /** * 重设List * @param newList */ var reset = function (newList) { keyListRef.current = []; setList(function () { newList.forEach(function (_, index) { setKey(index); }); return newList; }); }; /** * 在指定位置插入一个新数据 * @param index * @param item */ var insert = function (index, item) { setList(function (l) { var temp = __spreadArray([], __read(l), false); temp.splice(index, 0, item); setKey(index); return temp; }); }; var getKey = function (index) { return keyListRef.current[index]; }; var getIndex = function (key) { return keyListRef.current.findIndex(function (item) { return item === key; }); }; /** * 从指定位置开始,合并数据 * @param index * @param items */ var merge = function (index, items) { setList(function (l) { var temp = __spreadArray([], __read(l), false); items.forEach(function (_, i) { setKey(index + i); }); temp.splice.apply(temp, __spreadArray([index, 0], __read(items), false)); return temp; }); }; /** * 替换指定位置的数据 * @param index * @param item */ var replace = function (index, item) { setList(function (l) { var temp = __spreadArray([], __read(l), false); temp[index] = item; return temp; }); }; /** * 删除指定位置的数据 * @param index */ var remove = function (index) { setList(function (l) { var temp = __spreadArray([], __read(l), false); temp.splice(index, 1); try { keyListRef.current.splice(index, 1); } catch (e) { console.error(e); } return temp; }); }; /** * 移动数据到新位置 * @param oldIndex * @param newIndex */ var move = function (oldIndex, newIndex) { if (oldIndex === newIndex) return; setList(function (l) { var newList = __spreadArray([], __read(l), false); var temp = newList.filter(function (_, index) { return index !== oldIndex; }); temp.splice(newIndex, 0, newList[oldIndex]); try { var keyTemp = keyListRef.current.filter(function (_, index) { return index !== oldIndex; }); keyTemp.splice(newIndex, 0, keyListRef.current[oldIndex]); keyListRef.current = keyTemp; } catch (error) { console.error(error); } return temp; }); }; /** * 在当前数组中插入一个新的数据 * @param item */ var push = function (item) { setList(function (l) { setKey(l.length); return l.concat([item]); }); }; /** * 从数组中删除最后一个数据,返回新数组 */ var pop = function () { try { keyListRef.current = keyListRef.current.slice(0, keyListRef.current.length - 1); } catch (error) { console.error(error); } setList(function (l) { return l.slice(0, l.length - 1); }); }; /** * 在数组最前面插入一条新数据 * @param item */ var unshift = function (item) { setList(function (l) { setKey(0); return [item].concat(l); }); }; /** * 删除数组的第一条数据,并返回新数组 */ var shift = function () { try { keyListRef.current = keyListRef.current.slice(1, keyListRef.current.length); } catch (error) { console.error(error); } setList(function (l) { return l.slice(1, l.length); }); }; var sort = function (result) { return result .map(function (item, index) { return ({ key: index, item: item }); }) .sort(function (a, b) { return getIndex(a.key) - getIndex(b.key); }) .filter(function (item) { return !!item.item; }) .map(function (item) { return item.item; }); }; return { list: list, insert: useMemoizedFn(insert), merge: useMemoizedFn(merge), replace: useMemoizedFn(replace), remove: useMemoizedFn(remove), getKey: useMemoizedFn(getKey), getIndex: useMemoizedFn(getIndex), move: useMemoizedFn(move), push: useMemoizedFn(push), pop: useMemoizedFn(pop), unshift: useMemoizedFn(unshift), shift: useMemoizedFn(shift), sort: useMemoizedFn(sort), reset: useMemoizedFn(reset), }; } export { useDynamicList as default };