blob: 0982c133e66e70ea357c06703388c996d54d0e83 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
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 */
16import React from 'react';
talig8e9c0652017-12-20 14:30:43 +020017import PropTypes from 'prop-types';
AviZi280f8012017-06-09 02:39:56 +030018import ReactDOM from 'react-dom';
Avi Ziv61070c92017-07-26 17:37:57 +030019import {default as SDCTabs} from 'sdc-ui/lib/react/Tabs.js';
AviZi280f8012017-06-09 02:39:56 +030020import Overlay from 'react-bootstrap/lib/Overlay.js';
21import Tooltip from 'react-bootstrap/lib/Tooltip.js';
22
23import i18n from 'nfvo-utils/i18n/i18n.js';
24
25export default
26class Tabs extends React.Component {
27
28 static propTypes = {
talig8e9c0652017-12-20 14:30:43 +020029 children: PropTypes.node
AviZi280f8012017-06-09 02:39:56 +030030 };
31
32 cloneTab(element) {
33 const {invalidTabs} = this.props;
34 return React.cloneElement(
35 element,
36 {
Avi Ziv61070c92017-07-26 17:37:57 +030037 key: element.props.tabId,
38 className: invalidTabs.indexOf(element.props.tabId) > -1 ? 'invalid-tab' : 'valid-tab'
AviZi280f8012017-06-09 02:39:56 +030039 }
40 );
41 }
42
43 showTabsError() {
44 const {invalidTabs} = this.props;
Avi Ziv61070c92017-07-26 17:37:57 +030045 const showError = ((invalidTabs.length === 1 && invalidTabs[0] !== this.props.activeTab) || (invalidTabs.length > 1));
AviZi280f8012017-06-09 02:39:56 +030046 return showError;
47 }
48
49 render() {
50 // eslint-disable-next-line no-unused-vars
51 let {invalidTabs, ...tabProps} = this.props;
52 return (
53 <div>
Avi Ziv61070c92017-07-26 17:37:57 +030054 <SDCTabs {...tabProps} ref='tabsList' id='tabsList' >
AviZi280f8012017-06-09 02:39:56 +030055 {this.props.children.map(element => this.cloneTab(element))}
Avi Ziv61070c92017-07-26 17:37:57 +030056 </SDCTabs>
AviZi280f8012017-06-09 02:39:56 +030057 <Overlay
58 animation={false}
59 show={this.showTabsError()}
60 placement='bottom'
AviZi280f8012017-06-09 02:39:56 +030061 target={() => {
Avi Ziv61070c92017-07-26 17:37:57 +030062 let target = ReactDOM.findDOMNode(this.refs.tabsList).querySelector('ul > li.invalid-tab:not(.sdc-tab-active):nth-of-type(n)');
AviZi280f8012017-06-09 02:39:56 +030063 return target && target.offsetParent ? target : undefined;
64 }
65 }
66 container={() => {
Avi Ziv61070c92017-07-26 17:37:57 +030067 let target = ReactDOM.findDOMNode(this.refs.tabsList).querySelector('ul > li.invalid-tab:not(.sdc-tab-active):nth-of-type(n)');
AviZi280f8012017-06-09 02:39:56 +030068 return target && target.offsetParent ? target.offsetParent : this;
69 }}>
70 <Tooltip
71 id='error-some-tabs-contain-errors'
72 className='validation-error-message'>
73 {i18n('One or more tabs are invalid')}
74 </Tooltip>
75 </Overlay>
76 </div>
77 );
78 }
79}