|
1 | 1 | import React, { PropTypes } from 'react';
|
2 | 2 | import { reduxForm } from 'redux-form';
|
3 |
| -import { merge, validate } from 'talend-json-schema-form-core'; |
| 3 | +import { merge } from 'talend-json-schema-form-core'; |
4 | 4 |
|
5 | 5 | import Widget from './Widget';
|
6 |
| -import { validateAll } from './utils/validation'; |
| 6 | +import { validate, validateAll } from './utils/validation'; |
7 | 7 | import { mutateValue } from './utils/properties';
|
8 | 8 |
|
9 | 9 | const TRIGGER_AFTER = 'after';
|
@@ -52,50 +52,62 @@ class UIForm extends React.Component {
|
52 | 52 | */
|
53 | 53 | consolidate(event, schema, value) {
|
54 | 54 | this.setState(
|
55 |
| - prevState => ({ |
56 |
| - properties: mutateValue(prevState.properties, schema.key, value), |
57 |
| - validations: { |
| 55 | + (prevState) => { |
| 56 | + const properties = mutateValue(prevState.properties, schema.key, value); |
| 57 | + const validations = { |
58 | 58 | ...prevState.validations,
|
59 |
| - [schema.key]: validate(schema, value), |
60 |
| - }, |
61 |
| - }), |
62 |
| - () => { |
63 |
| - const { onChange, onTrigger } = this.props; |
64 |
| - |
65 |
| - if (onChange) { |
66 |
| - onChange({ |
67 |
| - jsonSchema: this.props.data.jsonSchema, // original jsonSchema |
68 |
| - uiSchema: this.props.data.uiSchema, // original uiSchema |
69 |
| - properties: this.state.properties, // current properties values |
70 |
| - }); |
71 |
| - } |
72 |
| - |
73 |
| - const { key, triggers } = schema; |
74 |
| - if (onTrigger && triggers && triggers.indexOf(TRIGGER_AFTER) !== -1) { |
75 |
| - onTrigger( |
76 |
| - this.state.properties, // current properties values |
77 |
| - key[key.length - 1], // field name |
78 |
| - value // field value |
79 |
| - ); |
80 |
| - } |
81 |
| - } |
| 59 | + [schema.key]: validate(schema, value, properties, this.props.validation), |
| 60 | + }; |
| 61 | + return { properties, validations }; |
| 62 | + }, |
| 63 | + () => this.handleChangesCallbacks(schema, value) |
82 | 64 | );
|
83 | 65 | }
|
84 | 66 |
|
| 67 | + /** |
| 68 | + * Triggers the onTrigger and onChange if needed |
| 69 | + * - onChange : at each field change |
| 70 | + * - onTrigger : when schema.trigger : ['after'] |
| 71 | + * @param schema The field schema |
| 72 | + * @param value The new value |
| 73 | + */ |
| 74 | + handleChangesCallbacks(schema, value) { |
| 75 | + const { onChange, onTrigger } = this.props; |
| 76 | + |
| 77 | + if (onChange) { |
| 78 | + onChange({ |
| 79 | + jsonSchema: this.props.data.jsonSchema, // original jsonSchema |
| 80 | + uiSchema: this.props.data.uiSchema, // original uiSchema |
| 81 | + properties: this.state.properties, // current properties values |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + const { key, triggers } = schema; |
| 86 | + if (onTrigger && triggers && triggers.indexOf(TRIGGER_AFTER) !== -1) { |
| 87 | + onTrigger( |
| 88 | + this.state.properties, // current properties values |
| 89 | + key[key.length - 1], // field name |
| 90 | + value // field value |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
85 | 95 | /**
|
86 | 96 | * Triggers a validation and update state.
|
87 | 97 | * @returns {boolean} true if the form is valid, false otherwise
|
88 | 98 | */
|
89 | 99 | isValid() {
|
90 |
| - const validations = validateAll(this.state.mergedSchema, this.state.properties); |
91 |
| - const keys = Object.keys(validations); |
92 |
| - for (const key of keys) { |
93 |
| - if (!validations[key].valid) { |
94 |
| - this.setState({ validations }); |
95 |
| - return false; |
96 |
| - } |
| 100 | + const validations = validateAll( |
| 101 | + this.state.mergedSchema, |
| 102 | + this.state.properties, |
| 103 | + this.props.validation |
| 104 | + ); |
| 105 | + |
| 106 | + const isValid = Object.keys(validations).every(key => validations[key].valid); |
| 107 | + if (!isValid) { |
| 108 | + this.setState({ validations }); |
97 | 109 | }
|
98 |
| - return true; |
| 110 | + return isValid; |
99 | 111 | }
|
100 | 112 |
|
101 | 113 | /**
|
@@ -152,9 +164,17 @@ if (process.env.NODE_ENV !== 'production') {
|
152 | 164 | onSubmit: PropTypes.func.isRequired,
|
153 | 165 | /**
|
154 | 166 | * Tigger > after callback.
|
| 167 | + * Prototype: function onTrigger(properties, fieldName, value) |
155 | 168 | * This is executed on changes on fields with uiSchema > triggers : ['after']
|
156 | 169 | */
|
157 | 170 | onTrigger: PropTypes.func,
|
| 171 | + /** |
| 172 | + * Custom validation function. |
| 173 | + * Prototype: function validation(properties, fieldName, value) |
| 174 | + * Return format : { valid: true|false, error: { message: 'my validation message' } } |
| 175 | + * This is triggered on fields that has their uiSchema > customValidation : true |
| 176 | + */ |
| 177 | + validation: PropTypes.func, |
158 | 178 | };
|
159 | 179 | }
|
160 | 180 |
|
|
0 commit comments