blob: b5db67c0274e4de33ad892d404389d93782d16ba [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3 *
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 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
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,
45 onValidateForm: PropTypes.func
46 };
AviZi280f8012017-06-09 02:39:56 +030047
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020048 constructor(props) {
49 super(props);
50 }
51 render() {
52 /* eslint-disable no-unused-vars */
53 let {
54 isValid,
55 onValidChange,
56 onValidityChanged,
57 onDataChanged,
58 formReady,
59 onValidateForm,
60 isReadOnlyMode,
61 hasButtons,
62 onSubmit,
63 labledButtons,
64 submitButtonText,
65 cancelButtonText,
66 children,
67 ...formProps
68 } = this.props;
69 /* eslint-enable no-unused-vars */
70 return (
71 <form
72 {...formProps}
svishnev3b0bea52018-05-08 16:15:21 +030073 ref={this.setFormRef}
74 onSubmit={this.handleFormValidation}>
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020075 <div className="validation-form-content">
76 <fieldset disabled={isReadOnlyMode}>{children}</fieldset>
77 </div>
78 {hasButtons && (
79 <ValidationButtons
80 labledButtons={labledButtons}
81 submitButtonText={submitButtonText}
82 cancelButtonText={cancelButtonText}
svishnev3b0bea52018-05-08 16:15:21 +030083 ref={this.setButtonsRef}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020084 isReadOnlyMode={isReadOnlyMode}
85 />
86 )}
87 </form>
88 );
89 }
AviZi280f8012017-06-09 02:39:56 +030090
svishnev3b0bea52018-05-08 16:15:21 +030091 handleFormValidation = event => {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020092 event.preventDefault();
93 if (this.props.onValidateForm && !this.props.formReady) {
94 return this.props.onValidateForm();
95 } else {
96 return this.handleFormSubmit(event);
97 }
svishnev3b0bea52018-05-08 16:15:21 +030098 };
99
100 setButtonsRef = buttons => (this.buttons = buttons);
101
102 setFormRef = form => (this.form = form);
103
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200104 handleFormSubmit(event) {
105 if (event) {
106 event.preventDefault();
107 }
108 if (this.props.onSubmit) {
109 return this.props.onSubmit(event);
110 }
111 }
AviZi280f8012017-06-09 02:39:56 +0300112
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200113 componentDidMount() {
114 if (this.props.hasButtons) {
115 this.buttons.setState({ isValid: this.props.isValid });
116 }
117 }
AviZi280f8012017-06-09 02:39:56 +0300118
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200119 componentDidUpdate(prevProps) {
120 // only handling this programatically if the validation of the form is done outside of the view
121 // (example with a form that is dependent on the state of other forms)
122 if (prevProps.isValid !== this.props.isValid) {
123 if (this.props.hasButtons) {
124 this.buttons.setState({ isValid: this.props.isValid });
125 }
126 // callback in case form is part of bigger picture in view
127 if (this.props.onValidChange) {
128 this.props.onValidChange(this.props.isValid);
129 }
AviZi280f8012017-06-09 02:39:56 +0300130
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200131 // TODO - maybe this has to be part of componentWillUpdate
132 if (this.props.onValidityChanged) {
133 this.props.onValidityChanged(this.props.isValid);
134 }
135 }
svishnev3b0bea52018-05-08 16:15:21 +0300136 if (
137 this.props.formReady &&
138 this.props.formReady !== prevProps.formReady
139 ) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200140 // if form validation succeeded -> continue with submit
141 this.handleFormSubmit();
142 }
143 }
AviZi280f8012017-06-09 02:39:56 +0300144}
145
Michael Landob3d48982017-06-11 14:22:02 +0300146export class TabsForm extends Form {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200147 render() {
148 /* eslint-disable no-unused-vars */
149 let {
150 submitButtonText,
151 cancelButtonText,
152 isValid,
153 formReady,
154 onValidateForm,
155 isReadOnlyMode,
156 hasButtons,
157 onSubmit,
158 labledButtons,
159 onValidChange,
160 onValidityChanged,
161 onDataChanged,
162 children,
163 ...formProps
164 } = this.props;
165 /* eslint-enable no-unused-vars */
166 return (
167 <form
168 {...formProps}
169 ref={form => (this.form = form)}
170 onSubmit={event => this.handleFormValidation(event)}>
171 <div className="validation-form-content">{children}</div>
172 {hasButtons && (
173 <ValidationButtons
174 labledButtons={labledButtons}
175 submitButtonText={submitButtonText}
176 cancelButtonText={cancelButtonText}
177 ref={buttons => (this.buttons = buttons)}
178 isReadOnlyMode={isReadOnlyMode}
179 />
180 )}
181 </form>
182 );
183 }
Michael Landob3d48982017-06-11 14:22:02 +0300184}
AviZi280f8012017-06-09 02:39:56 +0300185
186export default Form;