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