Skip to content

Commit

Permalink
refactor tabsbar with hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Alber-Writer committed Nov 12, 2024
1 parent 4baab33 commit 8a2e4e4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useEffect, useState } from 'react';
import { adjustTabWidths } from './business/tabsbar.business';
import {
extractCSVHeaders,
splitCSVContentIntoRows,
} from '@/common/utils/active-element-selector.utils';

interface TabListConfig {
text: string;
containerWidth: number;
minTabWidth: number;
tabXPadding: number;
tabsGap: number;
font: {
fontSize: number;
fontFamily: string;
};
}

export const useTabList = (tabsConfig: TabListConfig) => {
const { text, containerWidth, minTabWidth, tabXPadding, tabsGap, font } =
tabsConfig;

const [tabWidthList, setTabWidthList] = useState<{
widthList: number[];
relativeTabPosition: number[];
}>({ widthList: [], relativeTabPosition: [] });

const tabLabels = _extractTabLabelTexts(text);

useEffect(() => {
setTabWidthList(
adjustTabWidths({
tabs: tabLabels,
containerWidth,
minTabWidth,
tabXPadding,
tabsGap,
font: {
fontSize: font.fontSize,
fontFamily: font.fontFamily,
},
})
);
}, [text, containerWidth]);

//Return an unique array with all the info required by each tab
return tabLabels.map((tab, index) => ({
tab,
width: tabWidthList.widthList[index],
xPos: tabWidthList.relativeTabPosition[index],
}));
};

// Split text to tab labels List
function _extractTabLabelTexts(text: string) {
const csvData = splitCSVContentIntoRows(text);
const headers = extractCSVHeaders(csvData[0]);
return headers.map(header => header.text);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import { Group, Rect, Text } from 'react-konva';
import { ShapeSizeRestrictions, ShapeType } from '@/core/model';
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions';
import { ShapeProps } from '../../shape.model';
import {
extractCSVHeaders,
splitCSVContentIntoRows,
} from '@/common/utils/active-element-selector.utils';
import { useGroupShapeProps } from '../../mock-components.utils';
import { adjustTabWidths } from './business/tabsbar.business';
import { useTabList } from './tab-list.hook';

const tabsBarShapeSizeRestrictions: ShapeSizeRestrictions = {
minWidth: 450,
Expand Down Expand Up @@ -43,24 +39,19 @@ export const TabsBarShape = forwardRef<any, ShapeProps>((props, ref) => {
);
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;

const csvData = splitCSVContentIntoRows(text);
const headers = extractCSVHeaders(csvData[0]);
const tabLabels = headers.map(header => header.text);

// Calculate tab dimensions and margin
const minTabWidth = 40; // Min-width of each tab, without xPadding
// Tab dimensions and margin
const tabHeight = 30; // Tab height
const tabMargin = 10; // Horizontal margin between tabs
const tabsGap = 10; // Horizontal margin between tabs
const tabXPadding = 20;
const tabFont = { fontSize: 14, fontFamily: 'Arial' };
const bodyHeight = restrictedHeight - tabHeight - 10; // Height of the tabs bar body

const tabAdjustedWidths = adjustTabWidths({
tabs: tabLabels,
containerWidth: restrictedWidth - tabMargin * 2, //left and right tabList margin
minTabWidth,
const tabList = useTabList({
text,
containerWidth: restrictedWidth - tabsGap * 2, //left and right tabList margin
minTabWidth: 40, // Min-width of each tab, without xPadding
tabXPadding,
tabsGap: tabMargin,
tabsGap,
font: tabFont,
});

Expand All @@ -86,14 +77,11 @@ export const TabsBarShape = forwardRef<any, ShapeProps>((props, ref) => {
fill="white"
/>
{/* Map through headerRow to create tabs */}
{tabLabels.map((header, index) => {
const { widthList: newWidthList, relativeTabPosition: xPosList } =
tabAdjustedWidths;

{tabList.map(({ tab, width, xPos }, index) => {
return (
<Group key={index} x={10 + xPosList[index]} y={10}>
<Group key={index} x={10 + xPos} y={10}>
<Rect
width={newWidthList[index]}
width={width}
height={tabHeight}
fill={index === activeTab ? 'white' : '#E0E0E0'}
stroke="black"
Expand All @@ -102,11 +90,11 @@ export const TabsBarShape = forwardRef<any, ShapeProps>((props, ref) => {
<Text
x={tabXPadding}
y={8}
width={newWidthList[index] - tabXPadding * 2}
width={width - tabXPadding * 2}
height={tabHeight}
ellipsis={true}
wrap="none"
text={header} // Use the header text
text={tab}
fontFamily={tabFont.fontFamily}
fontSize={tabFont.fontSize}
fill="black"
Expand Down

0 comments on commit 8a2e4e4

Please sign in to comment.