@@ -6,11 +6,13 @@ export interface StoreContextValue<Tag extends BlockTagType> extends InitialStat
66
77export type BlockTagType = keyof JSX . IntrinsicElements ;
88
9+ export type Index = Record < string , number > ;
910export type Fields = React . ReactElement < HTMLInputElement & { index ?: number ; } > ;
1011export type Buttons = React . ReactElement < HTMLButtonElement & { index ?: number ; } > ;
1112export type Blocks < Tag extends BlockTagType = 'div' > = React . ReactElement < Tag & { index ?: number ; } > ;
1213
1314export interface RenderStateProps < T extends BlockTagType = 'div' > {
15+ $$index ?: Record < string , number > ;
1416 fields ?: Record < string , Fields | null > ;
1517 buttons ?: Record < string , Buttons | null > ;
1618 blocks ?: Record < string , Blocks < T > | null > ;
@@ -33,6 +35,7 @@ export interface InitialState<Tag extends BlockTagType = 'div'> extends RenderSt
3335}
3436
3537export const initialState : InitialState = {
38+ index : { } ,
3639 fields : { } ,
3740 buttons : { } ,
3841 blocks : { } ,
@@ -51,24 +54,25 @@ export function reducer(state: InitialState, action: Partial<RenderStateProps>):
5154 const result = {
5255 ...state ,
5356 ...action ,
57+ $$index : { ...state . $$index , ...action . $$index } ,
5458 fields : { ...state . fields , ...action . fields } ,
5559 buttons : { ...state . buttons , ...action . buttons } ,
5660 blocks : { ...state . blocks , ...action . blocks } ,
5761 extra : { ...state . extra , ...action . extra } ,
5862 }
59- const fieldsArray = Object . keys ( result . fields ) . map ( ( key , index ) => ( {
63+ const fieldsArray = Object . keys ( result . fields ) . map ( ( key ) => ( {
6064 name : key ,
61- index : result . fields [ key ] ?. props ?. index || 0 ,
65+ index : result . fields [ key ] ?. props ?. index || ( result . $$index || { } ) [ key ] || 0 ,
6266 children : result . fields [ key ] ,
6367 } ) ) ;
64- const buttonsArray = Object . keys ( result . buttons ) . map ( ( key , index ) => ( {
68+ const buttonsArray = Object . keys ( result . buttons ) . map ( ( key ) => ( {
6569 name : key ,
66- index : result . buttons [ key ] ?. props ?. index || 0 ,
70+ index : result . buttons [ key ] ?. props ?. index || ( result . $$index || { } ) [ key ] || 0 ,
6771 children : result . buttons [ key ] ,
6872 } ) ) ;
69- const blocksArray = Object . keys ( result . blocks ) . map ( ( key , index ) => ( {
73+ const blocksArray = Object . keys ( result . blocks ) . map ( ( key ) => ( {
7074 name : key ,
71- index : result . blocks [ key ] ?. props ?. index || 0 ,
75+ index : result . blocks [ key ] ?. props ?. index || ( result . $$index || { } ) [ key ] || 0 ,
7276 children : result . blocks [ key ] ,
7377 } ) ) ;
7478 return { ...result , data : { ...result . data , fields : fieldsArray , buttons : buttonsArray , blocks : blocksArray } } ;
0 commit comments