diff --git a/packages/forms/src/UIForm/UIForm.component.js b/packages/forms/src/UIForm/UIForm.component.js index da91edad702..fff5defa5d2 100644 --- a/packages/forms/src/UIForm/UIForm.component.js +++ b/packages/forms/src/UIForm/UIForm.component.js @@ -1,25 +1,20 @@ import React, { PropTypes } from 'react'; -import { reduxForm } from 'redux-form'; import { merge } from 'talend-json-schema-form-core'; import Widget from './Widget'; -import { validate, validateAll } from './utils/validation'; -import { mutateValue } from './utils/properties'; const TRIGGER_AFTER = 'after'; -class UIForm extends React.Component { +export default class UIForm extends React.Component { constructor(props) { super(props); - const { jsonSchema, uiSchema, properties } = props.data; + const { jsonSchema, uiSchema } = props; this.state = { mergedSchema: merge(jsonSchema, uiSchema), - properties: { ...properties }, - validations: {}, }; console.log(this.state.mergedSchema) - this.consolidate = this.consolidate.bind(this); + this.onChange = this.onChange.bind(this); this.submit = this.submit.bind(this); } @@ -27,40 +22,16 @@ class UIForm extends React.Component { * Update the state with the new schema. * @param jsonSchema * @param uiSchema - * @param properties */ - componentWillReceiveProps({ jsonSchema, uiSchema, properties }) { - if (!jsonSchema || !uiSchema || !properties) { + componentWillReceiveProps({ jsonSchema, uiSchema }) { + if (!jsonSchema || !uiSchema) { return; } this.setState({ mergedSchema: merge(jsonSchema, uiSchema), - properties: { ...properties }, }); } - /** - * Consolidate form with the new value. - * - it updates the validation on the modified field. - * - it triggers onChange / onTrigger callbacks - * @param event The change event - * @param schema The schema of the changed field - * @param value The new field value - */ - consolidate(event, schema, value) { - this.setState( - (prevState) => { - const properties = mutateValue(prevState.properties, schema.key, value); - const validations = { - ...prevState.validations, - [schema.key]: validate(schema, value, properties, this.props.validation), - }; - return { properties, validations }; - }, - () => this.handleChangesCallbacks(schema, value) - ); - } - /** * Triggers the onTrigger and onChange if needed * - onChange : at each field change @@ -68,60 +39,31 @@ class UIForm extends React.Component { * @param schema The field schema * @param value The new value */ - handleChangesCallbacks(schema, value) { - const { onChange, onTrigger } = this.props; - - if (onChange) { - onChange({ - jsonSchema: this.props.data.jsonSchema, // original jsonSchema - uiSchema: this.props.data.uiSchema, // original uiSchema - properties: this.state.properties, // current properties values - }); - } + onChange(event, schema, value) { + const { onChange, onTrigger, properties } = this.props; + onChange(schema, value, properties); const { key, triggers } = schema; if (onTrigger && triggers && triggers.indexOf(TRIGGER_AFTER) !== -1) { onTrigger( - this.state.properties, // current properties values + this.props.properties, // current properties values key[key.length - 1], // field name value // field value ); } } - /** - * Triggers a validation and update state. - * @returns {boolean} true if the form is valid, false otherwise - */ - isValid() { - const validations = validateAll( - this.state.mergedSchema, - this.state.properties, - this.props.validation - ); - - const isValid = Object.keys(validations).every(key => validations[key].valid); - if (!isValid) { - this.setState({ validations }); - } - return isValid; - } - /** * Triggers submit callback if form is valid * @param event the submit event */ submit(event) { event.preventDefault(); - if (this.isValid()) { - this.props.onSubmit(event, this.state.properties); - } + this.props.onSubmit(event, this.state.mergedSchema, this.props.properties); } render() { - const { formName } = this.props; - const { properties, validations } = this.state; - + const { errors, formName, properties } = this.props; return (