Skip to content

Commit

Permalink
feat(ui-tabs): add active property to tabs
Browse files Browse the repository at this point in the history
Closes: INSTUI-3876

Add support for multiple tabs->one tab panel. This makes
using React Router easier with less boilerplate code.
  • Loading branch information
joyenjoyer committed Oct 18, 2023
1 parent 6043660 commit 7a0f2b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/ui-tabs/src/Tabs/Panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class Panel extends Component<TabsPanelProps> {
textAlign: 'start',
variant: 'default',
isSelected: false,
padding: 'small'
padding: 'small',
active: 'false'
}

componentDidMount() {
Expand Down Expand Up @@ -92,6 +93,7 @@ class Panel extends Component<TabsPanelProps> {
isDisabled,
isSelected,
styles,
active,
...props
} = this.props
const isHidden = !isSelected || !!isDisabled
Expand Down
11 changes: 9 additions & 2 deletions packages/ui-tabs/src/Tabs/Panel/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ type TabsPanelOwnProps = {
* provides a reference to the underlying html root element
*/
elementRef?: (element: HTMLDivElement | null) => void
/**
* Only one `<Tabs.Panel />` can be marked as active. The marked panel's content is rendered
* for all the `<Tabs.Panel />`s.
*/
active?: boolean
}

type PropKeys = keyof TabsPanelOwnProps
Expand All @@ -82,7 +87,8 @@ const propTypes: PropValidators<PropKeys> = {
labelledBy: PropTypes.string,
padding: ThemeablePropTypes.spacing,
textAlign: PropTypes.oneOf(['start', 'center', 'end']),
elementRef: PropTypes.func
elementRef: PropTypes.func,
active: PropTypes.bool
}

const allowedProps: AllowedPropKeys = [
Expand All @@ -97,7 +103,8 @@ const allowedProps: AllowedPropKeys = [
'labelledBy',
'padding',
'textAlign',
'elementRef'
'elementRef',
'active'
]

export type { TabsPanelProps, TabsPanelStyle }
Expand Down
17 changes: 16 additions & 1 deletion packages/ui-tabs/src/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,17 @@ class Tabs extends Component<TabsProps, TabsState> {
...props
} = this.props

const activePanels = (React.Children.toArray(children) as PanelChild[])
.filter((child) => matchComponentTypes<PanelChild>(child, [Panel]))
.filter((child) => child.props.active)

if (
activePanels.length >
1 /*|| (activePanelCount === 1 && React.Children.toArray(children).length > 1)*/
) {
error(false, `[Tabs] Only one Panel can be marked as active.`)
}

const selectedChildIndex = (
React.Children.toArray(children) as PanelChild[]
)
Expand All @@ -447,7 +458,11 @@ class Tabs extends Component<TabsProps, TabsState> {
const id = uid()

tabs.push(this.createTab(index, id, selected, child))
panels.push(this.clonePanel(index, id, selected, child))
if (activePanels.length === 1) {
panels.push(this.clonePanel(index, id, selected, activePanels[0]))
} else {
panels.push(this.clonePanel(index, id, selected, child))
}

index++
} else {
Expand Down

0 comments on commit 7a0f2b7

Please sign in to comment.