blob: 62fc29a55c8e47763cc14bd2a3f221b2b6b4c7cf [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}
73 ref={form => (this.form = form)}
74 onSubmit={event => this.handleFormValidation(event)}>
75 <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}
83 ref={buttons => (this.buttons = buttons)}
84 isReadOnlyMode={isReadOnlyMode}
85 />
86 )}
87 </form>
88 );
89 }
AviZi280f8012017-06-09 02:39:56 +030090
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020091 handleFormValidation(event) {
92 event.preventDefault();
93 if (this.props.onValidateForm && !this.props.formReady) {
94 return this.props.onValidateForm();
95 } else {
96 return this.handleFormSubmit(event);
97 }
98 }
99 handleFormSubmit(event) {
100 if (event) {
101 event.preventDefault();
102 }
103 if (this.props.onSubmit) {
104 return this.props.onSubmit(event);
105 }
106 }
AviZi280f8012017-06-09 02:39:56 +0300107
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200108 componentDidMount() {
109 if (this.props.hasButtons) {
110 this.buttons.setState({ isValid: this.props.isValid });
111 }
112 }
AviZi280f8012017-06-09 02:39:56 +0300113
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200114 componentDidUpdate(prevProps) {
115 // only handling this programatically if the validation of the form is done outside of the view
116 // (example with a form that is dependent on the state of other forms)
117 if (prevProps.isValid !== this.props.isValid) {
118 if (this.props.hasButtons) {
119 this.buttons.setState({ isValid: this.props.isValid });
120 }
121 // callback in case form is part of bigger picture in view
122 if (this.props.onValidChange) {
123 this.props.onValidChange(this.props.isValid);
124 }
AviZi280f8012017-06-09 02:39:56 +0300125
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200126 // TODO - maybe this has to be part of componentWillUpdate
127 if (this.props.onValidityChanged) {
128 this.props.onValidityChanged(this.props.isValid);
129 }
130 }
131 if (this.props.formReady) {
132 // if form validation succeeded -> continue with submit
133 this.handleFormSubmit();
134 }
135 }
AviZi280f8012017-06-09 02:39:56 +0300136}
137
Michael Landob3d48982017-06-11 14:22:02 +0300138export class TabsForm extends Form {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200139 render() {
140 /* eslint-disable no-unused-vars */
141 let {
142 submitButtonText,
143 cancelButtonText,
144 isValid,
145 formReady,
146 onValidateForm,
147 isReadOnlyMode,
148 hasButtons,
149 onSubmit,
150 labledButtons,
151 onValidChange,
152 onValidityChanged,
153 onDataChanged,
154 children,
155 ...formProps
156 } = this.props;
157 /* eslint-enable no-unused-vars */
158 return (
159 <form
160 {...formProps}
161 ref={form => (this.form = form)}
162 onSubmit={event => this.handleFormValidation(event)}>
163 <div className="validation-form-content">{children}</div>
164 {hasButtons && (
165 <ValidationButtons
166 labledButtons={labledButtons}
167 submitButtonText={submitButtonText}
168 cancelButtonText={cancelButtonText}
169 ref={buttons => (this.buttons = buttons)}
170 isReadOnlyMode={isReadOnlyMode}
171 />
172 )}
173 </form>
174 );
175 }
Michael Landob3d48982017-06-11 14:22:02 +0300176}
AviZi280f8012017-06-09 02:39:56 +0300177
178export default Form;