1- import { toJS } from 'mobx' ;
21import { observer } from 'mobx-react' ;
32import { FormComponent , FormComponentProps } from 'mobx-react-helper' ;
43import { DataObject } from 'mobx-restful' ;
@@ -20,20 +19,10 @@ export class ArrayField<T extends DataObject = DataObject> extends FormComponent
2019> {
2120 static displayName = 'ArrayField' ;
2221
23- protected readonly rowIds = new WeakMap < object , string > ( ) ;
22+ protected readonly rowIds : string [ ] = [ ] ;
2423
25- protected readRowId ( item : T ) {
26- return this . rowIds . get ( item as object ) ;
27- }
28-
29- protected writeRowId ( item : T , rowId = uniqueID ( ) ) {
30- this . rowIds . set ( item as object , rowId ) ;
31-
32- return rowId ;
33- }
34-
35- protected getRowId ( item : T ) {
36- return this . readRowId ( item ) || this . writeRowId ( item ) ;
24+ protected getRowId ( index : number ) {
25+ return ( this . rowIds [ index ] ||= uniqueID ( ) ) ;
3726 }
3827
3928 componentDidMount ( ) {
@@ -46,46 +35,49 @@ export class ArrayField<T extends DataObject = DataObject> extends FormComponent
4635 const { innerValue = [ ] } = this ,
4736 item = { } as T ;
4837
49- this . writeRowId ( item ) ;
38+ this . rowIds . splice ( index , 0 , uniqueID ( ) ) ;
5039 this . innerValue = [ ...innerValue . slice ( 0 , index ) , item , ...innerValue . slice ( index ) ] ;
5140 } ;
5241
53- remove = ( index : number ) => ( this . innerValue = this . innerValue ?. filter ( ( _ , i ) => i !== index ) ) ;
42+ remove = ( index : number ) => {
43+ this . rowIds . splice ( index , 1 ) ;
44+
45+ this . innerValue = this . innerValue ?. filter ( ( _ , i ) => i !== index ) ;
46+ } ;
5447
5548 handleChange =
5649 ( index : number ) =>
5750 ( { currentTarget } : ChangeEvent < EventTarget > ) => {
5851 const item = formToJSON < T > ( currentTarget as HTMLFieldSetElement ) ,
5952 { innerValue = [ ] } = this ;
60- const currentItem = innerValue [ index ] ;
61-
62- this . writeRowId ( item , this . readRowId ( currentItem ) ) ;
6353
6454 this . innerValue = [ ...innerValue . slice ( 0 , index ) , item , ...innerValue . slice ( index + 1 ) ] ;
6555 } ;
6656
6757 handleUpdate =
6858 ( index : number ) =>
6959 ( { currentTarget } : ChangeEvent < EventTarget > ) => {
70- const item = formToJSON < T > ( currentTarget as HTMLFieldSetElement ) ,
71- currentItem = this . innerValue ! [ index ] ;
60+ const item = formToJSON < T > ( currentTarget as HTMLFieldSetElement ) ;
7261
73- this . writeRowId ( item , this . readRowId ( currentItem ) ) ;
7462 this . innerValue ! [ index ] = item ;
7563 } ;
7664
7765 render ( ) {
78- const { className = '' , style, name, renderItem } = this . props ;
66+ const { className = '' , style, name, renderItem } = this . props ,
67+ { value, rowIds } = this ;
68+ const length = value ?. length || 0 ;
69+
70+ if ( rowIds . length > length ) rowIds . length = length ;
7971
8072 return (
8173 < >
82- { this . value ?. map ( ( item , index , { length } ) => (
74+ { value ?. map ( ( item , index , { length } ) => (
8375 < fieldset
84- key = { this . getRowId ( item ) }
76+ key = { this . getRowId ( index ) }
8577 className = { `d-flex align-items-center my-2 gap-2 ${ className } ` }
8678 { ...{ style, name } }
87- onChange = { this . handleChange ( index ) }
88- onBlur = { this . handleUpdate ( index ) }
79+ onBlur = { this . handleChange ( index ) }
80+ onChange = { this . handleUpdate ( index ) }
8981 >
9082 < div className = "flex-fill" > { renderItem ( item , index ) } </ div >
9183 < ButtonGroup >
0 commit comments