1- import React , { useContext } from 'react' ;
1+ import React , { useContext , useRef } from 'react' ;
22import { KeyType , FormItemsProps } from './types' ;
33import { isObjectEmpty } from './utils/is' ;
44import { Context } from './hooks/context' ;
@@ -9,6 +9,7 @@ import { View, SafeAreaView } from 'react-native';
99import styles from './styles' ;
1010import { useTheme } from '@shopify/restyle' ;
1111import { Theme } from '../theme' ;
12+ import { move } from '../utils/utils' ;
1213
1314interface 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 ) ;
0 commit comments