|
| 1 | +import React, { PropTypes } from 'react'; |
| 2 | +import { reduxForm } from 'redux-form'; |
| 3 | +import { merge } from 'talend-json-schema-form-core'; |
| 4 | + |
| 5 | +import Widget from './Widget'; |
| 6 | +import { validate, validateAll } from './utils/validation'; |
| 7 | +import { mutateValue } from './utils/properties'; |
| 8 | + |
| 9 | +const TRIGGER_AFTER = 'after'; |
| 10 | + |
| 11 | +class UIForm extends React.Component { |
| 12 | + constructor(props) { |
| 13 | + super(props); |
| 14 | + const { jsonSchema, uiSchema, properties } = props.data; |
| 15 | + this.state = { |
| 16 | + mergedSchema: merge(jsonSchema, uiSchema), |
| 17 | + properties: { ...properties }, |
| 18 | + validations: {}, |
| 19 | + }; |
| 20 | + console.log(this.state.mergedSchema) |
| 21 | + |
| 22 | + this.consolidate = this.consolidate.bind(this); |
| 23 | + this.submit = this.submit.bind(this); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Update the state with the new schema. |
| 28 | + * @param jsonSchema |
| 29 | + * @param uiSchema |
| 30 | + * @param properties |
| 31 | + */ |
| 32 | + componentWillReceiveProps({ jsonSchema, uiSchema, properties }) { |
| 33 | + if (!jsonSchema || !uiSchema || !properties) { |
| 34 | + return; |
| 35 | + } |
| 36 | + this.setState({ |
| 37 | + mergedSchema: merge(jsonSchema, uiSchema), |
| 38 | + properties: { ...properties }, |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 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 | + /** |
| 65 | + * Triggers the onTrigger and onChange if needed |
| 66 | + * - onChange : at each field change |
| 67 | + * - onTrigger : when schema.trigger : ['after'] |
| 68 | + * @param schema The field schema |
| 69 | + * @param value The new value |
| 70 | + */ |
| 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 | + } |
| 81 | + |
| 82 | + const { key, triggers } = schema; |
| 83 | + if (onTrigger && triggers && triggers.indexOf(TRIGGER_AFTER) !== -1) { |
| 84 | + onTrigger( |
| 85 | + this.state.properties, // current properties values |
| 86 | + key[key.length - 1], // field name |
| 87 | + value // field value |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 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 | + /** |
| 111 | + * Triggers submit callback if form is valid |
| 112 | + * @param event the submit event |
| 113 | + */ |
| 114 | + submit(event) { |
| 115 | + event.preventDefault(); |
| 116 | + if (this.isValid()) { |
| 117 | + this.props.onSubmit(event, this.state.properties); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + render() { |
| 122 | + const { formName } = this.props; |
| 123 | + const { properties, validations } = this.state; |
| 124 | + |
| 125 | + return ( |
| 126 | + <form onSubmit={this.submit}> |
| 127 | + { |
| 128 | + this.state.mergedSchema.map((nextSchema, index) => ( |
| 129 | + <Widget |
| 130 | + key={index} |
| 131 | + formName={formName} |
| 132 | + onChange={this.consolidate} |
| 133 | + schema={nextSchema} |
| 134 | + properties={properties} |
| 135 | + validations={validations} |
| 136 | + /> |
| 137 | + )) |
| 138 | + } |
| 139 | + <button type="submit" className="btn btn-primary">Submit</button> |
| 140 | + </form> |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +if (process.env.NODE_ENV !== 'production') { |
| 146 | + 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 | + }), |
| 156 | + /** The form name that will be used to create ids */ |
| 157 | + formName: PropTypes.string, |
| 158 | + /** The change callback. It takes */ |
| 159 | + onChange: PropTypes.func, |
| 160 | + /** Form submit callback */ |
| 161 | + onSubmit: PropTypes.func.isRequired, |
| 162 | + /** |
| 163 | + * Tigger > after callback. |
| 164 | + * Prototype: function onTrigger(properties, fieldName, value) |
| 165 | + * This is executed on changes on fields with uiSchema > triggers : ['after'] |
| 166 | + */ |
| 167 | + 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, |
| 175 | + }; |
| 176 | +} |
| 177 | + |
| 178 | +export default reduxForm({ |
| 179 | + form: 'form', // a unique name for this form |
| 180 | +})(UIForm); |
0 commit comments