blob: 6df0bf9009577bf66bf3414752ba6063ef3c9878 [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 {
22
23 static defaultProps = {
24 hasButtons : true,
25 onSubmit : null,
26 onReset : null,
27 labledButtons: true,
28 onValidChange : null,
Avi Zivb8e2faf2017-07-18 19:45:38 +030029 isValid: true,
30 submitButtonText: null,
31 cancelButtonText: null
AviZi280f8012017-06-09 02:39:56 +030032 };
33
34 static propTypes = {
talig8e9c0652017-12-20 14:30:43 +020035 isValid : PropTypes.bool,
36 formReady : PropTypes.bool,
37 isReadOnlyMode : PropTypes.bool,
38 hasButtons : PropTypes.bool,
39 onSubmit : PropTypes.func,
40 onReset : PropTypes.func,
41 labledButtons: PropTypes.bool,
42 submitButtonText: PropTypes.string,
43 cancelButtonText: PropTypes.string,
44 onValidChange : PropTypes.func,
45 onValidityChanged: PropTypes.func,
46 onValidateForm: PropTypes.func
AviZi280f8012017-06-09 02:39:56 +030047 };
48
49 constructor(props) {
50 super(props);
51 }
52
53
54 render() {
55 // eslint-disable-next-line no-unused-vars
Avi Zivb8e2faf2017-07-18 19:45:38 +030056 let {isValid, onValidChange, onValidityChanged, onDataChanged, formReady, onValidateForm, isReadOnlyMode, hasButtons, onSubmit, labledButtons, submitButtonText,
57 cancelButtonText, children, ...formProps} = this.props;
AviZi280f8012017-06-09 02:39:56 +030058 return (
59 <form {...formProps} ref={(form) => this.form = form} onSubmit={event => this.handleFormValidation(event)}>
60 <div className='validation-form-content'>
61 <fieldset disabled={isReadOnlyMode}>
62 {children}
63 </fieldset>
64 </div>
Avi Zivb8e2faf2017-07-18 19:45:38 +030065 {hasButtons &&
66 <ValidationButtons
67 labledButtons={labledButtons}
68 submitButtonText={submitButtonText}
69 cancelButtonText={cancelButtonText}
70 ref={(buttons) => this.buttons = buttons}
71 isReadOnlyMode={isReadOnlyMode}/>}
AviZi280f8012017-06-09 02:39:56 +030072 </form>
73 );
74 }
75
76 handleFormValidation(event) {
77 event.preventDefault();
78 if (this.props.onValidateForm && !this.props.formReady){
79 return this.props.onValidateForm();
80 } else {
81 return this.handleFormSubmit(event);
82 }
83 }
84 handleFormSubmit(event) {
85 if (event) {
86 event.preventDefault();
87 }
88 if(this.props.onSubmit) {
89 return this.props.onSubmit(event);
90 }
91 }
92
93 componentDidMount() {
94 if (this.props.hasButtons) {
95 this.buttons.setState({isValid: this.props.isValid});
96 }
97 }
98
99
100
101 componentDidUpdate(prevProps) {
102 // only handling this programatically if the validation of the form is done outside of the view
103 // (example with a form that is dependent on the state of other forms)
104 if (prevProps.isValid !== this.props.isValid) {
105 if (this.props.hasButtons) {
106 this.buttons.setState({isValid: this.props.isValid});
107 }
108 // callback in case form is part of bigger picture in view
109 if (this.props.onValidChange) {
110 this.props.onValidChange(this.props.isValid);
111 }
112
113 // TODO - maybe this has to be part of componentWillUpdate
114 if(this.props.onValidityChanged) {
115 this.props.onValidityChanged(this.props.isValid);
116 }
117 }
118 if (this.props.formReady) { // if form validation succeeded -> continue with submit
119 this.handleFormSubmit();
120 }
121 }
122
123}
124
Michael Landob3d48982017-06-11 14:22:02 +0300125export class TabsForm extends Form {
126 render() {
127 // eslint-disable-next-line no-unused-vars
talig8e9c0652017-12-20 14:30:43 +0200128 let {submitButtonText, cancelButtonText, isValid, formReady, onValidateForm, isReadOnlyMode, hasButtons, onSubmit, labledButtons, onValidChange, onValidityChanged, onDataChanged, children,
129 ...formProps} = this.props;
Michael Landob3d48982017-06-11 14:22:02 +0300130 return (
131 <form {...formProps} ref={(form) => this.form = form} onSubmit={event => this.handleFormValidation(event)}>
132 <div className='validation-form-content'>
133 {children}
134 </div>
talig8e9c0652017-12-20 14:30:43 +0200135 {hasButtons &&
136 <ValidationButtons
137 labledButtons={labledButtons}
138 submitButtonText={submitButtonText}
139 cancelButtonText={cancelButtonText}
140 ref={buttons => this.buttons = buttons}
141 isReadOnlyMode={isReadOnlyMode}/>
142 }
Michael Landob3d48982017-06-11 14:22:02 +0300143 </form>
144 );
145 }
146}
AviZi280f8012017-06-09 02:39:56 +0300147
148export default Form;