@@ -4,12 +4,44 @@ import isArray from 'lodash/lang/isArray';
4
4
import isFunction from 'lodash/lang/isFunction' ;
5
5
import set from 'lodash/object/set' ;
6
6
import get from 'lodash/object/get' ;
7
+ import forOwn from 'lodash/object/forOwn' ;
7
8
import traverse from './utils/traverse' ;
8
-
9
9
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' ] ) {
17
+ return child ;
18
+ }
19
+
20
+ const newProps = { } ;
21
+ let extendedCount = 0 ;
22
+
23
+ forOwn ( props , ( fn , key ) => {
24
+ if ( typeof fn !== 'function' || fn . _extended ) {
25
+ return ;
26
+ }
27
+
28
+ extendedCount ++ ;
29
+
30
+ const newFn = ( ...args ) => fn ( ...args , index ) ;
31
+ newFn . _extended = true ;
32
+
33
+ newProps [ key ] = newFn ;
34
+ } ) ;
35
+
36
+ if ( extendedCount ) {
37
+ newProps [ 'data-extended' ] = true ;
38
+ const newChild = cloneElement ( child , newProps ) ;
39
+ return newChild ;
40
+ }
41
+
42
+ return child ;
43
+ }
44
+
13
45
export default class Fieldset extends Element {
14
46
static isElement = true ;
15
47
@@ -18,10 +50,12 @@ export default class Fieldset extends Element {
18
50
onChange : PropTypes . func ,
19
51
map : PropTypes . bool . isRequired ,
20
52
index : PropTypes . number ,
53
+ extend : PropTypes . bool . isRequired ,
21
54
} ;
22
55
23
56
static defaultProps = {
24
57
map : true ,
58
+ extend : false ,
25
59
} ;
26
60
27
61
getValue ( name ) {
@@ -60,18 +94,20 @@ export default class Fieldset extends Element {
60
94
}
61
95
62
96
_registerChildren ( children , topLevel ) {
63
- const { value, map } = this . props ;
97
+ const { value, map, index , extend } = this . props ;
64
98
65
99
if ( topLevel && map && isArray ( value ) ) {
66
100
return value . map ( ( currentValue , index ) => {
67
101
return this . _registerChildren ( (
68
- < Fieldset name = { index } key = { index } index = { index } >
102
+ < Fieldset name = { index } key = { index } index = { index } extend = { extend } >
69
103
{ children }
70
104
</ Fieldset >
71
105
) ) ;
72
106
} ) ;
73
107
}
74
108
109
+ const hasIndex = typeof index !== 'undefined' ;
110
+
75
111
return traverse ( children , ( child ) => {
76
112
if ( ! isFunction ( child . type ) || ! child . type . isElement ) {
77
113
return void 0 ;
@@ -90,7 +126,14 @@ export default class Fieldset extends Element {
90
126
onChange : ( value , component ) => this . setValue ( child . props . name , value , component ) ,
91
127
} ) ;
92
128
} , ( child ) => {
93
- const { replace } = this . getFormProps ( ) ;
129
+ const { replace, extend : formExtend } = this . getFormProps ( ) ;
130
+ if ( hasIndex && extend && formExtend ) {
131
+ const updatedChild = extendCallbacks ( child , index ) ;
132
+ if ( updatedChild !== child ) {
133
+ return updatedChild ;
134
+ }
135
+ }
136
+
94
137
if ( ! replace ) {
95
138
return void 0 ;
96
139
}
0 commit comments