1
- import React , { useContext } from 'react' ;
1
+ import React , { useContext , useRef } from 'react' ;
2
2
import { KeyType , FormItemsProps } from './types' ;
3
3
import { isObjectEmpty } from './utils/is' ;
4
4
import { Context } from './hooks/context' ;
@@ -9,6 +9,7 @@ import { View, SafeAreaView } from 'react-native';
9
9
import styles from './styles' ;
10
10
import { useTheme } from '@shopify/restyle' ;
11
11
import { Theme } from '../theme' ;
12
+ import { move } from '../utils/utils' ;
12
13
13
14
interface FormListProps {
14
15
formListValue : FormItemsProps ;
@@ -28,35 +29,62 @@ const FormList = ({
28
29
const theme = useTheme < Theme > ( ) ;
29
30
const { field, items = [ ] , renderAdd, renderHeader } = formListValue ;
30
31
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
+
31
41
const handleOperate = ( type = '' , index : number ) => {
32
42
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
+ }
35
51
// 下移
36
52
if ( type === 'moveDown' ) {
37
53
if ( index !== list . length - 1 ) {
54
+ keyManager . keys = move ( keyManager . keys , index , index + 1 ) ;
38
55
list [ index ] = list . splice ( index + 1 , 1 , list [ index ] ) [ 0 ] ;
39
56
} else {
57
+ keyManager . keys = move ( keyManager . keys , index , 0 ) ;
58
+
40
59
list . unshift ( list . splice ( index , 1 ) [ 0 ] ) ;
41
60
}
42
61
}
43
62
// 上移
44
63
if ( type === 'moveUp' ) {
45
64
if ( index !== 0 ) {
65
+ keyManager . keys = move ( keyManager . keys , index , index - 1 ) ;
66
+
46
67
list [ index ] = list . splice ( index - 1 , 1 , list [ index ] ) [ 0 ] ;
47
68
} else {
69
+ keyManager . keys = move ( keyManager . keys , index , 0 ) ;
70
+
48
71
list . push ( list . shift ( ) ) ;
49
72
}
50
73
}
51
74
// 置顶
52
75
if ( type === 'moveToTop' ) {
53
76
if ( index !== 0 ) {
77
+ keyManager . keys = move ( keyManager . keys , index , 0 ) ;
54
78
list . unshift ( list . splice ( index , 1 ) [ 0 ] ) ;
55
79
}
56
80
}
57
81
// 置底
58
82
if ( type === 'moveToBottom' ) {
59
83
if ( index !== list . length - 1 ) {
84
+ const lastIndex = keyManager . keys . length - 1 ;
85
+
86
+ keyManager . keys = move ( keyManager . keys , index , lastIndex ) ;
87
+
60
88
list . push ( list . splice ( index , 1 ) [ 0 ] ) ;
61
89
}
62
90
}
@@ -97,6 +125,7 @@ const FormList = ({
97
125
if ( v . type === 'cardList' ) {
98
126
return ;
99
127
}
128
+
100
129
return (
101
130
< View key = { i } style = { styles . form_items_container } >
102
131
< View style = { [ styles . form_items ] } >
@@ -111,18 +140,26 @@ const FormList = ({
111
140
112
141
return (
113
142
< 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
+ } ) }
126
163
{ renderAdd ?.( { add : ( ) => handleOperate ( 'add' , 0 ) } ) }
127
164
</ SafeAreaView >
128
165
) ;
0 commit comments