-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4baab33
commit 8a2e4e4
Showing
2 changed files
with
73 additions
and
25 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/common/components/mock-components/front-rich-components/tabsbar/tab-list.hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters