UNPKG

@td-design/rn-hooks

Version:

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

190 lines (186 loc) 5.65 kB
'use strict'; var _tslib = require('../_virtual/_tslib.js'); var react = require('react'); var index = require('../useMemoizedFn/index.js'); function useDynamicList(initialList) { if (initialList === void 0) { initialList = []; } var counterRef = react.useRef(-1); var keyListRef = react.useRef([]); var setKey = react.useCallback(function (index) { counterRef.current += 1; keyListRef.current.splice(index, 0, counterRef.current); }, []); var _a = _tslib.__read(react.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 = _tslib.__spreadArray([], _tslib.__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 = _tslib.__spreadArray([], _tslib.__read(l), false); items.forEach(function (_, i) { setKey(index + i); }); temp.splice.apply(temp, _tslib.__spreadArray([index, 0], _tslib.__read(items), false)); return temp; }); }; /** * 替换指定位置的数据 * @param index * @param item */ var replace = function (index, item) { setList(function (l) { var temp = _tslib.__spreadArray([], _tslib.__read(l), false); temp[index] = item; return temp; }); }; /** * 删除指定位置的数据 * @param index */ var remove = function (index) { setList(function (l) { var temp = _tslib.__spreadArray([], _tslib.__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 = _tslib.__spreadArray([], _tslib.__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: index(insert), merge: index(merge), replace: index(replace), remove: index(remove), getKey: index(getKey), getIndex: index(getIndex), move: index(move), push: index(push), pop: index(pop), unshift: index(unshift), shift: index(shift), sort: index(sort), reset: index(reset), }; } module.exports = useDynamicList;