blob: 027920c423a3d63ed7cff061704312beba2b133f [file] [log] [blame]
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03001/*
2 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
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
7 *
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03008 * http://www.apache.org/licenses/LICENSE-2.0
AviZi280f8012017-06-09 02:39:56 +03009 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030012 * 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.
AviZi280f8012017-06-09 02:39:56 +030015 */
16
17import React from 'react';
talig8e9c0652017-12-20 14:30:43 +020018import PropTypes from 'prop-types';
AviZi280f8012017-06-09 02:39:56 +030019import ValidationButtons from './ValidationButtons.jsx';
20
21class Form extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020022 static defaultProps = {
23 hasButtons: true,
24 onSubmit: null,
25 onReset: null,
26 labledButtons: true,
27 onValidChange: null,
28 isValid: true,
29 submitButtonText: null,
30 cancelButtonText: null
31 };
AviZi280f8012017-06-09 02:39:56 +030032
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020033 static propTypes = {
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,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030045 onValidateForm: PropTypes.func,
46 btnClassName: PropTypes.string
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020047 };
AviZi280f8012017-06-09 02:39:56 +030048
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020049 constructor(props) {
50 super(props);
51 }
52 render() {
53 /* eslint-disable no-unused-vars */
54 let {
55 isValid,
56 onValidChange,
57 onValidityChanged,
58 onDataChanged,
59 formReady,
60 onValidateForm,
61 isReadOnlyMode,
62 hasButtons,
63 onSubmit,
64 labledButtons,
65 submitButtonText,
66 cancelButtonText,
67 children,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030068 btnClassName,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020069 ...formProps
70 } = this.props;
71 /* eslint-enable no-unused-vars */
72 return (
73 <form
74 {...formProps}
svishnev3b0bea52018-05-08 16:15:21 +030075 ref={this.setFormRef}
76 onSubmit={this.handleFormValidation}>
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020077 <div className="validation-form-content">
78 <fieldset disabled={isReadOnlyMode}>{children}</fieldset>
79 </div>
80 {hasButtons && (
81 <ValidationButtons
82 labledButtons={labledButtons}
83 submitButtonText={submitButtonText}
84 cancelButtonText={cancelButtonText}
svishnev3b0bea52018-05-08 16:15:21 +030085 ref={this.setButtonsRef}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020086 isReadOnlyMode={isReadOnlyMode}
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030087 className={btnClassName}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020088 />
89 )}
90 </form>
91 );
92 }
AviZi280f8012017-06-09 02:39:56 +030093
svishnev3b0bea52018-05-08 16:15:21 +030094 handleFormValidation = event => {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020095 event.preventDefault();
96 if (this.props.onValidateForm && !this.props.formReady) {
97 return this.props.onValidateForm();
98 } else {
99 return this.handleFormSubmit(event);
100 }
svishnev3b0bea52018-05-08 16:15:21 +0300101 };
102
103 setButtonsRef = buttons => (this.buttons = buttons);
104
105 setFormRef = form => (this.form = form);
106
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200107 handleFormSubmit(event) {
108 if (event) {
109 event.preventDefault();
110 }
111 if (this.props.onSubmit) {
112 return this.props.onSubmit(event);
113 }
114 }
AviZi280f8012017-06-09 02:39:56 +0300115
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200116 componentDidMount() {
117 if (this.props.hasButtons) {
118 this.buttons.setState({ isValid: this.props.isValid });
119 }
120 }
AviZi280f8012017-06-09 02:39:56 +0300121
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200122 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 });
128 }
129 // callback in case form is part of bigger picture in view
130 if (this.props.onValidChange) {
131 this.props.onValidChange(this.props.isValid);
132 }
AviZi280f8012017-06-09 02:39:56 +0300133
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200134 // TODO - maybe this has to be part of componentWillUpdate
135 if (this.props.onValidityChanged) {
136 this.props.onValidityChanged(this.props.isValid);
137 }
138 }
svishnev3b0bea52018-05-08 16:15:21 +0300139 if (
140 this.props.formReady &&
141 this.props.formReady !== prevProps.formReady
142 ) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200143 // if form validation succeeded -> continue with submit
144 this.handleFormSubmit();
145 }
146 }
AviZi280f8012017-06-09 02:39:56 +0300147}
148
Michael Landob3d48982017-06-11 14:22:02 +0300149export class TabsForm extends Form {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200150 render() {
151 /* eslint-disable no-unused-vars */
152 let {
153 submitButtonText,
154 cancelButtonText,
155 isValid,
156 formReady,
157 onValidateForm,
158 isReadOnlyMode,
159 hasButtons,
160 onSubmit,
161 labledButtons,
162 onValidChange,
163 onValidityChanged,
164 onDataChanged,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300165 btnClassName,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200166 children,
167 ...formProps
168 } = this.props;
169 /* eslint-enable no-unused-vars */
170 return (
171 <form
172 {...formProps}
173 ref={form => (this.form = form)}
174 onSubmit={event => this.handleFormValidation(event)}>
175 <div className="validation-form-content">{children}</div>
176 {hasButtons && (
177 <ValidationButtons
178 labledButtons={labledButtons}
179 submitButtonText={submitButtonText}
180 cancelButtonText={cancelButtonText}
181 ref={buttons => (this.buttons = buttons)}
182 isReadOnlyMode={isReadOnlyMode}
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300183 className={btnClassName}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200184 />
185 )}
186 </form>
187 );
188 }
Michael Landob3d48982017-06-11 14:22:02 +0300189}
AviZi280f8012017-06-09 02:39:56 +0300190
191export default Form;