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'; |
| 17 | import ReactDOM from 'react-dom'; |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 18 | import {default as SDCTabs} from 'sdc-ui/lib/react/Tabs.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 19 | import Overlay from 'react-bootstrap/lib/Overlay.js'; |
| 20 | import Tooltip from 'react-bootstrap/lib/Tooltip.js'; |
| 21 | |
| 22 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 23 | |
| 24 | export default |
| 25 | class Tabs extends React.Component { |
| 26 | |
| 27 | static propTypes = { |
| 28 | children: React.PropTypes.node |
| 29 | }; |
| 30 | |
| 31 | cloneTab(element) { |
| 32 | const {invalidTabs} = this.props; |
| 33 | return React.cloneElement( |
| 34 | element, |
| 35 | { |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 36 | key: element.props.tabId, |
| 37 | className: invalidTabs.indexOf(element.props.tabId) > -1 ? 'invalid-tab' : 'valid-tab' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 38 | } |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | showTabsError() { |
| 43 | const {invalidTabs} = this.props; |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 44 | const showError = ((invalidTabs.length === 1 && invalidTabs[0] !== this.props.activeTab) || (invalidTabs.length > 1)); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 45 | return showError; |
| 46 | } |
| 47 | |
| 48 | render() { |
| 49 | // eslint-disable-next-line no-unused-vars |
| 50 | let {invalidTabs, ...tabProps} = this.props; |
| 51 | return ( |
| 52 | <div> |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 53 | <SDCTabs {...tabProps} ref='tabsList' id='tabsList' > |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 54 | {this.props.children.map(element => this.cloneTab(element))} |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 55 | </SDCTabs> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 56 | <Overlay |
| 57 | animation={false} |
| 58 | show={this.showTabsError()} |
| 59 | placement='bottom' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 60 | target={() => { |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 61 | let target = ReactDOM.findDOMNode(this.refs.tabsList).querySelector('ul > li.invalid-tab:not(.sdc-tab-active):nth-of-type(n)'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 62 | return target && target.offsetParent ? target : undefined; |
| 63 | } |
| 64 | } |
| 65 | container={() => { |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 66 | let target = ReactDOM.findDOMNode(this.refs.tabsList).querySelector('ul > li.invalid-tab:not(.sdc-tab-active):nth-of-type(n)'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 67 | return target && target.offsetParent ? target.offsetParent : this; |
| 68 | }}> |
| 69 | <Tooltip |
| 70 | id='error-some-tabs-contain-errors' |
| 71 | className='validation-error-message'> |
| 72 | {i18n('One or more tabs are invalid')} |
| 73 | </Tooltip> |
| 74 | </Overlay> |
| 75 | </div> |
| 76 | ); |
| 77 | } |
| 78 | } |