An open source design system that's fully customizable and agent ready
TypeScript
refactor(ChartZoom): take the reset domains from the first render, not an Effect (#9809)
Cindy Zhang0b39b51c2f97
diff --git a/packages/lab/src/Chart/ChartZoom.test.tsx b/packages/lab/src/Chart/ChartZoom.test.tsxindex 92580ad938..64ce06073f 100644--- a/packages/lab/src/Chart/ChartZoom.test.tsx+++ b/packages/lab/src/Chart/ChartZoom.test.tsx@@ -3,14 +3,15 @@/*** @file ChartZoom.test.tsx* @input Renders ChartZoom inside Chart with a stubbed ResizeObserver- * @output Render-smoke coverage for zoom: capture surface, portaled toolbar, toolbar opt-out.+ * @output Render-smoke coverage for zoom: capture surface, portaled toolbar, toolbar opt-out,+ * and Reset back to the first render's domains.* Wheel/pinch gestures need createSVGPoint/getScreenCTM, which do not exist in* jsdom, so they are intentionally out of scope here. (#4295)* @position Colocated test for ChartZoom.tsx*/import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';-import {render, screen, act} from '@testing-library/react';+import {render, screen, act, fireEvent} from '@testing-library/react';import {Chart} from './Chart';import {ChartZoom} from './ChartZoom';@@ -87,6 +88,44 @@ describe('ChartZoom', () => {).toBeInTheDocument();});+ it("resets to the first render's domains after the caller changes them", () => {+ const onXDomainChange = vi.fn();+ const onYDomainChange = vi.fn();+ function ZoomedChart({+ xDomain,+ yDomain,+ }: {+ xDomain: [number, number];+ yDomain: [number, number];+ }) {+ return (+ <Chart+ data={data}+ xKey="x"+ yKeys={['y']}+ xDomain={xDomain}+ yDomain={yDomain}+ interactive>+ <ChartZoom+ onXDomainChange={onXDomainChange}+ onYDomainChange={onYDomainChange}+ />+ </Chart>+ );+ }+ const {rerender} = render(+ <ZoomedChart xDomain={[0, 2]} yDomain={[0, 4]} />,+ );+ reportWidth(464);+ reportWidth(465);++ rerender(<ZoomedChart xDomain={[0.5, 1.5]} yDomain={[1, 3]} />);+ fireEvent.click(screen.getByRole('button', {name: 'Reset zoom'}));++ expect(onXDomainChange).toHaveBeenLastCalledWith([0, 2]);+ expect(onYDomainChange).toHaveBeenLastCalledWith([0, 4]);+ });+it('renders no toolbar when toolbar is false', () => {render(<Chart data={data} xKey="x" yKeys={['y']} interactive>diff --git a/packages/lab/src/Chart/ChartZoom.tsx b/packages/lab/src/Chart/ChartZoom.tsxindex d3b7de99f5..5963dca390 100644--- a/packages/lab/src/Chart/ChartZoom.tsx+++ b/packages/lab/src/Chart/ChartZoom.tsx@@ -7,10 +7,10 @@** Zoom is aspect-ratio-normalized: both axes scale by the same visual* factor regardless of their domain ranges. Reset button restores- * the initial domains captured on mount.+ * the domains of the first render.*/-import {type CSSProperties, useCallback, useRef, useEffect} from 'react';+import {type CSSProperties, useCallback, useRef, useState} from 'react';import {createPortal} from 'react-dom';import {IconButton} from '@astryxdesign/core/IconButton';import {useChart} from './ChartContext';@@ -101,22 +101,15 @@ export function ChartZoom({}: ChartZoomProps) {const {width, height, xScale, yScale, svgRef} = useChart();- // Capture initial domains on mount for reset- const initialDomainsRef = useRef<{+ const [initialDomains] = useState<{x: [number, number] | null;y: [number, number];- } | null>(null);-- useEffect(() => {- if (initialDomainsRef.current) {- return;- } // only capture once- const xDomain = isBandScale(xScale)+ }>(() => ({+ x: isBandScale(xScale)? null- : ((xScale as ScaleLinear<number, number>).domain() as [number, number]);- const yDomain = yScale.domain() as [number, number];- initialDomainsRef.current = {x: xDomain, y: yDomain};- }, [xScale, yScale]);+ : ((xScale as ScaleLinear<number, number>).domain() as [number, number]),+ y: yScale.domain() as [number, number],+ }));const dragRef = useRef<{startX: number;@@ -303,15 +296,11 @@ export function ChartZoom({}, []);const handleReset = useCallback(() => {- const init = initialDomainsRef.current;- if (!init) {- return;- }- if (init.x) {- onXDomainChange?.(init.x);+ if (initialDomains.x) {+ onXDomainChange?.(initialDomains.x);}- onYDomainChange?.(init.y);- }, [onXDomainChange, onYDomainChange]);+ onYDomainChange?.(initialDomains.y);+ }, [initialDomains, onXDomainChange, onYDomainChange]);// Toolbar portal targetconst portalTarget = svgRef.current?.parentElement;