2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
17 import React from 'react';
18 import ValidationButtons from './ValidationButtons.jsx';
20 class Form extends React.Component {
22 static defaultProps = {
29 submitButtonText: null,
30 cancelButtonText: null
34 isValid : React.PropTypes.bool,
35 formReady : React.PropTypes.bool,
36 isReadOnlyMode : React.PropTypes.bool,
37 hasButtons : React.PropTypes.bool,
38 onSubmit : React.PropTypes.func,
39 onReset : React.PropTypes.func,
40 labledButtons: React.PropTypes.bool,
41 submitButtonText: React.PropTypes.string,
42 cancelButtonText: React.PropTypes.string,
43 onValidChange : React.PropTypes.func,
44 onValidityChanged: React.PropTypes.func,
45 onValidateForm: React.PropTypes.func
54 // eslint-disable-next-line no-unused-vars
55 let {isValid, onValidChange, onValidityChanged, onDataChanged, formReady, onValidateForm, isReadOnlyMode, hasButtons, onSubmit, labledButtons, submitButtonText,
56 cancelButtonText, children, ...formProps} = this.props;
58 <form {...formProps} ref={(form) => this.form = form} onSubmit={event => this.handleFormValidation(event)}>
59 <div className='validation-form-content'>
60 <fieldset disabled={isReadOnlyMode}>
66 labledButtons={labledButtons}
67 submitButtonText={submitButtonText}
68 cancelButtonText={cancelButtonText}
69 ref={(buttons) => this.buttons = buttons}
70 isReadOnlyMode={isReadOnlyMode}/>}
75 handleFormValidation(event) {
76 event.preventDefault();
77 if (this.props.onValidateForm && !this.props.formReady){
78 return this.props.onValidateForm();
80 return this.handleFormSubmit(event);
83 handleFormSubmit(event) {
85 event.preventDefault();
87 if(this.props.onSubmit) {
88 return this.props.onSubmit(event);
93 if (this.props.hasButtons) {
94 this.buttons.setState({isValid: this.props.isValid});
100 componentDidUpdate(prevProps) {
101 // only handling this programatically if the validation of the form is done outside of the view
102 // (example with a form that is dependent on the state of other forms)
103 if (prevProps.isValid !== this.props.isValid) {
104 if (this.props.hasButtons) {
105 this.buttons.setState({isValid: this.props.isValid});
107 // callback in case form is part of bigger picture in view
108 if (this.props.onValidChange) {
109 this.props.onValidChange(this.props.isValid);
112 // TODO - maybe this has to be part of componentWillUpdate
113 if(this.props.onValidityChanged) {
114 this.props.onValidityChanged(this.props.isValid);
117 if (this.props.formReady) { // if form validation succeeded -> continue with submit
118 this.handleFormSubmit();
124 export class TabsForm extends Form {
126 // eslint-disable-next-line no-unused-vars
127 let {isValid, formReady, onValidateForm, isReadOnlyMode, hasButtons, onSubmit, labledButtons, onValidChange, onValidityChanged, onDataChanged, children, ...formProps} = this.props;
129 <form {...formProps} ref={(form) => this.form = form} onSubmit={event => this.handleFormValidation(event)}>
130 <div className='validation-form-content'>
133 {hasButtons && <ValidationButtons labledButtons={labledButtons} ref={(buttons) => this.buttons = buttons} isReadOnlyMode={isReadOnlyMode}/>}