Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing feature for disabling plugins in TyeScript #11403

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/developers/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ declare module 'chart.js' {
interface PluginOptionsByType<TType extends ChartType> {
customCanvasBackgroundColor?: {
color?: string
}
} | false
}
}
```
14 changes: 7 additions & 7 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2913,13 +2913,13 @@ export interface TooltipItem<TType extends ChartType> {
}

export interface PluginOptionsByType<TType extends ChartType> {
colors: ColorsPluginOptions;
decimation: DecimationOptions;
filler: FillerOptions;
legend: LegendOptions<TType>;
subtitle: TitleOptions;
title: TitleOptions;
tooltip: TooltipOptions<TType>;
colors: ColorsPluginOptions | false;
decimation: DecimationOptions | false;
filler: FillerOptions | false;
legend: LegendOptions<TType> | false;
subtitle: TitleOptions | false;
title: TitleOptions | false;
tooltip: TooltipOptions<TType> | false;
}
export interface PluginChartOptions<TType extends ChartType> {
plugins: PluginOptionsByType<TType>;
Expand Down
6 changes: 3 additions & 3 deletions test/types/defaults.ts
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast is not needed now anymore right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, viceversa. It is now needed (or we need to use ts-ignore). With previous PR impl, having 2 interfaces and defaults was extending the interface without the possible false value) it wasn't needed. Now it's needed because TS can not recognize the type:

[types] test/types/defaults.ts(5,30): error TS2339: Property 'display' does not exist on type 'false | TitleOptions'.
[types]   Property 'display' does not exist on type 'false'.


Chart.defaults.datasets.bar.backgroundColor = 'red';

Expand All @@ -27,4 +27,4 @@ Chart.defaults.layout = {
},
};

Chart.defaults.plugins.tooltip.boxPadding = 3;
(Chart.defaults.plugins.tooltip as TooltipOptions).boxPadding = 3;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 changes: 2 additions & 2 deletions test/types/overrides.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
5 changes: 3 additions & 2 deletions test/types/plugins/defaults.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
16 changes: 16 additions & 0 deletions test/types/plugins/disable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Chart } from '../../../src/types.js';

const chart = new Chart('id', {
type: 'bubble',
data: {
labels: [],
datasets: [{
data: []
}]
},
options: {
plugins: {
legend: false
}
}
});
Original file line number Diff line number Diff line change
@@ -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})`;
};
Expand Down