blob: 429985a902e67e22a574b7b39929213e3594b5a6 [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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020019import { 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
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020025export default class Tabs extends React.Component {
26 static propTypes = {
27 children: PropTypes.node
28 };
AviZi280f8012017-06-09 02:39:56 +030029
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020030 cloneTab(element) {
31 const { invalidTabs } = this.props;
32 return React.cloneElement(element, {
33 key: element.props.tabId,
34 className:
35 invalidTabs.indexOf(element.props.tabId) > -1
36 ? 'invalid-tab'
37 : 'valid-tab'
38 });
39 }
AviZi280f8012017-06-09 02:39:56 +030040
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020041 showTabsError() {
42 const { invalidTabs } = this.props;
43 const showError =
44 (invalidTabs.length === 1 &&
45 invalidTabs[0] !== this.props.activeTab) ||
46 invalidTabs.length > 1;
47 return showError;
48 }
AviZi280f8012017-06-09 02:39:56 +030049
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020050 render() {
51 // eslint-disable-next-line no-unused-vars
52 let { invalidTabs, ...tabProps } = this.props;
53 return (
54 <div>
55 <SDCTabs {...tabProps} ref="tabsList" id="tabsList">
56 {this.props.children.map(element => this.cloneTab(element))}
57 </SDCTabs>
58 <Overlay
59 animation={false}
60 show={this.showTabsError()}
61 placement="bottom"
62 target={() => {
63 let target = ReactDOM.findDOMNode(
64 this.refs.tabsList
65 ).querySelector(
66 'ul > li.invalid-tab:not(.sdc-tab-active):nth-of-type(n)'
67 );
68 return target && target.offsetParent
69 ? target
70 : undefined;
71 }}
72 container={() => {
73 let target = ReactDOM.findDOMNode(
74 this.refs.tabsList
75 ).querySelector(
76 'ul > li.invalid-tab:not(.sdc-tab-active):nth-of-type(n)'
77 );
78 return target && target.offsetParent
79 ? target.offsetParent
80 : this;
81 }}>
82 <Tooltip
83 id="error-some-tabs-contain-errors"
84 className="validation-error-message">
85 {i18n('One or more tabs are invalid')}
86 </Tooltip>
87 </Overlay>
88 </div>
89 );
90 }
AviZi280f8012017-06-09 02:39:56 +030091}