2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 import React from 'react';
18 import PropTypes from 'prop-types';
19 import ValidationButtons from './ValidationButtons.jsx';
21 class Form extends React.Component {
22 static defaultProps = {
29 submitButtonText: null,
30 cancelButtonText: null
34 isValid: PropTypes.bool,
35 formReady: PropTypes.bool,
36 isReadOnlyMode: PropTypes.bool,
37 hasButtons: PropTypes.bool,
38 onSubmit: PropTypes.func,
39 onReset: PropTypes.func,
40 labledButtons: PropTypes.bool,
41 submitButtonText: PropTypes.string,
42 cancelButtonText: PropTypes.string,
43 onValidChange: PropTypes.func,
44 onValidityChanged: PropTypes.func,
45 onValidateForm: PropTypes.func,
46 btnClassName: PropTypes.string
53 /* eslint-disable no-unused-vars */
71 /* eslint-enable no-unused-vars */
76 onSubmit={this.handleFormValidation}>
77 <div className="validation-form-content">
78 <fieldset disabled={isReadOnlyMode}>{children}</fieldset>
82 labledButtons={labledButtons}
83 submitButtonText={submitButtonText}
84 cancelButtonText={cancelButtonText}
85 ref={this.setButtonsRef}
86 isReadOnlyMode={isReadOnlyMode}
87 className={btnClassName}
94 handleFormValidation = event => {
95 event.preventDefault();
96 if (this.props.onValidateForm && !this.props.formReady) {
97 return this.props.onValidateForm();
99 return this.handleFormSubmit(event);
103 setButtonsRef = buttons => (this.buttons = buttons);
105 setFormRef = form => (this.form = form);
107 handleFormSubmit(event) {
109 event.preventDefault();
111 if (this.props.onSubmit) {
112 return this.props.onSubmit(event);
116 componentDidMount() {
117 if (this.props.hasButtons) {
118 this.buttons.setState({ isValid: this.props.isValid });
122 componentDidUpdate(prevProps) {
123 // only handling this programatically if the validation of the form is done outside of the view
124 // (example with a form that is dependent on the state of other forms)
125 if (prevProps.isValid !== this.props.isValid) {
126 if (this.props.hasButtons) {
127 this.buttons.setState({ isValid: this.props.isValid });
129 // callback in case form is part of bigger picture in view
130 if (this.props.onValidChange) {
131 this.props.onValidChange(this.props.isValid);
134 // TODO - maybe this has to be part of componentWillUpdate
135 if (this.props.onValidityChanged) {
136 this.props.onValidityChanged(this.props.isValid);
140 this.props.formReady &&
141 this.props.formReady !== prevProps.formReady
143 // if form validation succeeded -> continue with submit
144 this.handleFormSubmit();
149 export class TabsForm extends Form {
151 /* eslint-disable no-unused-vars */
169 /* eslint-enable no-unused-vars */
173 ref={form => (this.form = form)}
174 onSubmit={event => this.handleFormValidation(event)}>
175 <div className="validation-form-content">{children}</div>
178 labledButtons={labledButtons}
179 submitButtonText={submitButtonText}
180 cancelButtonText={cancelButtonText}
181 ref={buttons => (this.buttons = buttons)}
182 isReadOnlyMode={isReadOnlyMode}
183 className={btnClassName}