AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
| 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 | */ |
| 16 | import React from 'react'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 17 | import PropTypes from 'prop-types'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 18 | import ReactDOM from 'react-dom'; |
Arielk | a51eac0 | 2019-07-07 12:56:11 +0300 | [diff] [blame^] | 19 | import { Tabs as SDCTabs } from 'onap-ui-react'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 20 | import Overlay from 'react-bootstrap/lib/Overlay.js'; |
| 21 | import Tooltip from 'react-bootstrap/lib/Tooltip.js'; |
| 22 | |
| 23 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 24 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 25 | export default class Tabs extends React.Component { |
| 26 | static propTypes = { |
| 27 | children: PropTypes.node |
| 28 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 29 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 30 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 40 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 41 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 49 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 50 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 91 | } |