Skip to content

Commit

Permalink
Fix tab reference
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 26, 2023
1 parent 29f6344 commit 4d3aae7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions code/ui/components/src/tabs/tabs.hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useLayoutEffect, useRef, useState } from 'react';
import React, { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { sanitize } from '@storybook/csf';
import { styled } from '@storybook/theming';
import useResizeObserver from 'use-resize-observer';
Expand Down Expand Up @@ -105,7 +105,7 @@ export function useList(list: ChildrenList) {
style={{ visibility: 'hidden' }}
tabIndex={-1}
ref={(ref: HTMLButtonElement) => {
tabRefs.current.set(title, ref);
tabRefs.current.set(id, ref);
}}
className="tabbutton"
type="button"
Expand Down Expand Up @@ -134,8 +134,8 @@ export function useList(list: ChildrenList) {
let widthSum = 0;

const newInvisibleList = list.filter((item) => {
const { title } = item;
const tabButton = tabRefs.current.get(title);
const { id } = item;
const tabButton = tabRefs.current.get(id);

if (!tabButton) {
return false;
Expand All @@ -156,10 +156,15 @@ export function useList(list: ChildrenList) {
if (newVisibleList.length !== visibleList.length || previousList.current !== list) {
setVisibleList(newVisibleList);
setInvisibleList(newInvisibleList);
previousList.current = list;
}
}, [invisibleList.length, list, visibleList]);

useEffect(() => {
if (previousList.current !== list) {
previousList.current = list;
}
}, [list]);

useLayoutEffect(setTabLists, [setTabLists, tabBarWidth]);

return {
Expand Down
4 changes: 2 additions & 2 deletions code/ui/components/src/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ export const Tabs: FC<TabsProps> = memo(
<Wrapper absolute={absolute} bordered={bordered} id={htmlId}>
<WrapperChildren backgroundColor={backgroundColor}>
<TabBar ref={tabBarRef} role="tablist">
{visibleList.map(({ title, id, active, color }, index) => {
{visibleList.map(({ title, id, active, color }) => {
return (
<TabButton
id={`tabbutton-${sanitize(title)}`}
ref={(ref: HTMLButtonElement) => {
tabRefs.current.set(title, ref);
tabRefs.current.set(id, ref);
}}
className={`tabbutton ${active ? 'tabbutton-active' : ''}`}
type="button"
Expand Down

0 comments on commit 4d3aae7

Please sign in to comment.