Skip to content

Commit

Permalink
Merge pull request #26899 from storybookjs/chore/fix-rendering-of-sta…
Browse files Browse the repository at this point in the history
…teful-tabs-component

UI: Fix not re-rendering tabs on state change
  • Loading branch information
JReinhold authored Apr 24, 2024
2 parents 18e298a + 2aadfc1 commit 4c1d585
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
24 changes: 24 additions & 0 deletions code/ui/components/src/components/tabs/tabs.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ interface FibonacciMap {
[key: string]: number;
}

function Counter() {
const [count, setCount] = React.useState(0);
return <button onClick={() => setCount((prev) => prev + 1)}>{count}</button>;
}

function fibonacci(num: number, memo?: FibonacciMap): number {
if (!memo) {
memo = {};
Expand Down Expand Up @@ -376,3 +381,22 @@ export const StatelessWithCustomEmpty = {
/>
),
} satisfies StoryObj<typeof Tabs>;

export const StatefulWithStatefulPanel = {
render: (args) => {
const [update, setUpdate] = React.useState(0);
return (
<div>
<button onClick={() => setUpdate((prev) => prev + 1)}>Update</button>
<TabsState initial="test-1" {...args}>
<div id="test-1" title="Test 1">
<Counter key={update} />
</div>
<div id="test-2" title="Test 2">
<Counter key={update} />
</div>
</TabsState>
</div>
);
},
} satisfies Story;
7 changes: 1 addition & 6 deletions code/ui/components/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,13 @@ export const Tabs: FC<TabsProps> = memo(
emptyState,
showToolsWhenEmpty,
}) => {
const idList = childrenToList(children)
.map((i) => i.id)
.join(',');

const list = useMemo(
() =>
childrenToList(children).map((i, index) => ({
...i,
active: selected ? i.id === selected : index === 0,
})),
// eslint-disable-next-line react-hooks/exhaustive-deps -- we're using idList as a replacement for children
[selected, idList]
[children, selected]
);

const { visibleList, tabBarRef, tabRefs, AddonTab } = useList(list);
Expand Down

0 comments on commit 4c1d585

Please sign in to comment.