Skip to content

Commit

Permalink
chore(pivot-table): determine title scroll width when element is mounted
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikThePendric committed Sep 26, 2023
1 parent adba099 commit 338b549
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/__demo__/PivotTable.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -1139,7 +1146,15 @@ storiesOf('PivotTable', module).add(
}

return (
<div style={{ width: 250, height: 600, marginTop: 50 }}>
<div
style={{
width,
height: 600,
marginTop: 50,
transition: 'width 150ms',
}}
>
<button onClick={toggleWidth}>Toggle width</button>
<PivotTable data={data} visualization={visualization} />
</div>
)
Expand Down
13 changes: 10 additions & 3 deletions src/components/PivotTable/PivotTableTitleRow.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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(
Expand All @@ -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 (
<tr>
Expand All @@ -39,7 +46,7 @@ export const PivotTableTitleRow = ({
>
<div
style={{ marginLeft, maxWidth }}
ref={containerRef}
ref={handleContainerMount}
data-test="visualization-title"
className="title-cell-content"
>
Expand Down

0 comments on commit 338b549

Please sign in to comment.