Skip to content

Commit f608327

Browse files
committedJul 11, 2023
fix:修复form list上下移动渲染问题
1 parent 49d36d9 commit f608327

File tree

2 files changed

+80
-15
lines changed

2 files changed

+80
-15
lines changed
 

‎packages/core/src/Form/formList.tsx

+52-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext } from 'react';
1+
import React, { useContext, useRef } from 'react';
22
import { KeyType, FormItemsProps } from './types';
33
import { isObjectEmpty } from './utils/is';
44
import { Context } from './hooks/context';
@@ -9,6 +9,7 @@ import { View, SafeAreaView } from 'react-native';
99
import styles from './styles';
1010
import { useTheme } from '@shopify/restyle';
1111
import { Theme } from '../theme';
12+
import { move } from '../utils/utils';
1213

1314
interface FormListProps {
1415
formListValue: FormItemsProps;
@@ -28,35 +29,62 @@ const FormList = ({
2829
const theme = useTheme<Theme>();
2930
const { field, items = [], renderAdd, renderHeader } = formListValue;
3031

32+
const keyRef = useRef<{ keys: number[]; id: number }>({
33+
keys: [],
34+
id: 0,
35+
});
36+
37+
const keyManager = keyRef.current;
38+
39+
console.log('keyManager', keyRef);
40+
3141
const handleOperate = (type = '', index: number) => {
3242
let list = store[field] || [];
33-
if (type === 'add') list.push({});
34-
if (type === 'delete') list.splice(index, 1);
43+
if (type === 'add') {
44+
keyManager.id += 1;
45+
list.push({});
46+
}
47+
if (type === 'delete') {
48+
keyManager.keys = keyManager.keys.filter((_, keysIndex) => index !== keysIndex);
49+
list.splice(index, 1);
50+
}
3551
// 下移
3652
if (type === 'moveDown') {
3753
if (index !== list.length - 1) {
54+
keyManager.keys = move(keyManager.keys, index, index + 1);
3855
list[index] = list.splice(index + 1, 1, list[index])[0];
3956
} else {
57+
keyManager.keys = move(keyManager.keys, index, 0);
58+
4059
list.unshift(list.splice(index, 1)[0]);
4160
}
4261
}
4362
// 上移
4463
if (type === 'moveUp') {
4564
if (index !== 0) {
65+
keyManager.keys = move(keyManager.keys, index, index - 1);
66+
4667
list[index] = list.splice(index - 1, 1, list[index])[0];
4768
} else {
69+
keyManager.keys = move(keyManager.keys, index, 0);
70+
4871
list.push(list.shift());
4972
}
5073
}
5174
// 置顶
5275
if (type === 'moveToTop') {
5376
if (index !== 0) {
77+
keyManager.keys = move(keyManager.keys, index, 0);
5478
list.unshift(list.splice(index, 1)[0]);
5579
}
5680
}
5781
// 置底
5882
if (type === 'moveToBottom') {
5983
if (index !== list.length - 1) {
84+
const lastIndex = keyManager.keys.length - 1;
85+
86+
keyManager.keys = move(keyManager.keys, index, lastIndex);
87+
6088
list.push(list.splice(index, 1)[0]);
6189
}
6290
}
@@ -97,6 +125,7 @@ const FormList = ({
97125
if (v.type === 'cardList') {
98126
return;
99127
}
128+
100129
return (
101130
<View key={i} style={styles.form_items_container}>
102131
<View style={[styles.form_items]}>
@@ -111,18 +140,26 @@ const FormList = ({
111140

112141
return (
113142
<SafeAreaView style={{ backgroundColor: theme.colors.background }}>
114-
{(store[field] || []).map((item: Record<string, unknown>, index: number) => (
115-
<React.Fragment key={index}>
116-
{renderHeader?.(index, {
117-
remove: () => handleOperate('delete', index),
118-
moveUp: () => handleOperate('moveUp', index),
119-
moveDown: () => handleOperate('moveDown', index),
120-
moveToTop: () => handleOperate('moveToTop', index),
121-
moveToBottom: () => handleOperate('moveToBottom', index),
122-
})}
123-
<Card>{_render(index)}</Card>
124-
</React.Fragment>
125-
))}
143+
{(store[field] || []).map((item: Record<string, unknown>, index: number) => {
144+
let key = keyManager.keys[index];
145+
if (key === undefined) {
146+
keyManager.keys[index] = keyManager.id;
147+
key = keyManager.keys[index];
148+
keyManager.id += 1;
149+
}
150+
return (
151+
<React.Fragment key={key}>
152+
{renderHeader?.(index, {
153+
remove: () => handleOperate('delete', index),
154+
moveUp: () => handleOperate('moveUp', index),
155+
moveDown: () => handleOperate('moveDown', index),
156+
moveToTop: () => handleOperate('moveToTop', index),
157+
moveToBottom: () => handleOperate('moveToBottom', index),
158+
})}
159+
<Card>{_render(index)}</Card>
160+
</React.Fragment>
161+
);
162+
})}
126163
{renderAdd?.({ add: () => handleOperate('add', 0) })}
127164
</SafeAreaView>
128165
);

‎packages/core/src/utils/utils.ts

+28
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,31 @@ export function arrayTreeFilter<T>(
3131
* @returns
3232
*/
3333
export const last = (length: number, i: number) => length - 1 === i;
34+
35+
/**移动数据*/
36+
export const move = <T = any>(dataArray: T[], moveIndex: number, toIndex: number) => {
37+
const { length } = dataArray;
38+
if (moveIndex < 0 || moveIndex >= length || toIndex < 0 || toIndex >= length) {
39+
return dataArray;
40+
}
41+
const item = dataArray[moveIndex];
42+
const diff = moveIndex - toIndex;
43+
44+
if (diff > 0) {
45+
return [
46+
...dataArray.slice(0, toIndex),
47+
item,
48+
...dataArray.slice(toIndex, moveIndex),
49+
...dataArray.slice(moveIndex + 1, length),
50+
];
51+
}
52+
if (diff < 0) {
53+
return [
54+
...dataArray.slice(0, moveIndex),
55+
...dataArray.slice(moveIndex + 1, toIndex + 1),
56+
item,
57+
...dataArray.slice(toIndex + 1, length),
58+
];
59+
}
60+
return dataArray;
61+
};

0 commit comments

Comments
 (0)
Please sign in to comment.