1
1
import React , { PropTypes } from 'react' ;
2
- import { reduxForm } from 'redux-form' ;
3
2
import { merge } from 'talend-json-schema-form-core' ;
4
3
5
4
import Widget from './Widget' ;
6
- import { validate , validateAll } from './utils/validation' ;
7
- import { mutateValue } from './utils/properties' ;
8
5
9
6
const TRIGGER_AFTER = 'after' ;
10
7
11
- class UIForm extends React . Component {
8
+ export default class UIForm extends React . Component {
12
9
constructor ( props ) {
13
10
super ( props ) ;
14
- const { jsonSchema, uiSchema, properties } = props . data ;
11
+ const { jsonSchema, uiSchema } = props ;
15
12
this . state = {
16
13
mergedSchema : merge ( jsonSchema , uiSchema ) ,
17
- properties : { ...properties } ,
18
- validations : { } ,
19
14
} ;
20
15
console . log ( this . state . mergedSchema )
21
16
22
- this . consolidate = this . consolidate . bind ( this ) ;
17
+ this . onChange = this . onChange . bind ( this ) ;
23
18
this . submit = this . submit . bind ( this ) ;
24
19
}
25
20
26
21
/**
27
22
* Update the state with the new schema.
28
23
* @param jsonSchema
29
24
* @param uiSchema
30
- * @param properties
31
25
*/
32
- componentWillReceiveProps ( { jsonSchema, uiSchema, properties } ) {
33
- if ( ! jsonSchema || ! uiSchema || ! properties ) {
26
+ componentWillReceiveProps ( { jsonSchema, uiSchema } ) {
27
+ if ( ! jsonSchema || ! uiSchema ) {
34
28
return ;
35
29
}
36
30
this . setState ( {
37
31
mergedSchema : merge ( jsonSchema , uiSchema ) ,
38
- properties : { ...properties } ,
39
32
} ) ;
40
33
}
41
34
42
- /**
43
- * Consolidate form with the new value.
44
- * - it updates the validation on the modified field.
45
- * - it triggers onChange / onTrigger callbacks
46
- * @param event The change event
47
- * @param schema The schema of the changed field
48
- * @param value The new field value
49
- */
50
- consolidate ( event , schema , value ) {
51
- this . setState (
52
- ( prevState ) => {
53
- const properties = mutateValue ( prevState . properties , schema . key , value ) ;
54
- const validations = {
55
- ...prevState . validations ,
56
- [ schema . key ] : validate ( schema , value , properties , this . props . validation ) ,
57
- } ;
58
- return { properties, validations } ;
59
- } ,
60
- ( ) => this . handleChangesCallbacks ( schema , value )
61
- ) ;
62
- }
63
-
64
35
/**
65
36
* Triggers the onTrigger and onChange if needed
66
37
* - onChange : at each field change
67
38
* - onTrigger : when schema.trigger : ['after']
68
39
* @param schema The field schema
69
40
* @param value The new value
70
41
*/
71
- handleChangesCallbacks ( schema , value ) {
72
- const { onChange, onTrigger } = this . props ;
73
-
74
- if ( onChange ) {
75
- onChange ( {
76
- jsonSchema : this . props . data . jsonSchema , // original jsonSchema
77
- uiSchema : this . props . data . uiSchema , // original uiSchema
78
- properties : this . state . properties , // current properties values
79
- } ) ;
80
- }
42
+ onChange ( event , schema , value ) {
43
+ const { onChange, onTrigger, properties } = this . props ;
44
+ onChange ( schema , value , properties ) ;
81
45
82
46
const { key, triggers } = schema ;
83
47
if ( onTrigger && triggers && triggers . indexOf ( TRIGGER_AFTER ) !== - 1 ) {
84
48
onTrigger (
85
- this . state . properties , // current properties values
49
+ this . props . properties , // current properties values
86
50
key [ key . length - 1 ] , // field name
87
51
value // field value
88
52
) ;
89
53
}
90
54
}
91
55
92
- /**
93
- * Triggers a validation and update state.
94
- * @returns {boolean } true if the form is valid, false otherwise
95
- */
96
- isValid ( ) {
97
- const validations = validateAll (
98
- this . state . mergedSchema ,
99
- this . state . properties ,
100
- this . props . validation
101
- ) ;
102
-
103
- const isValid = Object . keys ( validations ) . every ( key => validations [ key ] . valid ) ;
104
- if ( ! isValid ) {
105
- this . setState ( { validations } ) ;
106
- }
107
- return isValid ;
108
- }
109
-
110
56
/**
111
57
* Triggers submit callback if form is valid
112
58
* @param event the submit event
113
59
*/
114
60
submit ( event ) {
115
61
event . preventDefault ( ) ;
116
- if ( this . isValid ( ) ) {
117
- this . props . onSubmit ( event , this . state . properties ) ;
118
- }
62
+ this . props . onSubmit ( event , this . state . mergedSchema , this . props . properties ) ;
119
63
}
120
64
121
65
render ( ) {
122
- const { formName } = this . props ;
123
- const { properties, validations } = this . state ;
124
-
66
+ const { errors, formName, properties } = this . props ;
125
67
return (
126
68
< form onSubmit = { this . submit } >
127
69
{
128
70
this . state . mergedSchema . map ( ( nextSchema , index ) => (
129
71
< Widget
130
72
key = { index }
131
73
formName = { formName }
132
- onChange = { this . consolidate }
74
+ onChange = { this . onChange }
133
75
schema = { nextSchema }
134
76
properties = { properties }
135
- validations = { validations }
77
+ errors = { errors }
136
78
/>
137
79
) )
138
80
}
@@ -144,19 +86,14 @@ class UIForm extends React.Component {
144
86
145
87
if ( process . env . NODE_ENV !== 'production' ) {
146
88
UIForm . propTypes = {
147
- /** Form schema configuration */
148
- data : PropTypes . shape ( {
149
- /** Json schema that specify the data model */
150
- jsonSchema : PropTypes . object ,
151
- /** UI schema that specify how to render the fields */
152
- uiSchema : PropTypes . array ,
153
- /** Form fields values. Note that it should contains @definitionName for triggers. */
154
- properties : PropTypes . object ,
155
- } ) ,
89
+ /** The forms errors { [fieldKey]: errorMessage } */
90
+ errors : PropTypes . object , // eslint-disable-line react/forbid-prop-types
156
91
/** The form name that will be used to create ids */
157
92
formName : PropTypes . string ,
158
- /** The change callback. It takes */
159
- onChange : PropTypes . func ,
93
+ /** Json schema that specify the data model */
94
+ jsonSchema : PropTypes . object , // eslint-disable-line react/forbid-prop-types
95
+ /** The change callback */
96
+ onChange : PropTypes . func . isRequired ,
160
97
/** Form submit callback */
161
98
onSubmit : PropTypes . func . isRequired ,
162
99
/**
@@ -165,16 +102,9 @@ if (process.env.NODE_ENV !== 'production') {
165
102
* This is executed on changes on fields with uiSchema > triggers : ['after']
166
103
*/
167
104
onTrigger : PropTypes . func ,
168
- /**
169
- * Custom validation function.
170
- * Prototype: function validation(properties, fieldName, value)
171
- * Return format : { valid: true|false, error: { message: 'my validation message' } }
172
- * This is triggered on fields that has their uiSchema > customValidation : true
173
- */
174
- validation : PropTypes . func ,
105
+ /** Form fields values. Note that it should contains @definitionName for triggers. */
106
+ properties : PropTypes . object , // eslint-disable-line react/forbid-prop-types
107
+ /** UI schema that specify how to render the fields */
108
+ uiSchema : PropTypes . array , // eslint-disable-line react/forbid-prop-types
175
109
} ;
176
110
}
177
-
178
- export default reduxForm ( {
179
- form : 'form' , // a unique name for this form
180
- } ) ( UIForm ) ;
0 commit comments