From 338b5491f1e2dec95b6e096a399f2950eced3709 Mon Sep 17 00:00:00 2001 From: HendrikThePendric Date: Tue, 26 Sep 2023 17:14:07 +0200 Subject: [PATCH] chore(pivot-table): determine title scroll width when element is mounted --- src/__demo__/PivotTable.stories.js | 17 ++++++++++++++++- src/components/PivotTable/PivotTableTitleRow.js | 13 ++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/__demo__/PivotTable.stories.js b/src/__demo__/PivotTable.stories.js index 2c631d079..89dea8aa3 100644 --- a/src/__demo__/PivotTable.stories.js +++ b/src/__demo__/PivotTable.stories.js @@ -1123,6 +1123,13 @@ storiesOf('PivotTable', module).add('DEGS', (_, { pivotTableOptions }) => { storiesOf('PivotTable', module).add( 'Truncated header cell', (_, { pivotTableOptions }) => { + const widths = [250, 200, 400] + const [width, setWidth] = useState(250) + const toggleWidth = () => + setWidth( + (currentWidth) => + widths[widths.indexOf(currentWidth) + 1] ?? widths[0] + ) const visualization = { ...narrativeVisualization, ...visualizationReset, @@ -1139,7 +1146,15 @@ storiesOf('PivotTable', module).add( } return ( -
+
+
) diff --git a/src/components/PivotTable/PivotTableTitleRow.js b/src/components/PivotTable/PivotTableTitleRow.js index 7ce48226a..fbab46daf 100644 --- a/src/components/PivotTable/PivotTableTitleRow.js +++ b/src/components/PivotTable/PivotTableTitleRow.js @@ -1,6 +1,6 @@ import { Tooltip } from '@dhis2/ui' import PropTypes from 'prop-types' -import React, { useRef, useMemo } from 'react' +import React, { useRef, useMemo, useCallback } from 'react' import { PivotTableCell } from './PivotTableCell.js' import { usePivotTableEngine } from './PivotTableEngineContext.js' import { cell as cellStyle } from './styles/PivotTable.style.js' @@ -12,6 +12,13 @@ export const PivotTableTitleRow = ({ totalWidth, }) => { const containerRef = useRef(null) + const initialScrollWidthRef = useRef(null) + const handleContainerMount = useCallback((node) => { + if (node) { + containerRef.current = node + initialScrollWidthRef.current = node.scrollWidth + } + }, []) const engine = usePivotTableEngine() const columnCount = engine.width + engine.rowDepth const maxWidth = useMemo( @@ -27,7 +34,7 @@ export const PivotTableTitleRow = ({ [containerWidth, scrollPosition.x, totalWidth] ) const titleIsTruncated = - containerRef.current?.scrollWidth > containerRef.current?.clientWidth + initialScrollWidthRef.current > containerRef.current?.clientWidth return ( @@ -39,7 +46,7 @@ export const PivotTableTitleRow = ({ >