From f6083270d0107439c9d59cbb495f1c1567d97514 Mon Sep 17 00:00:00 2001 From: SunLxy <1011771396@qq.com> Date: Tue, 11 Jul 2023 17:54:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8Dform=20list=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E7=A7=BB=E5=8A=A8=E6=B8=B2=E6=9F=93=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/Form/formList.tsx | 67 ++++++++++++++++++++++------- packages/core/src/utils/utils.ts | 28 ++++++++++++ 2 files changed, 80 insertions(+), 15 deletions(-) diff --git a/packages/core/src/Form/formList.tsx b/packages/core/src/Form/formList.tsx index 71a4468f4..7725a64a3 100644 --- a/packages/core/src/Form/formList.tsx +++ b/packages/core/src/Form/formList.tsx @@ -1,4 +1,4 @@ -import React, { useContext } from 'react'; +import React, { useContext, useRef } from 'react'; import { KeyType, FormItemsProps } from './types'; import { isObjectEmpty } from './utils/is'; import { Context } from './hooks/context'; @@ -9,6 +9,7 @@ import { View, SafeAreaView } from 'react-native'; import styles from './styles'; import { useTheme } from '@shopify/restyle'; import { Theme } from '../theme'; +import { move } from '../utils/utils'; interface FormListProps { formListValue: FormItemsProps; @@ -28,35 +29,62 @@ const FormList = ({ const theme = useTheme(); const { field, items = [], renderAdd, renderHeader } = formListValue; + const keyRef = useRef<{ keys: number[]; id: number }>({ + keys: [], + id: 0, + }); + + const keyManager = keyRef.current; + + console.log('keyManager', keyRef); + const handleOperate = (type = '', index: number) => { let list = store[field] || []; - if (type === 'add') list.push({}); - if (type === 'delete') list.splice(index, 1); + if (type === 'add') { + keyManager.id += 1; + list.push({}); + } + if (type === 'delete') { + keyManager.keys = keyManager.keys.filter((_, keysIndex) => index !== keysIndex); + list.splice(index, 1); + } // 下移 if (type === 'moveDown') { if (index !== list.length - 1) { + keyManager.keys = move(keyManager.keys, index, index + 1); list[index] = list.splice(index + 1, 1, list[index])[0]; } else { + keyManager.keys = move(keyManager.keys, index, 0); + list.unshift(list.splice(index, 1)[0]); } } // 上移 if (type === 'moveUp') { if (index !== 0) { + keyManager.keys = move(keyManager.keys, index, index - 1); + list[index] = list.splice(index - 1, 1, list[index])[0]; } else { + keyManager.keys = move(keyManager.keys, index, 0); + list.push(list.shift()); } } // 置顶 if (type === 'moveToTop') { if (index !== 0) { + keyManager.keys = move(keyManager.keys, index, 0); list.unshift(list.splice(index, 1)[0]); } } // 置底 if (type === 'moveToBottom') { if (index !== list.length - 1) { + const lastIndex = keyManager.keys.length - 1; + + keyManager.keys = move(keyManager.keys, index, lastIndex); + list.push(list.splice(index, 1)[0]); } } @@ -97,6 +125,7 @@ const FormList = ({ if (v.type === 'cardList') { return; } + return ( @@ -111,18 +140,26 @@ const FormList = ({ return ( - {(store[field] || []).map((item: Record, index: number) => ( - - {renderHeader?.(index, { - remove: () => handleOperate('delete', index), - moveUp: () => handleOperate('moveUp', index), - moveDown: () => handleOperate('moveDown', index), - moveToTop: () => handleOperate('moveToTop', index), - moveToBottom: () => handleOperate('moveToBottom', index), - })} - {_render(index)} - - ))} + {(store[field] || []).map((item: Record, index: number) => { + let key = keyManager.keys[index]; + if (key === undefined) { + keyManager.keys[index] = keyManager.id; + key = keyManager.keys[index]; + keyManager.id += 1; + } + return ( + + {renderHeader?.(index, { + remove: () => handleOperate('delete', index), + moveUp: () => handleOperate('moveUp', index), + moveDown: () => handleOperate('moveDown', index), + moveToTop: () => handleOperate('moveToTop', index), + moveToBottom: () => handleOperate('moveToBottom', index), + })} + {_render(index)} + + ); + })} {renderAdd?.({ add: () => handleOperate('add', 0) })} ); diff --git a/packages/core/src/utils/utils.ts b/packages/core/src/utils/utils.ts index 15857e3e6..b7f98b210 100644 --- a/packages/core/src/utils/utils.ts +++ b/packages/core/src/utils/utils.ts @@ -31,3 +31,31 @@ export function arrayTreeFilter( * @returns */ export const last = (length: number, i: number) => length - 1 === i; + +/**移动数据*/ +export const move = (dataArray: T[], moveIndex: number, toIndex: number) => { + const { length } = dataArray; + if (moveIndex < 0 || moveIndex >= length || toIndex < 0 || toIndex >= length) { + return dataArray; + } + const item = dataArray[moveIndex]; + const diff = moveIndex - toIndex; + + if (diff > 0) { + return [ + ...dataArray.slice(0, toIndex), + item, + ...dataArray.slice(toIndex, moveIndex), + ...dataArray.slice(moveIndex + 1, length), + ]; + } + if (diff < 0) { + return [ + ...dataArray.slice(0, moveIndex), + ...dataArray.slice(moveIndex + 1, toIndex + 1), + item, + ...dataArray.slice(toIndex + 1, length), + ]; + } + return dataArray; +};