diff --git a/src/components/Tabs/Bar.js b/src/components/Tabs/Bar.js index b06f5303e..336cd3d5d 100644 --- a/src/components/Tabs/Bar.js +++ b/src/components/Tabs/Bar.js @@ -2,7 +2,11 @@ import React from 'react'; import PropTypes from 'prop-types'; const Bar = ({ children }) => ( -
{children}
+
+ {React.Children.map(children, (child, index) => + React.cloneElement(child, { ...child.props, index }) + )} +
); Bar.propTypes = { diff --git a/src/components/Tabs/BarItem.js b/src/components/Tabs/BarItem.js index 577d52829..82f72712c 100644 --- a/src/components/Tabs/BarItem.js +++ b/src/components/Tabs/BarItem.js @@ -1,15 +1,30 @@ import React from 'react'; import PropTypes from 'prop-types'; -const BarItem = ({ children, count, disabled }) => ( -
- {children} - {count && {count}} -
-); +import useTabs from './useTabs'; + +const BarItem = ({ index, children, id, count, disabled }) => { + const [currentTab, setCurrentTab] = useTabs(); + + const isSelected = + id === currentTab || (currentTab === undefined && index === 0); + + return ( +
!disabled && setCurrentTab(id)} + style={{ textDecoration: disabled ? 'line-through' : 'normal' }} + > + {isSelected && +} + {children} + {count && {count}} +
+ ); +}; BarItem.propTypes = { + index: PropTypes.number.isRequired, children: PropTypes.node.isRequired, + id: PropTypes.string.isRequired, count: PropTypes.number, disabled: PropTypes.bool, }; diff --git a/src/components/Tabs/Page.js b/src/components/Tabs/Page.js index f6d03daae..5cd67fcbd 100644 --- a/src/components/Tabs/Page.js +++ b/src/components/Tabs/Page.js @@ -1,10 +1,23 @@ import React from 'react'; import PropTypes from 'prop-types'; -const Page = ({ children }) =>
{children}
; +import useTabs from './useTabs'; + +const Page = ({ index, children, id }) => { + const [currentTab] = useTabs(); + + const isSelected = + id === currentTab || (currentTab === undefined && index === 0); + + if (!isSelected) return null; + + return
{children}
; +}; Page.propTypes = { + index: PropTypes.number.isRequired, children: PropTypes.node.isRequired, + id: PropTypes.string.isRequired, }; export default Page; diff --git a/src/components/Tabs/Pages.js b/src/components/Tabs/Pages.js index 76c9a1404..74728a88e 100644 --- a/src/components/Tabs/Pages.js +++ b/src/components/Tabs/Pages.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; const Pages = ({ children }) => React.Children.map(children, (child, index) => - React.cloneElement(child, { index }) + React.cloneElement(child, { ...child.props, index }) ); Pages.propTypes = { diff --git a/src/components/Tabs/Tabs.js b/src/components/Tabs/Tabs.js index 5579b5ae7..1a1eb9474 100644 --- a/src/components/Tabs/Tabs.js +++ b/src/components/Tabs/Tabs.js @@ -5,10 +5,11 @@ import TabsContext from './Context'; import Bar from './Bar'; import BarItem from './BarItem'; +import Pages from './Pages'; import Page from './Page'; const Tabs = ({ children }) => { - const tabState = useState(0); + const tabState = useState(undefined); return ( {children} @@ -21,6 +22,7 @@ Tabs.propTypes = { Tabs.Bar = Bar; Tabs.BarItem = BarItem; +Tabs.Pages = Pages; Tabs.Page = Page; export default Tabs; diff --git a/src/pages/tabtest.js b/src/pages/tabtest.js index be2891429..19f7d828a 100644 --- a/src/pages/tabtest.js +++ b/src/pages/tabtest.js @@ -4,19 +4,25 @@ import Tabs from '../components/Tabs'; const TabTest = () => ( - Overview - Dashboards - Alerts - + Overview + + Dashboards + + + Alerts + + Synthetics checks - This is the page for overview + + This is the page for overview - This is the page for dashboards.. + This is the page for dashboards.. - This is the page for aLeRtS + This is the page for aLeRtS + );