From 9432030b65661bac26dccbaf354542e9c2d1b5d8 Mon Sep 17 00:00:00 2001 From: stockiNail Date: Thu, 13 Jul 2023 09:59:01 +0200 Subject: [PATCH 1/3] Add missing feature for disabling plugins in TyeScript --- docs/developers/plugins.md | 9 ++++++++- src/types/index.d.ts | 23 +++++++++++++++++++---- test/types/plugins/disable.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 test/types/plugins/disable.ts diff --git a/docs/developers/plugins.md b/docs/developers/plugins.md index 60747416b91..7227964502a 100644 --- a/docs/developers/plugins.md +++ b/docs/developers/plugins.md @@ -194,10 +194,17 @@ For example, to provide typings for the [`canvas backgroundColor plugin`](../con import {ChartType, Plugin} from 'chart.js'; declare module 'chart.js' { - interface PluginOptionsByType { + // For defaults options + interface DefaultsPluginOptionsByType { customCanvasBackgroundColor?: { color?: string } } + // for chart options + interface PluginOptionsByType { + customCanvasBackgroundColor?: { + color?: string + } | false + } } ``` diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 83ad302e30a..7c28892a399 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -663,7 +663,7 @@ export interface DatasetControllerChartComponent extends ChartComponent { }; } -export interface Defaults extends CoreChartOptions, ElementChartOptions, PluginChartOptions { +export interface Defaults extends CoreChartOptions, ElementChartOptions, DefaultsPluginChartOptions { scale: ScaleOptionsByType; scales: { @@ -701,7 +701,7 @@ export type Overrides = { [key in ChartType]: CoreChartOptions & ElementChartOptions & - PluginChartOptions & + DefaultsPluginChartOptions & DatasetChartOptions & ScaleChartOptions & ChartTypeRegistry[key]['chartOptions']; @@ -2912,7 +2912,7 @@ export interface TooltipItem { element: Element; } -export interface PluginOptionsByType { +export interface DefaultsPluginOptionsByType { colors: ColorsPluginOptions; decimation: DecimationOptions; filler: FillerOptions; @@ -2921,8 +2921,23 @@ export interface PluginOptionsByType { title: TitleOptions; tooltip: TooltipOptions; } + +export interface PluginOptionsByType { + colors: ColorsPluginOptions | false; + decimation: DecimationOptions | false; + filler: FillerOptions | false; + legend: LegendOptions | false; + subtitle: TitleOptions | false; + title: TitleOptions | false; + tooltip: TooltipOptions | false; +} + +export interface DefaultsPluginChartOptions { + plugins: DefaultsPluginOptionsByType; +} + export interface PluginChartOptions { - plugins: PluginOptionsByType; + plugins: PluginOptionsByType | false; } export interface BorderOptions { diff --git a/test/types/plugins/disable.ts b/test/types/plugins/disable.ts new file mode 100644 index 00000000000..a1281674329 --- /dev/null +++ b/test/types/plugins/disable.ts @@ -0,0 +1,29 @@ +import { Chart } from '../../../src/types.js'; + +const chart = new Chart('id', { + type: 'bubble', + data: { + labels: [], + datasets: [{ + data: [] + }] + }, + options: { + plugins: false + } +}); + +const chart1 = new Chart('id', { + type: 'bubble', + data: { + labels: [], + datasets: [{ + data: [] + }] + }, + options: { + plugins: { + legend: false + } + } +}); \ No newline at end of file From 920ee64ad09e78c5190360767810af72da5841e0 Mon Sep 17 00:00:00 2001 From: stockiNail Date: Fri, 14 Jul 2023 13:22:22 +0200 Subject: [PATCH 2/3] apply review --- docs/developers/plugins.md | 7 ------- src/types/index.d.ts | 20 +++---------------- test/types/defaults.ts | 6 +++--- test/types/overrides.ts | 4 ++-- test/types/plugins/defaults.ts | 5 +++-- test/types/plugins/disable.ts | 13 ------------ .../tooltip_parsed_data_chart_defaults.ts | 4 ++-- 7 files changed, 13 insertions(+), 46 deletions(-) diff --git a/docs/developers/plugins.md b/docs/developers/plugins.md index 7227964502a..4468a58f52e 100644 --- a/docs/developers/plugins.md +++ b/docs/developers/plugins.md @@ -194,13 +194,6 @@ For example, to provide typings for the [`canvas backgroundColor plugin`](../con import {ChartType, Plugin} from 'chart.js'; declare module 'chart.js' { - // For defaults options - interface DefaultsPluginOptionsByType { - customCanvasBackgroundColor?: { - color?: string - } - } - // for chart options interface PluginOptionsByType { customCanvasBackgroundColor?: { color?: string diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 7c28892a399..a78e15ce992 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -663,7 +663,7 @@ export interface DatasetControllerChartComponent extends ChartComponent { }; } -export interface Defaults extends CoreChartOptions, ElementChartOptions, DefaultsPluginChartOptions { +export interface Defaults extends CoreChartOptions, ElementChartOptions, PluginChartOptions { scale: ScaleOptionsByType; scales: { @@ -701,7 +701,7 @@ export type Overrides = { [key in ChartType]: CoreChartOptions & ElementChartOptions & - DefaultsPluginChartOptions & + PluginChartOptions & DatasetChartOptions & ScaleChartOptions & ChartTypeRegistry[key]['chartOptions']; @@ -2912,16 +2912,6 @@ export interface TooltipItem { element: Element; } -export interface DefaultsPluginOptionsByType { - colors: ColorsPluginOptions; - decimation: DecimationOptions; - filler: FillerOptions; - legend: LegendOptions; - subtitle: TitleOptions; - title: TitleOptions; - tooltip: TooltipOptions; -} - export interface PluginOptionsByType { colors: ColorsPluginOptions | false; decimation: DecimationOptions | false; @@ -2932,12 +2922,8 @@ export interface PluginOptionsByType { tooltip: TooltipOptions | false; } -export interface DefaultsPluginChartOptions { - plugins: DefaultsPluginOptionsByType; -} - export interface PluginChartOptions { - plugins: PluginOptionsByType | false; + plugins: PluginOptionsByType; } export interface BorderOptions { diff --git a/test/types/defaults.ts b/test/types/defaults.ts index 84071849245..d20415df1ca 100644 --- a/test/types/defaults.ts +++ b/test/types/defaults.ts @@ -1,8 +1,8 @@ -import { Chart } from '../../src/types.js'; +import { Chart, TitleOptions, TooltipOptions } from '../../src/types.js'; Chart.defaults.scales.time.time.minUnit = 'day'; -Chart.defaults.plugins.title.display = false; +(Chart.defaults.plugins.title as TitleOptions).display = false; Chart.defaults.datasets.bar.backgroundColor = 'red'; @@ -27,4 +27,4 @@ Chart.defaults.layout = { }, }; -Chart.defaults.plugins.tooltip.boxPadding = 3; +(Chart.defaults.plugins.tooltip as TooltipOptions).boxPadding = 3; diff --git a/test/types/overrides.ts b/test/types/overrides.ts index b4da296a322..9085dd98356 100644 --- a/test/types/overrides.ts +++ b/test/types/overrides.ts @@ -1,8 +1,8 @@ -import { Chart } from '../../src/types.js'; +import { Chart, TitleOptions } from '../../src/types.js'; Chart.overrides.bar.scales.x.type = 'time'; -Chart.overrides.bar.plugins.title.display = false; +(Chart.overrides.bar.plugins.title as TitleOptions).display = false; Chart.overrides.line.datasets.bar.backgroundColor = 'red'; diff --git a/test/types/plugins/defaults.ts b/test/types/plugins/defaults.ts index 55a08ac7ad8..a11c4dce09a 100644 --- a/test/types/plugins/defaults.ts +++ b/test/types/plugins/defaults.ts @@ -1,8 +1,9 @@ -import { defaults } from '../../../src/types.js'; +import { defaults, LegendOptions } from '../../../src/types.js'; // https://github.com/chartjs/Chart.js/issues/8711 -const original = defaults.plugins.legend.labels.generateLabels; +const original = (defaults.plugins.legend as LegendOptions<"line">).labels.generateLabels; +// @ts-ignore defaults.plugins.legend.labels.generateLabels = function(chart) { return [{ datasetIndex: 0, diff --git a/test/types/plugins/disable.ts b/test/types/plugins/disable.ts index a1281674329..d6ed066e192 100644 --- a/test/types/plugins/disable.ts +++ b/test/types/plugins/disable.ts @@ -1,19 +1,6 @@ import { Chart } from '../../../src/types.js'; const chart = new Chart('id', { - type: 'bubble', - data: { - labels: [], - datasets: [{ - data: [] - }] - }, - options: { - plugins: false - } -}); - -const chart1 = new Chart('id', { type: 'bubble', data: { labels: [], diff --git a/test/types/plugins/plugin.tooltip/tooltip_parsed_data_chart_defaults.ts b/test/types/plugins/plugin.tooltip/tooltip_parsed_data_chart_defaults.ts index 5072824bcf9..b5584adeb43 100644 --- a/test/types/plugins/plugin.tooltip/tooltip_parsed_data_chart_defaults.ts +++ b/test/types/plugins/plugin.tooltip/tooltip_parsed_data_chart_defaults.ts @@ -1,6 +1,6 @@ -import { Chart } from '../../../../src/types.js'; +import { Chart, TooltipOptions } from '../../../../src/types.js'; -Chart.overrides.bubble.plugins.tooltip.callbacks.label = (item) => { +(Chart.overrides.bubble.plugins.tooltip as TooltipOptions<'bubble'>).callbacks.label = (item) => { const { x, y, _custom: r } = item.parsed; return `${item.label}: (${x}, ${y}, ${r})`; }; From 3cdec3a9d36138ac2318a57322e76a7e2be1836c Mon Sep 17 00:00:00 2001 From: stockiNail Date: Fri, 14 Jul 2023 13:30:19 +0200 Subject: [PATCH 3/3] remove empty line --- src/types/index.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/types/index.d.ts b/src/types/index.d.ts index a78e15ce992..65e1213dcd7 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -2921,7 +2921,6 @@ export interface PluginOptionsByType { title: TitleOptions | false; tooltip: TooltipOptions | false; } - export interface PluginChartOptions { plugins: PluginOptionsByType; }