Skip to content

Commit

Permalink
🚸 Tabs: make conditionalRender prop for Panels (equinor#3062)
Browse files Browse the repository at this point in the history
* 🚸 Tabs: make conditionalRender prop for Panels

* Tabs: update documentation
  • Loading branch information
oddvernes authored Sep 20, 2023
1 parent a2c8688 commit bc684cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/eds-core-react/src/components/Tabs/TabPanels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import {
} from 'react'
import { TabsContext } from './Tabs.context'

export type TabPanelsProps = HTMLAttributes<HTMLDivElement>
export type TabPanelsProps = {
conditionalRender?: boolean
} & HTMLAttributes<HTMLDivElement>

const TabPanels = forwardRef<HTMLDivElement, TabPanelsProps>(function TabPanels(
{ children, ...props },
{ children, conditionalRender, ...props },
ref,
) {
const { activeTab, tabsId } = useContext(TabsContext)

const Panels = ReactChildren.map(children, (child: ReactElement, $index) => {
if (activeTab !== $index) return null
if (conditionalRender && activeTab !== $index) return null
return cloneElement(child, {
id: `${tabsId}-panel-${$index + 1}`,
'aria-labelledby': `${tabsId}-tab-${$index + 1}`,
hidden: activeTab !== $index,
})
})

Expand Down
2 changes: 2 additions & 0 deletions packages/eds-core-react/src/components/Tabs/Tabs.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ To navigate using the keyboard use:

Focus outline is only visible when navigating using the keyboard.

The non-active `Tabs.Panels` are by default hidden with `display: none`, but can instead be conditionally rendered by adding the `conditionalRender` prop

<Canvas of={ComponentStories.WithPanels} />

### With third party routers
Expand Down
10 changes: 9 additions & 1 deletion packages/eds-core-react/src/components/Tabs/Tabs.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ const meta: Meta<typeof Tabs> = {
Panels: Tabs.Panels,
Panel: Tabs.Panel,
},
argTypes: {
activeTab: {
options: [0, 1],
control: {
type: 'select',
},
},
},
parameters: {
docs: {
page,
Expand Down Expand Up @@ -175,7 +183,7 @@ export const WithPanels: StoryFn<TabsProps> = () => {
<Tabs.Tab disabled>Tab three</Tabs.Tab>
<Tabs.Tab>Tab four</Tabs.Tab>
</Tabs.List>
<Tabs.Panels>
<Tabs.Panels conditionalRender>
<Tabs.Panel>Panel one</Tabs.Panel>
<Tabs.Panel>Panel two</Tabs.Panel>
<Tabs.Panel>Panel three</Tabs.Panel>
Expand Down

0 comments on commit bc684cb

Please sign in to comment.