From 49dac2bd013764d4d79ad1f57364c270df88f3d2 Mon Sep 17 00:00:00 2001 From: HendrikThePendric Date: Wed, 13 Sep 2023 09:25:44 +0200 Subject: [PATCH] feat(pivot-table): truncate title and show full in tooltip --- src/__demo__/PivotTable.stories.js | 26 ++++++++++ .../PivotTable/PivotTableTitleRow.js | 52 ++++++++++++++----- .../PivotTable/styles/PivotTable.style.js | 8 ++- 3 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/__demo__/PivotTable.stories.js b/src/__demo__/PivotTable.stories.js index b4c46f346..2c631d079 100644 --- a/src/__demo__/PivotTable.stories.js +++ b/src/__demo__/PivotTable.stories.js @@ -1119,3 +1119,29 @@ storiesOf('PivotTable', module).add('DEGS', (_, { pivotTableOptions }) => { ) }) + +storiesOf('PivotTable', module).add( + 'Truncated header cell', + (_, { pivotTableOptions }) => { + const visualization = { + ...narrativeVisualization, + ...visualizationReset, + ...pivotTableOptions, + columns: narrativeVisualization.filters, + filters: narrativeVisualization.columns, + rowTotals: true, + colTotals: true, + } + + const data = { + ...narrativeData, + rows: [narrativeData.rows[0]], + } + + return ( +
+ +
+ ) + } +) diff --git a/src/components/PivotTable/PivotTableTitleRow.js b/src/components/PivotTable/PivotTableTitleRow.js index 681eaf1ab..7ce48226a 100644 --- a/src/components/PivotTable/PivotTableTitleRow.js +++ b/src/components/PivotTable/PivotTableTitleRow.js @@ -1,5 +1,6 @@ +import { Tooltip } from '@dhis2/ui' import PropTypes from 'prop-types' -import React, { useState, useEffect } from 'react' +import React, { useRef, useMemo } from 'react' import { PivotTableCell } from './PivotTableCell.js' import { usePivotTableEngine } from './PivotTableEngineContext.js' import { cell as cellStyle } from './styles/PivotTable.style.js' @@ -10,32 +11,55 @@ export const PivotTableTitleRow = ({ containerWidth, totalWidth, }) => { + const containerRef = useRef(null) const engine = usePivotTableEngine() const columnCount = engine.width + engine.rowDepth + const maxWidth = useMemo( + () => containerWidth - (engine.cellPadding * 2 + 2), + [containerWidth, engine.cellPadding] + ) + const marginLeft = useMemo( + () => + Math.max( + 0, + Math.min(scrollPosition?.x ?? 0, totalWidth - containerWidth) + ), + [containerWidth, scrollPosition.x, totalWidth] + ) + const titleIsTruncated = + containerRef.current?.scrollWidth > containerRef.current?.clientWidth - const [position, setPosition] = useState(scrollPosition.x) - useEffect(() => { - setPosition( - Math.max(0, Math.min(scrollPosition.x, totalWidth - containerWidth)) - ) - }, [containerWidth, scrollPosition.x, totalWidth]) return (
- {title} + {titleIsTruncated ? ( + + {({ ref: tooltipRef, onMouseOver, onMouseOut }) => ( +
+ {title} +
+ )} +
+ ) : ( + title + )}
diff --git a/src/components/PivotTable/styles/PivotTable.style.js b/src/components/PivotTable/styles/PivotTable.style.js index 23cfab64b..74bf840ec 100644 --- a/src/components/PivotTable/styles/PivotTable.style.js +++ b/src/components/PivotTable/styles/PivotTable.style.js @@ -109,10 +109,16 @@ export const cell = css` align-items: center; justify-content: center; } - .title { + .title-cell { font-weight: bold; background-color: #cddaed; } + .title-cell-content { + text-align: center; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + } .row-header { background-color: #dae6f8; }