AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
| 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 | |
| 17 | import React from 'react'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 18 | import PropTypes from 'prop-types'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 19 | import ValidationButtons from './ValidationButtons.jsx'; |
| 20 | |
| 21 | class Form extends React.Component { |
| 22 | |
| 23 | static defaultProps = { |
| 24 | hasButtons : true, |
| 25 | onSubmit : null, |
| 26 | onReset : null, |
| 27 | labledButtons: true, |
| 28 | onValidChange : null, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 29 | isValid: true, |
| 30 | submitButtonText: null, |
| 31 | cancelButtonText: null |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static propTypes = { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 35 | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | constructor(props) { |
| 50 | super(props); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | render() { |
| 55 | // eslint-disable-next-line no-unused-vars |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 56 | let {isValid, onValidChange, onValidityChanged, onDataChanged, formReady, onValidateForm, isReadOnlyMode, hasButtons, onSubmit, labledButtons, submitButtonText, |
| 57 | cancelButtonText, children, ...formProps} = this.props; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 58 | 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 65 | {hasButtons && |
| 66 | <ValidationButtons |
| 67 | labledButtons={labledButtons} |
| 68 | submitButtonText={submitButtonText} |
| 69 | cancelButtonText={cancelButtonText} |
| 70 | ref={(buttons) => this.buttons = buttons} |
| 71 | isReadOnlyMode={isReadOnlyMode}/>} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 72 | </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 Lando | b3d4898 | 2017-06-11 14:22:02 +0300 | [diff] [blame] | 125 | export class TabsForm extends Form { |
| 126 | render() { |
| 127 | // eslint-disable-next-line no-unused-vars |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 128 | let {submitButtonText, cancelButtonText, isValid, formReady, onValidateForm, isReadOnlyMode, hasButtons, onSubmit, labledButtons, onValidChange, onValidityChanged, onDataChanged, children, |
| 129 | ...formProps} = this.props; |
Michael Lando | b3d4898 | 2017-06-11 14:22:02 +0300 | [diff] [blame] | 130 | return ( |
| 131 | <form {...formProps} ref={(form) => this.form = form} onSubmit={event => this.handleFormValidation(event)}> |
| 132 | <div className='validation-form-content'> |
| 133 | {children} |
| 134 | </div> |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 135 | {hasButtons && |
| 136 | <ValidationButtons |
| 137 | labledButtons={labledButtons} |
| 138 | submitButtonText={submitButtonText} |
| 139 | cancelButtonText={cancelButtonText} |
| 140 | ref={buttons => this.buttons = buttons} |
| 141 | isReadOnlyMode={isReadOnlyMode}/> |
| 142 | } |
Michael Lando | b3d4898 | 2017-06-11 14:22:02 +0300 | [diff] [blame] | 143 | </form> |
| 144 | ); |
| 145 | } |
| 146 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 147 | |
| 148 | export default Form; |