@@ -26,6 +26,10 @@ export type CustomListAction<CompCtor extends CompConstructor = CompConstructor>
26
26
> ;
27
27
28
28
type ListAction < CompCtor extends CompConstructor = CompConstructor > =
29
+ | {
30
+ type : "setChildrens" ;
31
+ value : Array < ConstructorToDataType < CompCtor > > ;
32
+ }
29
33
| {
30
34
type : "push" ;
31
35
value : ConstructorToDataType < CompCtor > ;
@@ -145,7 +149,19 @@ export function list<ChildCompCtor extends CompConstructor<any, any>>(
145
149
146
150
private reduceCustom ( action : ListAction < ChildCompCtor > ) : this {
147
151
switch ( action . type ) {
148
- case "push" :
152
+ case "setChildrens" : {
153
+ const childrenMap : Record < number , ConstructorToComp < ChildCompCtor > > = { } ;
154
+ const childrenOrder : number [ ] = [ ] ;
155
+ action . value . forEach ( ( children : any , key : number ) => {
156
+ childrenMap [ key ] = newChild ( this . dispatch , String ( key ) , children ) ;
157
+ childrenOrder . push ( key ) ;
158
+ } )
159
+ return setFieldsNoTypeCheck ( this , {
160
+ children : childrenMap ,
161
+ childrenOrder : childrenOrder ,
162
+ } ) ;
163
+ }
164
+ case "push" : {
149
165
const key = this . genKey ( ) ;
150
166
const newChildren = {
151
167
...this . children ,
@@ -155,6 +171,7 @@ export function list<ChildCompCtor extends CompConstructor<any, any>>(
155
171
children : newChildren ,
156
172
childrenOrder : [ ...this . childrenOrder , key ] ,
157
173
} ) ;
174
+ }
158
175
case "pushComp" : {
159
176
const key = this . genKey ( ) ;
160
177
const newChildren = {
@@ -209,6 +226,17 @@ export function list<ChildCompCtor extends CompConstructor<any, any>>(
209
226
}
210
227
}
211
228
}
229
+
230
+ setChildrensAction ( value : Array < ConstructorToDataType < ChildCompCtor > > ) {
231
+ return customAction < ListAction < ChildCompCtor > > (
232
+ {
233
+ type : "setChildrens" ,
234
+ value : value ,
235
+ } ,
236
+ true
237
+ ) ;
238
+ }
239
+
212
240
pushAction ( value : ConstructorToDataType < ChildCompCtor > ) {
213
241
return customAction < ListAction < ChildCompCtor > > (
214
242
{
0 commit comments