11import React , { Component , PropTypes } from "react" ;
22
3- import { defaultTypeValue } from "../../utils" ;
3+ import { getDefaultFormState } from "../../utils" ;
44import SchemaField from "./SchemaField" ;
55
66
@@ -12,50 +12,45 @@ class ArrayField extends Component {
1212 constructor ( props ) {
1313 super ( props ) ;
1414 const formData = Array . isArray ( props . formData ) ? props . formData : null ;
15- this . state = { items : formData || props . schema . default || [ ] } ;
15+ this . state = { items : formData || getDefaultFormState ( props . schema ) || [ ] } ;
1616 }
1717
1818 get itemTitle ( ) {
1919 const { schema} = this . props ;
2020 return schema . items . title || schema . items . description || "Item" ;
2121 }
2222
23- defaultItem ( itemsSchema ) {
24- if ( itemsSchema . default ) {
25- return itemsSchema . default ;
26- }
27- return defaultTypeValue ( itemsSchema . type ) ;
28- }
29-
3023 isItemRequired ( itemsSchema ) {
3124 return itemsSchema . type === "string" && itemsSchema . minLength > 0 ;
3225 }
3326
34- asyncSetState ( state ) {
27+ asyncSetState ( state , options ) {
3528 // ensure state is propagated to parent component when it's actually set
36- this . setState ( state , _ => this . props . onChange ( this . state . items ) ) ;
29+ this . setState ( state , _ => this . props . onChange ( this . state . items , options ) ) ;
3730 }
3831
3932 onAddClick ( event ) {
4033 event . preventDefault ( ) ;
41- this . setState ( {
42- items : this . state . items . concat ( this . defaultItem ( this . props . schema . items ) )
43- } ) ;
34+ const { items} = this . state ;
35+ const { schema} = this . props ;
36+ this . asyncSetState ( {
37+ items : items . concat ( getDefaultFormState ( schema . items ) )
38+ } , { validate : false } ) ;
4439 }
4540
4641 onDropClick ( index , event ) {
4742 event . preventDefault ( ) ;
48- this . setState ( {
43+ this . asyncSetState ( {
4944 items : this . state . items . filter ( ( _ , i ) => i !== index )
50- } ) ;
45+ } , { validate : false } ) ;
5146 }
5247
5348 onChange ( index , value ) {
5449 this . asyncSetState ( {
5550 items : this . state . items . map ( ( item , i ) => {
5651 return index === i ? value : item ;
5752 } )
58- } ) ;
53+ } , { validate : false } ) ;
5954 }
6055
6156 render ( ) {
@@ -68,17 +63,19 @@ class ArrayField extends Component {
6863 { schema . description ? < div > { schema . description } </ div > : null }
6964 < div className = "array-item-list" > {
7065 items . map ( ( item , index ) => {
71- return < div key = { index } >
72- < SchemaField
73- schema = { schema . items }
74- uiSchema = { uiSchema . items }
75- formData = { items [ index ] }
76- required = { this . isItemRequired ( schema . items ) }
77- onChange = { this . onChange . bind ( this , index ) } />
78- < p className = "array-item-remove" >
79- < button type = "button"
80- onClick = { this . onDropClick . bind ( this , index ) } > -</ button > </ p >
81- </ div > ;
66+ return (
67+ < div key = { index } >
68+ < SchemaField
69+ schema = { schema . items }
70+ uiSchema = { uiSchema . items }
71+ formData = { items [ index ] }
72+ required = { this . isItemRequired ( schema . items ) }
73+ onChange = { this . onChange . bind ( this , index ) } />
74+ < p className = "array-item-remove" >
75+ < button type = "button"
76+ onClick = { this . onDropClick . bind ( this , index ) } > -</ button > </ p >
77+ </ div >
78+ ) ;
8279 } )
8380 } </ div >
8481 < p className = "array-item-add" >
0 commit comments