2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 import React from 'react';
24 import Tab from 'react-bootstrap/lib/Tab.js';
27 class ValidationTab extends React.Component {
30 children: React.PropTypes.node,
31 eventKey: React.PropTypes.any.isRequired,
32 onValidationStateChange: React.PropTypes.func //This property is assigned
34 // React.cloneElement. lookup
35 // ValidationTabs.jsx.
36 // therefore it cannot be
37 // stated as required!
42 this.validationComponents = [];
45 static childContextTypes = {
46 validationParent: React.PropTypes.any
49 static contextTypes = {
50 validationParent: React.PropTypes.any
54 return {validationParent: this};
63 let validationParent = this.context.validationParent;
64 if (validationParent) {
65 validationParent.register(this);
69 componentWillUnmount() {
70 let validationParent = this.context.validationParent;
71 if (validationParent) {
72 validationParent.unregister(this);
76 register(validationComponent) {
77 this.validationComponents.push(validationComponent);
80 unregister(validationComponent) {
81 this.childValidStateChanged(validationComponent, true);
82 this.validationComponents =
83 this.validationComponents.filter(
84 otherValidationComponent => validationComponent !==
85 otherValidationComponent);
88 notifyValidStateChangedToParent(isValid) {
90 let validationParent = this.context.validationParent;
91 if (validationParent) {
92 validationParent.childValidStateChanged(this, isValid);
96 childValidStateChanged(validationComponent, isValid) {
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();
107 this.setState({isValid: newValidState, notifyParent: true});
113 this.validationComponents.forEach(validationComponent => {
114 const isValidationComponentValid = validationComponent.validate().isValid;
115 isValid = isValidationComponentValid && isValid;
117 this.setState({isValid, notifyParent: false});
121 componentDidUpdate(prevProps, prevState) {
122 if (prevState.isValid !== this.state.isValid) {
123 if (this.state.notifyParent) {
124 this.notifyValidStateChangedToParent(this.state.isValid);
126 this.props.onValidationStateChange(this.props.eventKey,
132 return this.state.isValid;
136 let {children, ...tabProps} = this.props;
138 <Tab {...tabProps}>{children}</Tab>