From 02aa2f187c8180d9eed4460672f6116db90f6d0f Mon Sep 17 00:00:00 2001 From: Jose C Quintas Jr Date: Thu, 6 Jun 2024 13:39:29 +0200 Subject: [PATCH] [charts] Simplify plugin types (#13396) Signed-off-by: Jose C Quintas Jr Signed-off-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> --- .../src/ChartContainer/ChartContainer.tsx | 6 +++--- .../src/ChartContainer/defaultPlugins.ts | 8 +++++++- .../src/ChartContainer/usePluginsMerge.ts | 4 ++-- packages/x-charts/src/models/plugin.ts | 20 +++++++++---------- scripts/x-charts.exports.json | 1 - 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/x-charts/src/ChartContainer/ChartContainer.tsx b/packages/x-charts/src/ChartContainer/ChartContainer.tsx index 0fcf2de946d6..812368b60a87 100644 --- a/packages/x-charts/src/ChartContainer/ChartContainer.tsx +++ b/packages/x-charts/src/ChartContainer/ChartContainer.tsx @@ -15,11 +15,11 @@ import { } from '../context/CartesianContextProvider'; import { ChartsAxesGradients } from '../internals/components/ChartsAxesGradients'; import { HighlightedProvider, HighlightedProviderProps } from '../context'; -import { ChartsPluginTypes } from '../models/plugin'; +import { ChartsPluginType } from '../models/plugin'; import { ChartSeriesType } from '../models/seriesType/config'; import { usePluginsMerge } from './usePluginsMerge'; -export type ChartContainerProps = Omit< +export type ChartContainerProps = Omit< ChartsSurfaceProps & Omit & Omit & @@ -32,7 +32,7 @@ export type ChartContainerProps = O * An array of plugins defining how to preprocess data. * If not provided, the container supports line, bar, scatter and pie charts. */ - plugins?: ChartsPluginTypes[]; + plugins?: ChartsPluginType[]; }; const ChartContainer = React.forwardRef(function ChartContainer(props: ChartContainerProps, ref) { diff --git a/packages/x-charts/src/ChartContainer/defaultPlugins.ts b/packages/x-charts/src/ChartContainer/defaultPlugins.ts index 8b648863eb51..c14585c0d6b5 100644 --- a/packages/x-charts/src/ChartContainer/defaultPlugins.ts +++ b/packages/x-charts/src/ChartContainer/defaultPlugins.ts @@ -2,5 +2,11 @@ import { plugin as barPlugin } from '../BarChart/plugin'; import { plugin as scatterPlugin } from '../ScatterChart/plugin'; import { plugin as linePlugin } from '../LineChart/plugin'; import { plugin as piePlugin } from '../PieChart/plugin'; +import { ChartsPluginType } from '../models'; -export const defaultPlugins = [barPlugin, scatterPlugin, linePlugin, piePlugin]; +export const defaultPlugins: ChartsPluginType<'bar' | 'scatter' | 'line' | 'pie'>[] = [ + barPlugin, + scatterPlugin, + linePlugin, + piePlugin, +]; diff --git a/packages/x-charts/src/ChartContainer/usePluginsMerge.ts b/packages/x-charts/src/ChartContainer/usePluginsMerge.ts index 82e4293dbbab..ee1043e918f9 100644 --- a/packages/x-charts/src/ChartContainer/usePluginsMerge.ts +++ b/packages/x-charts/src/ChartContainer/usePluginsMerge.ts @@ -1,11 +1,11 @@ import * as React from 'react'; -import { ChartsPluginTypes, ColorProcessorsConfig } from '../models'; +import { ChartsPluginType, ColorProcessorsConfig } from '../models'; import { ChartSeriesType } from '../models/seriesType/config'; import { ExtremumGettersConfig } from '../context/CartesianContextProvider'; import { SeriesFormatterConfig } from '../context/SeriesContextProvider'; import { defaultPlugins } from './defaultPlugins'; -export function usePluginsMerge(plugins?: ChartsPluginTypes[]) { +export function usePluginsMerge(plugins?: ChartsPluginType[]) { const defaultizedPlugins = plugins ?? defaultPlugins; return React.useMemo(() => { diff --git a/packages/x-charts/src/models/plugin.ts b/packages/x-charts/src/models/plugin.ts index 176f5abcc357..a8b4dd5287e5 100644 --- a/packages/x-charts/src/models/plugin.ts +++ b/packages/x-charts/src/models/plugin.ts @@ -14,14 +14,12 @@ export type ColorProcessorsConfig = { [Key in T]?: ColorProcessor; }; -export type ChartsPluginType = { - seriesType: T; - seriesFormatter: Formatter; - colorProcessor: ColorProcessor; - xExtremumGetter?: ExtremumGetter; - yExtremumGetter?: ExtremumGetter; -}; - -export type ChartsPluginTypes = { - [Key in T]: ChartsPluginType; -}[T]; +export type ChartsPluginType = T extends ChartSeriesType + ? { + seriesType: T; + seriesFormatter: Formatter; + colorProcessor: ColorProcessor; + xExtremumGetter?: ExtremumGetter; + yExtremumGetter?: ExtremumGetter; + } + : never; diff --git a/scripts/x-charts.exports.json b/scripts/x-charts.exports.json index 22e3d6404aef..916933cd7bb9 100644 --- a/scripts/x-charts.exports.json +++ b/scripts/x-charts.exports.json @@ -93,7 +93,6 @@ { "name": "ChartsOnAxisClickHandlerProps", "kind": "Interface" }, { "name": "ChartsPieSorting", "kind": "TypeAlias" }, { "name": "ChartsPluginType", "kind": "TypeAlias" }, - { "name": "ChartsPluginTypes", "kind": "TypeAlias" }, { "name": "ChartsReferenceLine", "kind": "Function" }, { "name": "ChartsReferenceLineClasses", "kind": "Interface" }, { "name": "ChartsReferenceLineClassKey", "kind": "TypeAlias" },