@@ -4,12 +4,44 @@ import isArray from 'lodash/lang/isArray';
44import isFunction from 'lodash/lang/isFunction' ;
55import set from 'lodash/object/set' ;
66import get from 'lodash/object/get' ;
7+ import forOwn from 'lodash/object/forOwn' ;
78import traverse from './utils/traverse' ;
8-
99import Input from './Input' ;
1010import Select from './Select' ;
1111import Textarea from './Textarea' ;
1212
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+
1345export default class Fieldset extends Element {
1446 static isElement = true ;
1547
@@ -18,10 +50,12 @@ export default class Fieldset extends Element {
1850 onChange : PropTypes . func ,
1951 map : PropTypes . bool . isRequired ,
2052 index : PropTypes . number ,
53+ extend : PropTypes . bool . isRequired ,
2154 } ;
2255
2356 static defaultProps = {
2457 map : true ,
58+ extend : false ,
2559 } ;
2660
2761 getValue ( name ) {
@@ -60,18 +94,20 @@ export default class Fieldset extends Element {
6094 }
6195
6296 _registerChildren ( children , topLevel ) {
63- const { value, map } = this . props ;
97+ const { value, map, index , extend } = this . props ;
6498
6599 if ( topLevel && map && isArray ( value ) ) {
66100 return value . map ( ( currentValue , index ) => {
67101 return this . _registerChildren ( (
68- < Fieldset name = { index } key = { index } index = { index } >
102+ < Fieldset name = { index } key = { index } index = { index } extend = { extend } >
69103 { children }
70104 </ Fieldset >
71105 ) ) ;
72106 } ) ;
73107 }
74108
109+ const hasIndex = typeof index !== 'undefined' ;
110+
75111 return traverse ( children , ( child ) => {
76112 if ( ! isFunction ( child . type ) || ! child . type . isElement ) {
77113 return void 0 ;
@@ -90,7 +126,14 @@ export default class Fieldset extends Element {
90126 onChange : ( value , component ) => this . setValue ( child . props . name , value , component ) ,
91127 } ) ;
92128 } , ( 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+
94137 if ( ! replace ) {
95138 return void 0 ;
96139 }
0 commit comments