From 87966c1d6801396f3fa983db29d4d4342922d633 Mon Sep 17 00:00:00 2001 From: Jose Quintas Date: Thu, 6 Jun 2024 11:18:12 +0200 Subject: [PATCH 1/4] Simpler plugin types --- .../src/ChartContainer/ChartContainer.tsx | 6 +++--- .../src/ChartContainer/defaultPlugins.ts | 9 ++++++++- .../src/ChartContainer/usePluginsMerge.ts | 4 ++-- packages/x-charts/src/models/plugin.ts | 20 +++++++++---------- 4 files changed, 22 insertions(+), 17 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..0b5de2a99bf3 100644 --- a/packages/x-charts/src/ChartContainer/defaultPlugins.ts +++ b/packages/x-charts/src/ChartContainer/defaultPlugins.ts @@ -2,5 +2,12 @@ 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 { ChartSeriesType } from '../models/seriesType/config'; +import { ChartsPluginType } from '../models'; -export const defaultPlugins = [barPlugin, scatterPlugin, linePlugin, piePlugin]; +export const defaultPlugins: ChartsPluginType[] = [ + 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; From fcab82481d217e5f8ebde7eebd4cfb52ec09c4e4 Mon Sep 17 00:00:00 2001 From: Jose Quintas Date: Thu, 6 Jun 2024 12:24:34 +0200 Subject: [PATCH 2/4] Remove unused ChartsPluginTypes from x-charts.exports.json --- scripts/x-charts.exports.json | 1 - 1 file changed, 1 deletion(-) 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" }, From b43e1e6b2ffa50c313da24a3a53eb2df1feebb24 Mon Sep 17 00:00:00 2001 From: Jose C Quintas Jr Date: Thu, 6 Jun 2024 12:44:03 +0200 Subject: [PATCH 3/4] Update packages/x-charts/src/ChartContainer/defaultPlugins.ts Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr --- packages/x-charts/src/ChartContainer/defaultPlugins.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/x-charts/src/ChartContainer/defaultPlugins.ts b/packages/x-charts/src/ChartContainer/defaultPlugins.ts index 0b5de2a99bf3..1a275e31cbf6 100644 --- a/packages/x-charts/src/ChartContainer/defaultPlugins.ts +++ b/packages/x-charts/src/ChartContainer/defaultPlugins.ts @@ -5,7 +5,7 @@ import { plugin as piePlugin } from '../PieChart/plugin'; import { ChartSeriesType } from '../models/seriesType/config'; import { ChartsPluginType } from '../models'; -export const defaultPlugins: ChartsPluginType[] = [ +export const defaultPlugins: ChartsPluginType<'bar' | 'scatter' | 'line' | 'pie'>[] = [ barPlugin, scatterPlugin, linePlugin, From 4c3eda46b6183f318b37550122570d73ac0e5356 Mon Sep 17 00:00:00 2001 From: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Date: Thu, 6 Jun 2024 13:21:07 +0200 Subject: [PATCH 4/4] Update packages/x-charts/src/ChartContainer/defaultPlugins.ts Signed-off-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> --- packages/x-charts/src/ChartContainer/defaultPlugins.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/x-charts/src/ChartContainer/defaultPlugins.ts b/packages/x-charts/src/ChartContainer/defaultPlugins.ts index 1a275e31cbf6..c14585c0d6b5 100644 --- a/packages/x-charts/src/ChartContainer/defaultPlugins.ts +++ b/packages/x-charts/src/ChartContainer/defaultPlugins.ts @@ -2,7 +2,6 @@ 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 { ChartSeriesType } from '../models/seriesType/config'; import { ChartsPluginType } from '../models'; export const defaultPlugins: ChartsPluginType<'bar' | 'scatter' | 'line' | 'pie'>[] = [