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 { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 22 | 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 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 32 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 33 | 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 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 47 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 48 | 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} |
svishnev | 3b0bea5 | 2018-05-08 16:15:21 +0300 | [diff] [blame^] | 73 | ref={this.setFormRef} |
| 74 | onSubmit={this.handleFormValidation}> |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 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} |
svishnev | 3b0bea5 | 2018-05-08 16:15:21 +0300 | [diff] [blame^] | 83 | ref={this.setButtonsRef} |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 84 | isReadOnlyMode={isReadOnlyMode} |
| 85 | /> |
| 86 | )} |
| 87 | </form> |
| 88 | ); |
| 89 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 90 | |
svishnev | 3b0bea5 | 2018-05-08 16:15:21 +0300 | [diff] [blame^] | 91 | handleFormValidation = event => { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 92 | event.preventDefault(); |
| 93 | if (this.props.onValidateForm && !this.props.formReady) { |
| 94 | return this.props.onValidateForm(); |
| 95 | } else { |
| 96 | return this.handleFormSubmit(event); |
| 97 | } |
svishnev | 3b0bea5 | 2018-05-08 16:15:21 +0300 | [diff] [blame^] | 98 | }; |
| 99 | |
| 100 | setButtonsRef = buttons => (this.buttons = buttons); |
| 101 | |
| 102 | setFormRef = form => (this.form = form); |
| 103 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 104 | handleFormSubmit(event) { |
| 105 | if (event) { |
| 106 | event.preventDefault(); |
| 107 | } |
| 108 | if (this.props.onSubmit) { |
| 109 | return this.props.onSubmit(event); |
| 110 | } |
| 111 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 112 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 113 | componentDidMount() { |
| 114 | if (this.props.hasButtons) { |
| 115 | this.buttons.setState({ isValid: this.props.isValid }); |
| 116 | } |
| 117 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 118 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 119 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 130 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 131 | // TODO - maybe this has to be part of componentWillUpdate |
| 132 | if (this.props.onValidityChanged) { |
| 133 | this.props.onValidityChanged(this.props.isValid); |
| 134 | } |
| 135 | } |
svishnev | 3b0bea5 | 2018-05-08 16:15:21 +0300 | [diff] [blame^] | 136 | if ( |
| 137 | this.props.formReady && |
| 138 | this.props.formReady !== prevProps.formReady |
| 139 | ) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 140 | // if form validation succeeded -> continue with submit |
| 141 | this.handleFormSubmit(); |
| 142 | } |
| 143 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 144 | } |
| 145 | |
Michael Lando | b3d4898 | 2017-06-11 14:22:02 +0300 | [diff] [blame] | 146 | export class TabsForm extends Form { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 147 | 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 Lando | b3d4898 | 2017-06-11 14:22:02 +0300 | [diff] [blame] | 184 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 185 | |
| 186 | export default Form; |