-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tabs typescript #715
Tabs typescript #715
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
// @ts-nocheck | ||
import React, { forwardRef } from 'react' | ||
import PropTypes from 'prop-types' | ||
import styled from 'styled-components' | ||
import { tabPanel as tokens } from './Tabs.tokens' | ||
|
||
|
@@ -12,10 +10,12 @@ const { | |
}, | ||
} = tokens | ||
|
||
const StyledTabPanel = styled.div.attrs(() => ({ | ||
tabIndex: 0, | ||
role: 'tabpanel', | ||
}))({ | ||
const StyledTabPanel = styled.div.attrs( | ||
(): React.HTMLAttributes<HTMLDivElement> => ({ | ||
tabIndex: 0, | ||
role: 'tabpanel', | ||
}), | ||
)({ | ||
paddingTop, | ||
paddingBottom, | ||
outline: 'none', | ||
|
@@ -25,26 +25,20 @@ const StyledTabPanel = styled.div.attrs(() => ({ | |
}, | ||
}) | ||
|
||
const TabPanel = forwardRef(function TabPanel({ ...props }, ref) { | ||
type Props = { | ||
/** If `true`, the panel will be hidden. */ | ||
hidden?: boolean | ||
} & React.HTMLAttributes<HTMLDivElement> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, add |
||
|
||
const TabPanel = forwardRef<HTMLDivElement, Props>(function TabPanel( | ||
{ ...props }, | ||
ref, | ||
) { | ||
return ( | ||
<StyledTabPanel ref={ref} {...props}> | ||
{props.children} | ||
</StyledTabPanel> | ||
) | ||
}) | ||
|
||
TabPanel.propTypes = { | ||
/** @ignore */ | ||
children: PropTypes.node.isRequired, | ||
/** @ignore */ | ||
className: PropTypes.string, | ||
/** If `true`, the panel will be hidden. */ | ||
hidden: PropTypes.bool, | ||
} | ||
|
||
TabPanel.defaultProps = { | ||
className: null, | ||
hidden: null, | ||
} | ||
|
||
export { TabPanel } |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { forwardRef, ReactElement, useContext } from 'react' | ||
import { TabsContext } from './Tabs.context' | ||
|
||
type Props = React.HTMLAttributes<HTMLDivElement> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, add |
||
|
||
const TabPanels = forwardRef<HTMLDivElement, Props>(function TabPanels( | ||
{ children, ...props }, | ||
ref, | ||
) { | ||
const { activeTab, tabsId } = useContext(TabsContext) | ||
|
||
const Panels = React.Children.map(children, (child: ReactElement, index) => | ||
React.cloneElement(child, { | ||
id: `${tabsId}-panel-${index + 1}`, | ||
'aria-labelledby': `${tabsId}-tab-${index + 1}`, | ||
hidden: activeTab !== index, | ||
}), | ||
) | ||
return ( | ||
<div ref={ref} {...props}> | ||
{Panels} | ||
</div> | ||
) | ||
}) | ||
|
||
export { TabPanels } |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createContext } from 'react' | ||
import { Variants } from './Tabs.types' | ||
|
||
type State = { | ||
variant: Variants | ||
handleChange: (index: number) => void | ||
activeTab: number | ||
tabsId: string | ||
tabsFocused: boolean | ||
} | ||
|
||
const TabsContext = createContext<State>({ | ||
variant: 'minWidth', | ||
handleChange: () => null, | ||
activeTab: 0, | ||
tabsId: '', | ||
tabsFocused: false, | ||
}) | ||
|
||
const TabsProvider = TabsContext.Provider | ||
const TabsConsumer = TabsContext.Consumer | ||
|
||
export { TabsContext, TabsProvider, TabsConsumer } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// @ts-nocheck | ||
import { tokens } from '@equinor/eds-tokens' | ||
|
||
const { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
children
should be mentioned here as it is required, right? 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed during standup. Added tests and fallbacks for if children is not defined to stay as close as possible to native behaviour