@@ -10,33 +10,67 @@ import Input from './Input';
10
10
import Select from './Select' ;
11
11
import Textarea from './Textarea' ;
12
12
13
- function extendCallbacks ( child , index ) {
14
- const props = child . props ;
15
-
16
- if ( typeof index === 'undefined' || props [ 'data-extended' ] ) {
13
+ function extendChild ( child , parent ) {
14
+ const { index } = parent . props ;
15
+ if ( typeof index === 'undefined' ) {
17
16
return child ;
18
17
}
19
18
19
+ const { addIndex, remove, up, down, onClick } = child . props ;
20
20
const newProps = { } ;
21
- let extendedCount = 0 ;
21
+ let changed = false ;
22
22
23
- forOwn ( props , ( fn , key ) => {
24
- if ( typeof fn !== 'function' || fn . _extended ) {
25
- return ;
26
- }
27
23
28
- extendedCount ++ ;
24
+ if ( addIndex ) {
25
+ newProps . addIndex = null ;
26
+ changed = true ;
27
+
28
+ forOwn ( child . props , ( fn , key ) => {
29
+ if ( typeof fn === 'function' ) {
30
+ newProps [ key ] = ( ...args ) => fn ( index , ...args ) ;
31
+ }
32
+ } ) ;
33
+ }
34
+
35
+ const onClickBefore = newProps . onClick || onClick ;
36
+
37
+ if ( remove ) {
38
+ newProps . remove = null ;
39
+ changed = true ;
29
40
30
- const newFn = ( ... args ) => fn ( index , ... args ) ;
31
- newFn . _extended = true ;
41
+ newProps . onClick = ( ) => {
42
+ parent . remove ( index ) ;
32
43
33
- newProps [ key ] = newFn ;
34
- } ) ;
44
+ if ( onClickBefore ) {
45
+ onClickBefore ( ) ;
46
+ }
47
+ } ;
48
+ } else if ( up ) {
49
+ newProps . up = null ;
50
+ changed = true ;
51
+
52
+ newProps . onClick = ( ) => {
53
+ parent . up ( index ) ;
35
54
36
- if ( extendedCount ) {
37
- newProps [ 'data-extended' ] = true ;
38
- const newChild = cloneElement ( child , newProps ) ;
39
- return newChild ;
55
+ if ( onClickBefore ) {
56
+ onClickBefore ( ) ;
57
+ }
58
+ } ;
59
+ } else if ( down ) {
60
+ newProps . down = null ;
61
+ changed = true ;
62
+
63
+ newProps . onClick = ( ) => {
64
+ parent . down ( index ) ;
65
+
66
+ if ( onClickBefore ) {
67
+ onClickBefore ( ) ;
68
+ }
69
+ } ;
70
+ }
71
+
72
+ if ( changed ) {
73
+ return cloneElement ( child , newProps ) ;
40
74
}
41
75
42
76
return child ;
@@ -50,7 +84,6 @@ export default class Fieldset extends Element {
50
84
onChange : PropTypes . func ,
51
85
map : PropTypes . bool . isRequired ,
52
86
index : PropTypes . number ,
53
- addIndex : PropTypes . bool ,
54
87
} ;
55
88
56
89
static defaultProps = {
@@ -69,8 +102,7 @@ export default class Fieldset extends Element {
69
102
|| props . className !== nextProps . className
70
103
|| props . value !== nextProps . value
71
104
|| props . map !== nextProps . map
72
- || props . index !== nextProps . index
73
- || props . addIndex !== nextProps . addIndex ) ;
105
+ || props . index !== nextProps . index ) ;
74
106
}
75
107
76
108
remove ( index ) {
@@ -212,20 +244,18 @@ export default class Fieldset extends Element {
212
244
_registerChildren ( children , topLevel ) {
213
245
this . smartUpdate = true ;
214
246
215
- const { value, map, index, addIndex } = this . props ;
247
+ const { value, map, index } = this . props ;
216
248
217
249
if ( topLevel && map && isArray ( value ) ) {
218
250
return value . map ( ( val , index ) => {
219
251
return this . _registerChildren ( (
220
- < Fieldset name = { index } key = { index } index = { index } addIndex = { addIndex } >
252
+ < Fieldset name = { index } key = { index } index = { index } >
221
253
{ children }
222
254
</ Fieldset >
223
255
) ) ;
224
256
} ) ;
225
257
}
226
258
227
- const hasIndex = typeof index !== 'undefined' ;
228
-
229
259
return traverse ( children , ( child ) => {
230
260
if ( ! isFunction ( child . type ) || ! child . type . isElement ) {
231
261
return void 0 ;
@@ -247,32 +277,12 @@ export default class Fieldset extends Element {
247
277
onChange : ( value , component ) => this . setValue ( name , value , component ) ,
248
278
} ) ;
249
279
} , ( child ) => {
250
- const { replace } = this . getFormProps ( ) ;
251
-
252
- if ( hasIndex ) {
253
- if ( child . props . remove ) {
254
- return cloneElement ( child , {
255
- remove : null ,
256
- onClick : ( ) => this . remove ( index ) ,
257
- } ) ;
258
- } else if ( child . props . up ) {
259
- return cloneElement ( child , {
260
- up : null ,
261
- onClick : ( ) => this . up ( index ) ,
262
- } ) ;
263
- } else if ( child . props . down ) {
264
- return cloneElement ( child , {
265
- down : null ,
266
- onClick : ( ) => this . down ( index ) ,
267
- } ) ;
268
- } else if ( addIndex || child . props . addIndex ) {
269
- const updatedChild = extendCallbacks ( child , index ) ;
270
- if ( updatedChild !== child ) {
271
- return updatedChild ;
272
- }
273
- }
280
+ const updatedChild = extendChild ( child , this ) ;
281
+ if ( updatedChild !== child ) {
282
+ return updatedChild ;
274
283
}
275
284
285
+ const { replace } = this . getFormProps ( ) ;
276
286
if ( ! replace ) {
277
287
return void 0 ;
278
288
}
0 commit comments