11import React , { PureComponent , Component } from "react"
22import PropTypes from "prop-types"
33import { List , fromJS } from "immutable"
4+ import ImPropTypes from "react-immutable-proptypes"
45//import "less/json-schema-form"
56
67const noop = ( ) => { }
@@ -11,6 +12,7 @@ const JsonSchemaPropShape = {
1112 keyName : PropTypes . any ,
1213 fn : PropTypes . object . isRequired ,
1314 schema : PropTypes . object ,
15+ errors : ImPropTypes . list ,
1416 required : PropTypes . bool ,
1517 description : PropTypes . any
1618}
@@ -20,7 +22,8 @@ const JsonSchemaDefaultProps = {
2022 onChange : noop ,
2123 schema : { } ,
2224 keyName : "" ,
23- required : false
25+ required : false ,
26+ errors : List ( )
2427}
2528
2629export class JsonSchemaForm extends Component {
@@ -29,15 +32,15 @@ export class JsonSchemaForm extends Component {
2932 static defaultProps = JsonSchemaDefaultProps
3033
3134 render ( ) {
32- let { schema, value, onChange, getComponent, fn } = this . props
35+ let { schema, errors , value, onChange, getComponent, fn } = this . props
3336
3437 if ( schema . toJS )
3538 schema = schema . toJS ( )
3639
3740 let { type, format= "" } = schema
3841
3942 let Comp = ( format ? getComponent ( `JsonSchema_${ type } _${ format } ` ) : getComponent ( `JsonSchema_${ type } ` ) ) || getComponent ( "JsonSchema_string" )
40- return < Comp { ...this . props } fn = { fn } getComponent = { getComponent } value = { value } onChange = { onChange } schema = { schema } />
43+ return < Comp { ...this . props } errors = { errors } fn = { fn } getComponent = { getComponent } value = { value } onChange = { onChange } schema = { schema } />
4144 }
4245
4346}
@@ -51,14 +54,15 @@ export class JsonSchema_string extends Component {
5154 }
5255 onEnumChange = ( val ) => this . props . onChange ( val )
5356 render ( ) {
54- let { getComponent, value, schema, required, description } = this . props
57+ let { getComponent, value, schema, errors , required, description } = this . props
5558 let enumValue = schema [ "enum" ]
56- let errors = schema . errors || [ ]
59+
60+ errors = errors . toJS ? errors . toJS ( ) : [ ]
5761
5862 if ( enumValue ) {
5963 const Select = getComponent ( "Select" )
6064 return ( < Select className = { errors . length ? "invalid" : "" }
61- title = { errors . length ? errors : "" }
65+ title = { errors . length ? errors : "" }
6266 allowedValues = { enumValue }
6367 value = { value }
6468 allowEmptyValue = { ! required }
@@ -70,14 +74,14 @@ export class JsonSchema_string extends Component {
7074 if ( schema [ "type" ] === "file" ) {
7175 return ( < Input type = "file"
7276 className = { errors . length ? "invalid" : "" }
73- title = { errors . length ? errors : "" }
77+ title = { errors . length ? errors : "" }
7478 onChange = { this . onChange }
7579 disabled = { isDisabled } /> )
7680 }
7781 else {
7882 return ( < Input type = { schema . format === "password" ? "password" : "text" }
7983 className = { errors . length ? "invalid" : "" }
80- title = { errors . length ? errors : "" }
84+ title = { errors . length ? errors : "" }
8185 value = { value }
8286 placeholder = { description }
8387 onChange = { this . onChange }
@@ -131,9 +135,10 @@ export class JsonSchema_array extends PureComponent {
131135 }
132136
133137 render ( ) {
134- let { getComponent, required, schema, fn } = this . props
138+ let { getComponent, required, schema, errors, fn } = this . props
139+
140+ errors = errors . toJS ? errors . toJS ( ) : [ ]
135141
136- let errors = schema . errors || [ ]
137142 let itemSchema = fn . inferSchema ( schema . items )
138143
139144 const JsonSchemaForm = getComponent ( "JsonSchemaForm" )
@@ -145,7 +150,7 @@ export class JsonSchema_array extends PureComponent {
145150 if ( enumValue ) {
146151 const Select = getComponent ( "Select" )
147152 return ( < Select className = { errors . length ? "invalid" : "" }
148- title = { errors . length ? errors : "" }
153+ title = { errors . length ? errors : "" }
149154 multiple = { true }
150155 value = { value }
151156 allowedValues = { enumValue }
@@ -160,7 +165,7 @@ export class JsonSchema_array extends PureComponent {
160165 let schema = Object . assign ( { } , itemSchema )
161166 if ( errors . length ) {
162167 let err = errors . filter ( ( err ) => err . index === i )
163- if ( err . length ) schema . errors = [ err [ 0 ] . error + i ]
168+ if ( err . length ) errors = [ err [ 0 ] . error + i ]
164169 }
165170 return (
166171 < div key = { i } className = "json-schema-form-item" >
@@ -182,12 +187,13 @@ export class JsonSchema_boolean extends Component {
182187
183188 onEnumChange = ( val ) => this . props . onChange ( val )
184189 render ( ) {
185- let { getComponent, value, schema } = this . props
186- let errors = schema . errors || [ ]
190+ let { getComponent, value, errors, schema } = this . props
191+ errors = errors . toJS ? errors . toJS ( ) : [ ]
192+
187193 const Select = getComponent ( "Select" )
188194
189195 return ( < Select className = { errors . length ? "invalid" : "" }
190- title = { errors . length ? errors : "" }
196+ title = { errors . length ? errors : "" }
191197 value = { String ( value ) }
192198 allowedValues = { fromJS ( schema . enum || [ "true" , "false" ] ) }
193199 allowEmptyValue = { ! this . props . required }
0 commit comments