Merge merge request !25 (clear stale chart hover state)
This commit is contained in:
commit
651b03cbd1
2 files changed
+37
-1
No files matched your search
@@ -18,6 +18,12 @@ export default function LineChart({ points, h = 150, unit = '', color = 'var(--a
|
||||
const tipRef = useRef(null)
|
||||
const [hover, setHover] = useState(null) // { x, y, iso, v }
|
||||
|
||||
// A new dataset has different coordinates and meaning, so never carry the previous
|
||||
// point selection into it. Layout timing removes the marker before the new chart paints.
|
||||
useLayoutEffect(() => {
|
||||
setHover(null)
|
||||
}, [points])
|
||||
|
||||
// The tooltip is placed after layout, from its measured size, because the chart
|
||||
// lives in an overflow-clipped box: a fixed half-width offset (what this used to
|
||||
// do) hangs the label off the edge on the first and last point, and the clip then
|
||||
|
||||
@@ -27,6 +27,15 @@ const point = (year, month, day, y) => ({
|
||||
y
|
||||
})
|
||||
|
||||
const firstPoints = [
|
||||
{ t: Date.UTC(2026, 0, 1), y: 80, d: '2026-01-01' },
|
||||
{ t: Date.UTC(2026, 0, 15), y: 82, d: '2026-01-15' },
|
||||
]
|
||||
const nextPoints = [
|
||||
{ t: Date.UTC(2026, 1, 1), y: 78, d: '2026-02-01' },
|
||||
{ t: Date.UTC(2026, 1, 15), y: 79, d: '2026-02-15' },
|
||||
]
|
||||
|
||||
function renderChart(points) {
|
||||
act(() => root.render(<LineChart points={points} axes={false} unit="kg" />))
|
||||
}
|
||||
@@ -71,4 +80,25 @@ describe('LineChart hover date', () => {
|
||||
const lastIso = isoOf(new Date(last.t))
|
||||
expect(hoverAt(340)).toBe(`${fmtDate(lastIso, true, true)} · 71 kg`)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('LineChart hover state', () => {
|
||||
it('clears the tooltip and hover markers when points are replaced, then allows hovering again', () => {
|
||||
renderChart(firstPoints)
|
||||
hoverAt(170)
|
||||
|
||||
expect(container.querySelector('.ctip')).toBeTruthy()
|
||||
expect(container.querySelector('.cvl')).toBeTruthy()
|
||||
expect(container.querySelector('.chl')).toBeTruthy()
|
||||
|
||||
renderChart(nextPoints)
|
||||
|
||||
expect(container.querySelector('.ctip')).toBeNull()
|
||||
expect(container.querySelector('.cvl')).toBeNull()
|
||||
expect(container.querySelector('.chl')).toBeNull()
|
||||
|
||||
hoverAt(170)
|
||||
expect(container.querySelector('.ctip')).toBeTruthy()
|
||||
expect(container.querySelector('.cvl')).toBeTruthy()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user