@@ -9,10 +9,7 @@ import traverse from './utils/traverse';
99import Input from './Input' ;
1010import Select from './Select' ;
1111import Textarea from './Textarea' ;
12-
13- function isNumeric ( value ) {
14- return ! isNaN ( parseFloat ( value ) ) && isFinite ( value ) ;
15- }
12+ import Index from './Index' ;
1613
1714export default class Fieldset extends Element {
1815 static isElement = true ;
@@ -21,30 +18,36 @@ export default class Fieldset extends Element {
2118 ...Element . propTypes ,
2219 onChange : PropTypes . func ,
2320 map : PropTypes . bool . isRequired ,
21+ index : PropTypes . number ,
2422 } ;
2523
2624 static defaultProps = {
2725 map : true ,
2826 } ;
2927
3028 getValue ( name ) {
31- const value = this . props . value || { } ;
29+ const { value = { } } = this . props ;
30+
31+ if ( typeof name === 'undefined' || name === null ) {
32+ return value ;
33+ }
34+
3235 return get ( value , name ) ;
3336 }
3437
35- setValue ( name , value ) {
38+ setValue ( name , value , component ) {
3639 const currentValue = this . props . value ;
3740 const newState = isArray ( currentValue )
3841 ? [ ...this . props . value ]
3942 : { ...this . props . value } ;
4043
4144 set ( newState , name , value ) ;
4245
43- this . props . onChange ( newState ) ;
46+ this . props . onChange ( newState , component ) ;
4447 }
4548
46- getPath ( name ) {
47- if ( ! name ) {
49+ buildPath ( name ) {
50+ if ( typeof name === 'undefined' || name === null ) {
4851 return void 0 ;
4952 }
5053
@@ -57,44 +60,11 @@ export default class Fieldset extends Element {
5760 return this . props . form . props ;
5861 }
5962
60- handleChange ( evn ) {
61- const target = evn . target ;
62- if ( ! target ) {
63- return ;
64- }
65-
66- const propertyName = target . getAttribute ( 'data-property' ) ;
67- if ( ! propertyName ) {
68- return ;
69- }
70-
71- // IE < 10 bug catcher
72- try {
73- evn . stopPropagation ( ) ;
74- } catch ( err ) {
75- console . log ( err . message ) ;
76- }
77-
78- let value = target . type === 'checkbox'
79- ? ! ! target . checked
80- : target . value ;
81-
82- if ( target . type === 'number' && isNumeric ( value ) ) {
83- // fix decimal numbers
84- const numberValue = Number ( value ) ;
85- if ( numberValue . toString ( ) === value ) {
86- value = numberValue ;
87- }
88- }
89-
90- this . setValue ( propertyName , value ) ;
91- }
92-
9363 _registerChildren ( children , topLevel ) {
9464 const { value, map } = this . props ;
9565
9666 if ( topLevel && map && isArray ( value ) ) {
97- return value . map ( ( value , index ) => {
67+ return value . map ( ( currentValue , index ) => {
9868 return this . _registerChildren ( (
9969 < Fieldset name = { index } key = { index } index = { index } >
10070 { children }
@@ -108,24 +78,17 @@ export default class Fieldset extends Element {
10878 return void 0 ;
10979 }
11080
111- if ( ! child . props . name && child . props . name !== 0 ) {
112- return cloneElement ( child , {
113- originalProps : child . props ,
114- form : this . props . form || this ,
115- fieldset : this ,
116- } ) ;
117- }
118-
11981 const currentValue = this . getValue ( child . props . name ) ;
82+ const currentPath = this . buildPath ( child . props . name ) ;
12083
12184 return cloneElement ( child , {
12285 originalProps : child . props ,
12386 value : typeof child . props . value !== 'undefined' ? child . props . value : currentValue ,
12487 currentValue,
12588 form : this . props . form || this ,
12689 fieldset : this ,
127- path : this . getPath ( child . props . name ) ,
128- onChange : ( value ) => this . setValue ( child . props . name , value ) ,
90+ path : currentPath ,
91+ onChange : ( value , component ) => this . setValue ( child . props . name , value , component ) ,
12992 } ) ;
13093 } , ( child ) => {
13194 const { replace } = this . getFormProps ( ) ;
@@ -149,7 +112,7 @@ export default class Fieldset extends Element {
149112 const children = this . _registerChildren ( this . props . children , true ) ;
150113
151114 return (
152- < fieldset onChange = { this . handleChange . bind ( this ) } name = { this . props . name } >
115+ < fieldset name = { this . props . name } >
153116 { children }
154117 </ fieldset >
155118 ) ;
0 commit comments