1
- import { useState } from "react" ;
1
+ import { useEffect , useState } from "react" ;
2
2
import {
3
3
antd ,
4
4
styleControl ,
@@ -97,24 +97,59 @@ let StoneParcelComp = new UICompBuilder(
97
97
{ ...initValue } ,
98
98
] ) ;
99
99
100
+ useEffect ( ( ) => {
101
+ console . log ( props . data . value ) ;
102
+ if ( props . data . length ) {
103
+ setParcelValues ( props . data . value ) ;
104
+ }
105
+ } , [ props . data , parcelValues ] ) ;
106
+
107
+ // convenient function, so that we don't need to pass index to child component
108
+ const closureHandleChange =
109
+ ( i : number , parcelValue : StoneValue ) => ( k : string , v : string | number ) =>
110
+ handleChange ( i , { ...parcelValue , [ k ] : v } ) ;
100
111
const handleChange = ( key : number , value : StoneValue ) => {
101
112
setParcelValues ( ( parcelValues : StoneValue [ ] ) : StoneValue [ ] => {
102
- return parcelValues . map (
113
+ const newParcelValues = parcelValues . map (
103
114
( parcelValue : StoneValue , i : number ) : StoneValue => {
104
115
if ( i === key ) {
105
116
return value ;
106
117
}
107
118
return parcelValue ;
108
119
} ,
109
120
) ;
121
+ props . data . onChange ( newParcelValues ) ;
122
+ props . onEvent ( "change" ) ;
123
+ return newParcelValues ;
124
+ } ) ;
125
+ } ;
126
+
127
+ const handleAdd = ( ) => {
128
+ setParcelValues ( ( parcelValues : StoneValue [ ] ) => {
129
+ const newParcelValues = [ ...parcelValues , initValue ] ;
130
+ props . data . onChange ( newParcelValues ) ;
131
+ props . onEvent ( "change" ) ;
132
+ return newParcelValues ;
110
133
} ) ;
111
- props . data . onChange ( parcelValues ) ;
112
- props . onEvent ( "change" ) ;
113
134
} ;
114
- const handleClose = ( i : number ) => {
115
- setParcelValues ( parcelValues . toSpliced ( i , 1 ) ) ;
116
- props . data . onChange ( parcelValues ) ;
117
- props . onEvent ( "change" ) ;
135
+
136
+ // convenient function, so that we don't need to pass index to child component
137
+ const closureHandleClose = ( i : number ) => ( ) => handleClose ( i ) ;
138
+ const handleClose = ( key : number ) => {
139
+ setParcelValues ( ( parcelValues : StoneValue [ ] ) : StoneValue [ ] => {
140
+ const newParcelValues = parcelValues . reduce (
141
+ ( acc : StoneValue [ ] , curr : StoneValue , i : number ) : StoneValue [ ] => {
142
+ if ( i !== key ) {
143
+ acc . push ( curr ) ;
144
+ }
145
+ return acc ;
146
+ } ,
147
+ [ ] ,
148
+ ) ;
149
+ props . data . onChange ( newParcelValues ) ;
150
+ props . onEvent ( "change" ) ;
151
+ return newParcelValues ;
152
+ } ) ;
118
153
} ;
119
154
120
155
const [ dimensions , setDimensions ] = useState ( { width : 480 , height : 280 } ) ;
@@ -141,17 +176,15 @@ let StoneParcelComp = new UICompBuilder(
141
176
{ parcelValues . map ( ( parcelValue , i ) => (
142
177
< StoneParcel
143
178
key = { i }
144
- i = { i }
145
- parcelValue = { parcelValue }
146
- handleChange = { handleChange }
147
- handleClose = { handleClose }
179
+ handleChange = { closureHandleChange ( i , parcelValue ) }
180
+ handleClose = { closureHandleClose ( i ) }
148
181
/>
149
182
) ) }
150
183
< antd . Button
151
184
type = "primary"
152
185
shape = "circle"
153
186
icon = { < PlusOutlined /> }
154
- onClick = { ( ) => setParcelValues ( [ ... parcelValues , initValue ] ) }
187
+ onClick = { handleAdd }
155
188
> </ antd . Button >
156
189
</ Container >
157
190
) ;
0 commit comments