Skip to content

Commit

Permalink
fix(Tabs): uncontrolled Tabs react to updated defaultActiveKey (#10317)
Browse files Browse the repository at this point in the history
* fix(Tabs): uncontrolled Tabs react to updated defaultActiveKey

* fix(Tabs): update state only in uncontrolled tabs
  • Loading branch information
adamviktora authored May 8, 2024
1 parent 3135c37 commit 8e21814
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/react-core/src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,27 @@ class Tabs extends React.Component<TabsProps, TabsState> {
this.direction = getLanguageDirection(this.tabList.current);
}

static getDerivedStateFromProps(nextProps: TabsProps, prevState: TabsState) {
if (prevState.uncontrolledActiveKey === undefined) {
return null;
}

const childrenHasTabWithActiveEventKey = React.Children.toArray(nextProps.children)
.filter((child): child is TabElement => React.isValidElement(child))
.some(({ props }) => props.eventKey === prevState.uncontrolledActiveKey);

// if uncontrolledActiveKey is an existing eventKey of any Tab of nextProps.children --> don't update uncontrolledActiveKey
if (childrenHasTabWithActiveEventKey) {
return null;
}

// otherwise update state derived from nextProps.defaultActiveKey
return {
uncontrolledActiveKey: nextProps.defaultActiveKey,
shownKeys: nextProps.defaultActiveKey !== undefined ? [nextProps.defaultActiveKey] : [nextProps.activeKey] // only for mountOnEnter case
};
}

render() {
const {
className,
Expand Down

0 comments on commit 8e21814

Please sign in to comment.