Skip to content

Commit 7ef2edd

Browse files
alexfauquetteA-s-h-o-k
authored andcommitted
[charts] Fix zoom option reactivity (mui#16262)
1 parent 0bd6ffc commit 7ef2edd

File tree

7 files changed

+36
-30
lines changed

7 files changed

+36
-30
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { AxisId, DefaultizedZoomOptions } from '@mui/x-charts/internals';
2+
import { AxisConfig, ChartsXAxisProps, ChartsYAxisProps, ScaleName } from '@mui/x-charts/models';
3+
import { defaultizeZoom } from './defaultizeZoom';
4+
5+
export const creatZoomLookup = (
6+
axes: AxisConfig<ScaleName, any, ChartsXAxisProps | ChartsYAxisProps>[],
7+
) =>
8+
axes.reduce<Record<AxisId, DefaultizedZoomOptions>>((acc, v) => {
9+
const { zoom, id: axisId } = v;
10+
const defaultizedZoom = defaultizeZoom(zoom, axisId, 'x');
11+
if (defaultizedZoom) {
12+
acc[axisId] = defaultizedZoom;
13+
}
14+
return acc;
15+
}, {});

packages/x-charts-pro/src/internals/plugins/useChartProZoom/useChartProZoom.selectors.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
import { ChartRootSelector, createSelector } from '@mui/x-charts/internals';
1+
import {
2+
ChartRootSelector,
3+
createSelector,
4+
selectorChartRawXAxis,
5+
selectorChartRawYAxis,
6+
} from '@mui/x-charts/internals';
27
import { UseChartProZoomSignature } from './useChartProZoom.types';
8+
import { creatZoomLookup } from './creatZoomLookup';
39

410
export const selectorChartZoomState: ChartRootSelector<UseChartProZoomSignature> = (state) =>
511
state.zoom;
612

13+
const selectorChartXZoomOptionsLookup = createSelector(selectorChartRawXAxis, creatZoomLookup);
14+
15+
const selectorChartYZoomOptionsLookup = createSelector(selectorChartRawYAxis, creatZoomLookup);
16+
717
export const selectorChartZoomOptionsLookup = createSelector(
8-
selectorChartZoomState,
9-
(zoom) => zoom.optionsLookup,
18+
[selectorChartXZoomOptionsLookup, selectorChartYZoomOptionsLookup],
19+
(xLookup, yLookup) => ({ ...xLookup, ...yLookup }),
1020
);
1121

1222
export const selectorChartZoomData = createSelector(

packages/x-charts-pro/src/internals/plugins/useChartProZoom/useChartProZoom.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
ZoomData,
1111
} from '@mui/x-charts/internals';
1212
import { UseChartProZoomSignature } from './useChartProZoom.types';
13-
import { defaultizeZoom } from './defaultizeZoom';
13+
import { creatZoomLookup } from './creatZoomLookup';
1414
import {
1515
getDiff,
1616
getHorizontalCenterRatio,
@@ -80,6 +80,7 @@ export const useChartProZoom: ChartPlugin<UseChartProZoomSignature> = ({
8080
() => Object.values(optionsLookup).some((v) => v.panning) || false,
8181
[optionsLookup],
8282
);
83+
8384
const isDraggingRef = React.useRef(false);
8485
const touchStartRef = React.useRef<{ x: number; y: number; zoomData: ZoomData[] } | null>(null);
8586
React.useEffect(() => {
@@ -372,22 +373,8 @@ useChartProZoom.params = {
372373

373374
useChartProZoom.getDefaultizedParams = ({ params }) => {
374375
const optionsLookup = {
375-
...params.defaultizedXAxis.reduce<Record<AxisId, DefaultizedZoomOptions>>((acc, v) => {
376-
const { zoom, id: axisId } = v;
377-
const defaultizedZoom = defaultizeZoom(zoom, axisId, 'x');
378-
if (defaultizedZoom) {
379-
acc[axisId] = defaultizedZoom;
380-
}
381-
return acc;
382-
}, {}),
383-
...params.defaultizedYAxis.reduce<Record<AxisId, DefaultizedZoomOptions>>((acc, v) => {
384-
const { zoom, id: axisId } = v;
385-
const defaultizedZoom = defaultizeZoom(zoom, axisId, 'y');
386-
if (defaultizedZoom) {
387-
acc[axisId] = defaultizedZoom;
388-
}
389-
return acc;
390-
}, {}),
376+
...creatZoomLookup(params.defaultizedXAxis),
377+
...creatZoomLookup(params.defaultizedYAxis),
391378
};
392379

393380
return {
@@ -399,7 +386,6 @@ useChartProZoom.getDefaultizedParams = ({ params }) => {
399386
useChartProZoom.getInitialState = (params) => {
400387
return {
401388
zoom: {
402-
optionsLookup: params.optionsLookup,
403389
zoomData:
404390
params.initialZoom === undefined
405391
? initializeZoomData(params.optionsLookup)

packages/x-charts-pro/src/internals/plugins/useChartProZoom/useChartProZoom.types.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ export type UseChartProZoomDefaultizedParameters = UseChartProZoomParameters &
3131

3232
export interface UseChartProZoomState {
3333
zoom: {
34-
/**
35-
* The zoom options for each axis.
36-
*/
37-
optionsLookup: Record<AxisId, DefaultizedZoomOptions>;
3834
/**
3935
* Whether the user is currently interacting with the chart.
4036
* This is useful to disable animations while the user is interacting.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { useChartCartesianAxis } from './useChartCartesianAxis';
22
export type * from './useChartCartesianAxis.types';
3+
export * from './useChartCartesianAxis.selectors';
34
export { defaultizeAxis } from './defaultizeAxis';
45
export * from './computeAxisValue';
56
export * from './zoom.types';

packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/useChartCartesianAxis.selectors.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ const selectorChartZoomMap = createSelector(
4444
(zoom) => zoom?.zoomData && createZoomMap(zoom?.zoomData),
4545
);
4646

47-
const selectorChartZoomOptionsLookup = createSelector(
48-
selectorChartZoomState,
49-
(zoom) => zoom?.optionsLookup,
50-
);
47+
const selectorChartZoomOptionsLookup = createSelector(selectorChartZoomState, () => undefined);
5148

5249
const selectorChartXFilter = createSelector(
5350
[
@@ -96,6 +93,7 @@ const selectorChartZoomAxisFilters = createSelector(
9693
}
9794

9895
const xFilters = xAxis.reduce<ZoomAxisFilters>((acc, axis, index) => {
96+
// @ts-expect-error The type is defined in the pro package
9997
const filter = xMapper(axis, index);
10098
if (filter !== null) {
10199
acc[axis.id] = filter;
@@ -104,6 +102,7 @@ const selectorChartZoomAxisFilters = createSelector(
104102
}, {});
105103

106104
const yFilters = yAxis.reduce<ZoomAxisFilters>((acc, axis, index) => {
105+
// @ts-expect-error The type is defined in the pro package
107106
const filter = yMapper(axis, index);
108107
if (filter !== null) {
109108
acc[axis.id] = filter;

packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/useChartCartesianAxis.types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ export interface UseChartCartesianAxisState {
6666
* @ignore - state populated by the useChartProZoomPlugin
6767
*/
6868
zoom?: {
69-
optionsLookup: Record<AxisId, DefaultizedZoomOptions>;
7069
isInteracting: boolean;
7170
zoomData: ZoomData[];
7271
};

0 commit comments

Comments
 (0)