Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AdaptiveTabs): fix calc of tabs width #232

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/components/AdaptiveTabs/AdaptiveTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
const b = block('adaptive-tabs');
const TAB_CLASS_NAME = b('tab');
const getSortObjectKeysByValuesFunc =
(objectToSort: Record<string, any>) => (a: string, b: string) => {

Check warning on line 24 in src/components/AdaptiveTabs/AdaptiveTabs.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type

Check warning on line 24 in src/components/AdaptiveTabs/AdaptiveTabs.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'b' is already declared in the upper scope on line 21 column 7
if (objectToSort[a] > objectToSort[b]) {
return 1;
}
Expand Down Expand Up @@ -185,7 +185,7 @@
private tabsRootNode = React.createRef<HTMLDivElement>();
private tabsListNode = React.createRef<HTMLDivElement>();

get activeTab() {

Check warning on line 188 in src/components/AdaptiveTabs/AdaptiveTabs.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

getter functions should be placed after render
const {activeTab, items, allowNotSelected} = this.props;
if (activeTab) {
return activeTab;
Expand Down Expand Up @@ -213,7 +213,7 @@

this.breakpoints = Object.keys(props.breakpointsConfig)
.map(Number)
.sort((a, b) => a - b);

Check warning on line 216 in src/components/AdaptiveTabs/AdaptiveTabs.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'b' is already declared in the upper scope on line 21 column 7

/* save the object, where the keys are the "name" of the current container width, the values are the maximum width of the tab for
the corresponding container width (in percent of the container width) */
Expand All @@ -227,7 +227,7 @@
breakpointName = LARGE_CONTAINER_WIDTH_NAME;
}

accum[breakpointName] = props.breakpointsConfig[currentBreakpointWidth] || 100;

Check warning on line 230 in src/components/AdaptiveTabs/AdaptiveTabs.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Assignment to property of function parameter 'accum'

return accum;
},
Expand All @@ -246,9 +246,9 @@
this.setState({currentContainerWidthName: this.getCurrentContainerWidthName()});

if (this.tabsRootNode.current.clientWidth > this.breakpoints[0]) {
if ((document as any).fonts) {

Check warning on line 249 in src/components/AdaptiveTabs/AdaptiveTabs.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
(document as any).fonts.ready.then(this.initialCollectDimensions);

Check warning on line 250 in src/components/AdaptiveTabs/AdaptiveTabs.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
} else if ((document as any).readyState === READY_STATE_COMPLETE) {

Check warning on line 251 in src/components/AdaptiveTabs/AdaptiveTabs.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
setTimeout(this.initialCollectDimensions, 0);
} else {
window.addEventListener('load', this.initialCollectDimensions);
Expand Down Expand Up @@ -389,6 +389,7 @@
/*
Recalculating the Layout - Phases 1 and 2
*/
// eslint-disable-next-line complexity
recalculateTabs = () => {
if (!this.tabsRootNode.current) {
return;
Expand Down Expand Up @@ -436,7 +437,7 @@
of the switcher width will not exceed the width of the container */
for (let i = 0; i < this.tabsWidth.length; i++) {
renderedTabsSumWidth = renderedTabsSumWidth + this.tabsWidth[i];
const switcherWidthCorrection = i >= items.length - 1 ? 0 : this.switcherWidth;
const switcherWidthCorrection = i === items.length - 1 ? 0 : this.switcherWidth;
const isOverflown = renderedTabsSumWidth + switcherWidthCorrection > tabsRootNodeWidth;

if (firstHiddenTabIndexForSequentialCase === null && isOverflown) {
Expand All @@ -456,6 +457,10 @@
}
}

if (tabChosenFromSelectIndex > -1) {
maxHiddenTabWidth = this.tabsWidth[tabChosenFromSelectIndex];
}

if (maxHiddenTabWidth && typeof firstHiddenTabIndexForSequentialCase === 'number') {
let rightSpace = maxHiddenTabWidth - emptySpace;

Expand All @@ -465,10 +470,7 @@
firstHiddenTabIndex = firstHiddenTabIndexForSequentialCase;

for (let j = firstHiddenTabIndexForSequentialCase - 1; j >= 0; j--) {
// We need to use maxHiddenTabWidth instead of tabWidth[i],
// beacause the tab, that we are going to render at index j
// may be active tab, which can be wider than real tab at index j
rightSpace = rightSpace - maxHiddenTabWidth;
rightSpace = rightSpace - this.tabsWidth[j];

if (rightSpace < 0) {
firstHiddenTabIndex = j + (tabChosenFromSelectIndex > j ? 0 : 1);
Expand Down Expand Up @@ -507,6 +509,10 @@

this.setState({firstHiddenTabIndex});

if (firstHiddenTabIndex > tabChosenFromSelectIndex) {
this.setState({tabChosenFromSelectId: null});
}

/*phase 1 is complete, call the method of adjusting the width of the overflowing tabs to fill the entire width of the container*/
this.setUpOverflownTabs(firstHiddenTabIndex, activeTabIndex, tabsRootNodeWidth);

Expand Down Expand Up @@ -845,7 +851,7 @@

const maxWidth =
dimensionsWereCollected && typeof this.overflownTabsCurrentWidth[tabIndex] === 'number'
? this.overflownTabsCurrentWidth[tabIndex] - this.tabItemPaddingRight
? this.overflownTabsCurrentWidth[tabIndex] + this.tabItemPaddingRight
: `${this.tabMaxWidthInPercentsForScreenSize[currentContainerWidthName!]}%`;

const tabNode = <Tab {...item} active={item.id === activeTabID} />;
Expand Down
Loading