2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
27 * ValidationForm should be used in order to have a form that handles it's
28 * internal validation state. All ValidationInputs inside the form are checked
29 * for validity and the styling and submit buttons are updated accordingly.
31 * The properties that ahould be given to the form:
32 * labledButtons - whether or not use icons only as the form default buttons or
33 * use buttons with labels onSubmit - function for click on the submit button
34 * onReset - function for click on the reset button
36 import React from 'react';
37 import ValidationButtons from './ValidationButtons.jsx';
39 class ValidationForm extends React.Component {
41 static childContextTypes = {
42 validationParent: React.PropTypes.any,
43 isReadOnlyMode: React.PropTypes.bool
46 static defaultProps = {
56 isValid: React.PropTypes.bool,
57 hasButtons: React.PropTypes.bool,
58 onSubmit: React.PropTypes.func,
59 onReset: React.PropTypes.func,
60 labledButtons: React.PropTypes.bool,
61 onValidChange: React.PropTypes.func
65 isValid: this.props.isValid
70 this.validationComponents = [];
74 var buttons = (this.props.hasButtons) ?
75 <ValidationButtons labledButtons={this.props.labledButtons}
77 isReadOnlyMode={this.props.isReadOnlyMode}/>
80 <form {...this.props} onSubmit={event => this.handleFormSubmit(event)}>
81 <div className='validation-form-content'>{this.props.children}</div>
87 handleFormSubmit(event) {
88 event.preventDefault();
89 let isFormValid = true;
90 this.validationComponents.forEach(validationComponent => {
91 const isInputValid = validationComponent.validate().isValid;
92 isFormValid = isInputValid && isFormValid;
94 if (isFormValid && this.props.onSubmit) {
95 this.props.onSubmit(event);
96 } else if (!isFormValid) {
97 this.setState({isValid: false});
101 componentDidUpdate(prevProps, prevState) {
102 // only handling this programatically if the validation of the form is done
103 // outside of the view (example with a form that is dependent on the state
105 if (prevProps.isValid !== this.props.isValid) {
106 if (this.props.hasButtons) {
107 this.refs.buttons.setState({isValid: this.state.isValid});
109 } else if (this.state.isValid !== prevState.isValid) {
110 if (this.props.hasButtons) {
111 this.refs.buttons.setState({isValid: this.state.isValid});
113 // callback in case form is part of bigger picture in view
114 if (this.props.onValidChange) {
115 this.props.onValidChange(this.state.isValid);
120 componentDidMount() {
121 if (this.props.hasButtons) {
122 this.refs.buttons.setState({isValid: this.state.isValid});
129 validationParent: this,
130 isReadOnlyMode: this.props.isReadOnlyMode
136 * Used by ValidationInput in order to let the (parent) form know
137 * the valid state. If there is a change in the state of the form,
138 * the buttons will be updated.
140 * @param validationComponent
143 childValidStateChanged(validationComponent, isValid) {
144 if (isValid !== this.state.isValid) {
145 let oldState = this.state.isValid;
146 let newState = isValid &&
147 this.validationComponents.filter(
148 otherValidationComponent => validationComponent !==
149 otherValidationComponent).every(otherValidationComponent => {
150 return otherValidationComponent.isValid();
153 if (oldState !== newState) {
154 this.setState({isValid: newState});
159 register(validationComponent) {
160 this.validationComponents.push(validationComponent);
163 unregister(validationComponent) {
164 this.childValidStateChanged(validationComponent, true);
165 this.validationComponents =
166 this.validationComponents.filter(
167 otherValidationComponent => validationComponent !==
168 otherValidationComponent);
173 export default ValidationForm;