2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 import React from 'react';
27 import Tab from 'react-bootstrap/lib/Tab.js';
30 class ValidationTab extends React.Component {
33 children: React.PropTypes.node,
34 eventKey: React.PropTypes.any.isRequired,
35 onValidationStateChange: React.PropTypes.func //This property is assigned
37 // React.cloneElement. lookup
38 // ValidationTabs.jsx.
39 // therefore it cannot be
40 // stated as required!
45 this.validationComponents = [];
48 static childContextTypes = {
49 validationParent: React.PropTypes.any
52 static contextTypes = {
53 validationParent: React.PropTypes.any
57 return {validationParent: this};
66 let validationParent = this.context.validationParent;
67 if (validationParent) {
68 validationParent.register(this);
72 componentWillUnmount() {
73 let validationParent = this.context.validationParent;
74 if (validationParent) {
75 validationParent.unregister(this);
79 register(validationComponent) {
80 this.validationComponents.push(validationComponent);
83 unregister(validationComponent) {
84 this.childValidStateChanged(validationComponent, true);
85 this.validationComponents =
86 this.validationComponents.filter(
87 otherValidationComponent => validationComponent !==
88 otherValidationComponent);
91 notifyValidStateChangedToParent(isValid) {
93 let validationParent = this.context.validationParent;
94 if (validationParent) {
95 validationParent.childValidStateChanged(this, isValid);
99 childValidStateChanged(validationComponent, isValid) {
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();
110 this.setState({isValid: newValidState, notifyParent: true});
116 this.validationComponents.forEach(validationComponent => {
117 const isValidationComponentValid = validationComponent.validate().isValid;
118 isValid = isValidationComponentValid && isValid;
120 this.setState({isValid, notifyParent: false});
124 componentDidUpdate(prevProps, prevState) {
125 if (prevState.isValid !== this.state.isValid) {
126 if (this.state.notifyParent) {
127 this.notifyValidStateChangedToParent(this.state.isValid);
129 this.props.onValidationStateChange(this.props.eventKey,
135 return this.state.isValid;
139 let {children, ...tabProps} = this.props;
141 <Tab {...tabProps}>{children}</Tab>