Skip to content

Commit

Permalink
fix(comparison-table): Fix comparison table not setting ref on load (#…
Browse files Browse the repository at this point in the history
…256)

* fix(comparison-table): Fix comparison table not setting ref on load

* remove console.log

* fix hardcoded userId
  • Loading branch information
diogomateus authored Aug 24, 2023
1 parent 4d78f78 commit 44b85e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/lib/components/comparisonTable/hooks/useComparisonTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import debounce from 'lodash.debounce';
import { useCallback, useEffect, useRef, useState } from 'react';

import { ArrowValues } from '../components/TableArrows';
import generateId from '../../../util/generateId';

export const useComparisonTable = () => {
const [showMore, setShowMore] = useState<boolean>(false);
const [headerWidth, setHeaderWidth] = useState(1400);

const [headerId, setHeaderId] = useState('');
const [selectedTabIndex, setSelectedTabIndex] = useState(0);

const [selectedSection, setSelectedSection] = useState('');

const headerRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -110,8 +110,25 @@ export const useComparisonTable = () => {
setShowMore(!showMore);
};

useEffect(() => {
if (headerRef.current) {
return;
}

const headerById = document.getElementById(headerId);

if (headerById) {
scrollContainerCallbackRef(headerById);
}
}, [headerId, scrollContainerCallbackRef]);

useEffect(() => {
setHeaderId(generateId());
}, []);

return {
headerWidth,
headerId,
contentContainerRef,
selectedSection,
setSelectedSection,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/components/comparisonTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import { Fragment } from 'react';
import { Fragment, useEffect } from 'react';
import { ScrollSync, ScrollSyncPane } from 'react-scroll-sync';

import { AccordionItem } from './components/AccordionItem';
Expand Down Expand Up @@ -85,6 +85,7 @@ const ComparisonTable = <T extends { id: number }>(
handleArrowsClick,
toggleMoreRows,
showMore,
headerId,
} = useComparisonTable();

const cssVariablesStyle = {
Expand All @@ -105,6 +106,7 @@ const ComparisonTable = <T extends { id: number }>(
<div className={classNames(baseStyles.header, styles?.header)}>
<ScrollSyncPane>
<div
id={headerId}
className={classNames(baseStyles.container, {
[baseStyles.noScrollBars]: hideScrollBars,
})}
Expand Down

0 comments on commit 44b85e8

Please sign in to comment.