Including build.sh to run gulp-webpack
[aai/sparky-fe.git] / src / generic-components / input / validation / ValidationTab.jsx
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 import React from 'react';
24 import Tab from 'react-bootstrap/lib/Tab.js';
25
26 export default
27 class ValidationTab extends React.Component {
28                 
29                 static propTypes = {
30                                 children: React.PropTypes.node,
31                                 eventKey: React.PropTypes.any.isRequired,
32                                 onValidationStateChange: React.PropTypes.func //This property is assigned
33                                                                 // dynamically via
34                                                                 // React.cloneElement. lookup
35                                                                 // ValidationTabs.jsx.
36                                                                 // therefore it cannot be
37                                                                 // stated as required!
38                 };
39                 
40                 constructor(props) {
41                                 super(props);
42                                 this.validationComponents = [];
43                 }
44                 
45                 static childContextTypes = {
46                                 validationParent: React.PropTypes.any
47                 };
48                 
49                 static contextTypes = {
50                                 validationParent: React.PropTypes.any
51                 };
52                 
53                 getChildContext() {
54                                 return {validationParent: this};
55                 }
56                 
57                 state = {
58                                 isValid: true,
59                                 notifyParent: false
60                 };
61                 
62                 componentDidMount() {
63                                 let validationParent = this.context.validationParent;
64                                 if (validationParent) {
65                                                 validationParent.register(this);
66                                 }
67                 }
68                 
69                 componentWillUnmount() {
70                                 let validationParent = this.context.validationParent;
71                                 if (validationParent) {
72                                                 validationParent.unregister(this);
73                                 }
74                 }
75                 
76                 register(validationComponent) {
77                                 this.validationComponents.push(validationComponent);
78                 }
79                 
80                 unregister(validationComponent) {
81                                 this.childValidStateChanged(validationComponent, true);
82                                 this.validationComponents =
83                                                 this.validationComponents.filter(
84                                                                 otherValidationComponent => validationComponent !==
85                                                                 otherValidationComponent);
86                 }
87                 
88                 notifyValidStateChangedToParent(isValid) {
89                                 
90                                 let validationParent = this.context.validationParent;
91                                 if (validationParent) {
92                                                 validationParent.childValidStateChanged(this, isValid);
93                                 }
94                 }
95                 
96                 childValidStateChanged(validationComponent, isValid) {
97                                 
98                                 const currentValidState = this.state.isValid;
99                                 if (isValid !== currentValidState) {
100                                                 let filteredValidationComponents = this.validationComponents.filter(
101                                                                 otherValidationComponent => validationComponent !==
102                                                                 otherValidationComponent);
103                                                 let newValidState = isValid &&
104                                                                 filteredValidationComponents.every(otherValidationComponent => {
105                                                                                 return otherValidationComponent.isValid();
106                                                                 });
107                                                 this.setState({isValid: newValidState, notifyParent: true});
108                                 }
109                 }
110                 
111                 validate() {
112                                 let isValid = true;
113                                 this.validationComponents.forEach(validationComponent => {
114                                                 const isValidationComponentValid = validationComponent.validate().isValid;
115                                                 isValid = isValidationComponentValid && isValid;
116                                 });
117                                 this.setState({isValid, notifyParent: false});
118                                 return {isValid};
119                 }
120                 
121                 componentDidUpdate(prevProps, prevState) {
122                                 if (prevState.isValid !== this.state.isValid) {
123                                                 if (this.state.notifyParent) {
124                                                                 this.notifyValidStateChangedToParent(this.state.isValid);
125                                                 }
126                                                 this.props.onValidationStateChange(this.props.eventKey,
127                                                                 this.state.isValid);
128                                 }
129                 }
130                 
131                 isValid() {
132                                 return this.state.isValid;
133                 }
134                 
135                 render() {
136                                 let {children, ...tabProps} = this.props;
137                                 return (
138                                                 <Tab {...tabProps}>{children}</Tab>
139                                 );
140                 }
141 }