@@ -9,11 +9,22 @@ import { Field } from './Field'
9
9
import { Form } from './Form'
10
10
import { JSXComponent , IFieldProps , FormPathPattern } from '../types'
11
11
12
+ const uniqueIdRef = { current : 0 }
13
+
14
+ const getUniqueId = ( ) => {
15
+ return uniqueIdRef . current ++
16
+ }
17
+
18
+ const createIndexKey = ( ) => {
19
+ return `_$id_${ getUniqueId ( ) } _`
20
+ }
21
+
12
22
export class ArrayField <
13
23
Decorator extends JSXComponent = any ,
14
24
Component extends JSXComponent = any
15
25
> extends Field < Decorator , Component , any , any [ ] > {
16
26
displayName = 'ArrayField'
27
+ indexKeys : Array < string > = [ ]
17
28
18
29
constructor (
19
30
address : FormPathPattern ,
@@ -22,6 +33,7 @@ export class ArrayField<
22
33
designable : boolean
23
34
) {
24
35
super ( address , props , form , designable )
36
+ this . indexKeys = [ ]
25
37
this . makeAutoCleanable ( )
26
38
}
27
39
@@ -32,20 +44,37 @@ export class ArrayField<
32
44
( newLength , oldLength ) => {
33
45
if ( oldLength && ! newLength ) {
34
46
cleanupArrayChildren ( this , 0 )
47
+ this . indexKeys = [ ]
35
48
} else if ( newLength < oldLength ) {
36
49
cleanupArrayChildren ( this , newLength )
50
+ this . indexKeys = this . indexKeys . slice ( 0 , newLength )
37
51
}
38
52
}
39
53
)
40
54
)
41
55
}
42
56
57
+ getIndexKey ( index : number ) {
58
+ if ( ! this . indexKeys [ index ] ) {
59
+ const newKey = createIndexKey ( )
60
+ this . indexKeys [ index ] = newKey
61
+ return newKey
62
+ }
63
+ return this . indexKeys [ index ]
64
+ }
65
+
66
+ getCurrentKeyIndex ( key : string ) {
67
+ return this . indexKeys . indexOf ( key )
68
+ }
69
+
43
70
push = ( ...items : any [ ] ) => {
44
71
return action ( ( ) => {
45
72
if ( ! isArr ( this . value ) ) {
46
73
this . value = [ ]
74
+ this . indexKeys = [ ]
47
75
}
48
76
this . value . push ( ...items )
77
+ this . indexKeys . push ( ...items . map ( createIndexKey ) )
49
78
return this . onInput ( this . value )
50
79
} )
51
80
}
@@ -59,6 +88,7 @@ export class ArrayField<
59
88
deleteCount : 1 ,
60
89
} )
61
90
this . value . pop ( )
91
+ this . indexKeys . pop ( )
62
92
return this . onInput ( this . value )
63
93
} )
64
94
}
@@ -67,6 +97,7 @@ export class ArrayField<
67
97
return action ( ( ) => {
68
98
if ( ! isArr ( this . value ) ) {
69
99
this . value = [ ]
100
+ this . indexKeys = [ ]
70
101
}
71
102
if ( items . length === 0 ) {
72
103
return
@@ -76,6 +107,7 @@ export class ArrayField<
76
107
insertCount : items . length ,
77
108
} )
78
109
this . value . splice ( index , 0 , ...items )
110
+ this . indexKeys . splice ( index , 0 , ...items . map ( createIndexKey ) )
79
111
return this . onInput ( this . value )
80
112
} )
81
113
}
@@ -88,6 +120,7 @@ export class ArrayField<
88
120
deleteCount : 1 ,
89
121
} )
90
122
this . value . splice ( index , 1 )
123
+ this . indexKeys . splice ( index , 1 )
91
124
return this . onInput ( this . value )
92
125
} )
93
126
}
@@ -100,6 +133,7 @@ export class ArrayField<
100
133
deleteCount : 1 ,
101
134
} )
102
135
this . value . shift ( )
136
+ this . indexKeys . shift ( )
103
137
return this . onInput ( this . value )
104
138
} )
105
139
}
@@ -114,6 +148,7 @@ export class ArrayField<
114
148
insertCount : items . length ,
115
149
} )
116
150
this . value . unshift ( ...items )
151
+ this . indexKeys . unshift ( ...items . map ( createIndexKey ) )
117
152
return this . onInput ( this . value )
118
153
} )
119
154
}
@@ -123,6 +158,7 @@ export class ArrayField<
123
158
if ( fromIndex === toIndex ) return
124
159
return action ( ( ) => {
125
160
move ( this . value , fromIndex , toIndex )
161
+ move ( this . indexKeys , fromIndex , toIndex )
126
162
exchangeArrayState ( this , {
127
163
fromIndex,
128
164
toIndex,
0 commit comments