diff --git a/components/tab/package.json b/components/tab/package.json index ba8d0c0427..e13dbcf445 100644 --- a/components/tab/package.json +++ b/components/tab/package.json @@ -32,6 +32,7 @@ "styled-jsx": "^4" }, "dependencies": { + "@dhis2-ui/tooltip": "^9.4.3", "@dhis2/prop-types": "^3.1.2", "@dhis2/ui-constants": "9.6.0", "@dhis2/ui-icons": "9.6.0", diff --git a/components/tab/src/tab-bar/tabs.js b/components/tab/src/tab-bar/tabs.js index e999671f0d..8b586bf8bb 100644 --- a/components/tab/src/tab-bar/tabs.js +++ b/components/tab/src/tab-bar/tabs.js @@ -1,26 +1,86 @@ import { colors } from '@dhis2/ui-constants' import cx from 'classnames' import PropTypes from 'prop-types' -import React from 'react' - -const Tabs = ({ children, fixed, dataTest }) => ( -
- {children} - - -
-) + return + } + + const currentIndex = childrenRefs.findIndex( + (ref) => ref.current === currentFocus + ) + + if (currentIndex === -1) { + return + } + + if (event.key === 'ArrowRight') { + event.preventDefault() + const nextIndex = (currentIndex + 1) % childrenRefs.length + childrenRefs[nextIndex].current.focus() + } + + if (event.key === 'ArrowLeft') { + event.preventDefault() + const prevIndex = + (currentIndex - 1 + childrenRefs.length) % childrenRefs.length + childrenRefs[prevIndex].current.focus() + } + } + + return ( +
+ {React.Children.map(children, (child, index) => { + if (React.isValidElement(child)) { + return React.cloneElement(child, { + ref: childrenRefs[index], + }) + } + // Wrap non-element children e.g string in a + return ( + + {child} + + ) + })} + + +
+ ) +} Tabs.propTypes = { dataTest: PropTypes.string.isRequired, diff --git a/components/tab/src/tab/tab.js b/components/tab/src/tab/tab.js index bb85dcec9c..9a8cd0ae71 100644 --- a/components/tab/src/tab/tab.js +++ b/components/tab/src/tab/tab.js @@ -1,136 +1,164 @@ +import { Tooltip } from '@dhis2-ui/tooltip' import { colors, theme } from '@dhis2/ui-constants' import cx from 'classnames' import PropTypes from 'prop-types' -import React from 'react' - -const Tab = ({ - icon, - onClick, - selected, - disabled, - children, - className, - dataTest, -}) => ( - +import React, { useState, useEffect, useRef } from 'react' + +export const Tab = React.forwardRef( + ( + { icon, onClick, selected, disabled, children, className, dataTest }, + ref + ) => { + let tabRef = useRef(null) + if (ref) { + tabRef = ref + } + const [isOverflowing, setIsOverflowing] = useState(false) + + useEffect(() => { + const checkOverflow = () => { + const isOverflow = + tabRef.current.scrollWidth > tabRef.current.clientWidth + setIsOverflowing(isOverflow) + } + checkOverflow() + }, []) + + return ( + + ) + } ) Tab.defaultProps = { @@ -149,4 +177,4 @@ Tab.propTypes = { onClick: PropTypes.func, } -export { Tab } +Tab.displayName = 'Tab'