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

Give elements their own interface definition #8290

Merged
merged 1 commit into from
Jan 30, 2021
Merged
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
14 changes: 8 additions & 6 deletions types/index.esm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1834,14 +1834,16 @@ export const BarElement: ChartComponent & {
new (cfg: any): BarElement;
};

export interface ElementOptions {
Copy link
Member

Choose a reason for hiding this comment

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

When I read ElementOptions, I'm thinking of options for an element, and I would expect to be able to extend it for each element type (i.e. interface ArcOptions extends ElementOptions). Shouldn't this be named differently then, maybe ElementsOptions or ElementOptionsMap or something else that makes it clear it's a record of element type -> element options (ElementOptionsByType)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback.

I see your point. For what it's worth, the name ElementOptions would match the current PluginOptions (which seems to serve a similar purpose). Otherwise, I might pick ElementOptionsMap (seems clearest to my personal taste) or ElementsOptions (to be a bit more consistent with pre-existing type definitions' names).

I don't have a strong opinion, so I'll defer to others.

After taking another look at this, I realized that I could probably set up a ...Registry interface; this should serve the same purpose and would be similar to Chart.js's ScaleTypeRegistry and ChartTypeRegistry.

export interface ElementTypeRegistry {
        arc: ArcOptions & ArcHoverOptions;
        bar: BarOptions & BarHoverOptions;
        line: LineOptions & LineHoverOptions;
        point: PointOptions & PointHoverOptions;
 }
 export interface ElementChartOptions {
       elements: { [k in keyof ElementTypeRegistry]: ElementTypeRegistry[k]; };
 }

Or, using an options sub-object to match the other ...TypeRegistries: (This doesn't seem to be necessary for elements, but it's consistent and allows room for future growth.)

export interface ElementTypeRegistry {
	arc: {
		options: ArcOptions & ArcHoverOptions;
	}
	bar: {
		options: BarOptions & BarHoverOptions;
	}
	line: {
		options: LineOptions & LineHoverOptions;
	}
	point: {
		options: PointOptions & PointHoverOptions;
	}
}
export interface ElementChartOptions {
	elements: { [k in keyof ElementTypeRegistry]: ElementTypeRegistry[k]['options']; };
}

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

I agree, same problem with PluginOptions, ScaleOptions (and maybe some others). For this last, it's even more confusing because there is CoreScaleOptions. There is also CommonOptions which is too generic ("common" for what?) and should be renamed since it's not an interface that's supposed to be extend by all options set.

I would reserve these {Type}Options (i.e. PluginOptions, ScaleOptions, ElementOptions, etc.) for base interfaces, containing common properties for each type (if needed). We could prefix them to make explicit that these are "base" interfaces but I'm not sure I would pick Core (maybe Base instead).

I'm not convinced by ElementOptionsMap finally because it's not a JavaScript Map, so could be confusing. I'm not familiar with these *Registry types, but if it's a common pattern in our TD, then I guess it's a good one. Though, it would need to contain Options in the name (maybe ElementOptionsRegistry) because we can also have a registry for element (not element options). Then we should also rename PluginOptionsRegistry, ScaleOptionsRegistry, etc.

I kind like/prefer my last suggestion: ElementOptionsByType (PluginOptionsByType, ScaleOptionsByType) because it's clear that's a plain object containing a mapping between the type name and options. With Registry, it's a bit confusing because I understand "registry" as an object containing some logic / extra methods (i.e. ScaleOptionsRegistry.register()).

Finally, I would rename ElementChartOptions as well (or even better, remove it since I find it useless and making hard to understand the type hierarchy.

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer ElementTypeOptions over ElementOptionsByType. Not a strong opinion, just personal preference on naming conventions.

arc: ArcOptions & ArcHoverOptions;
bar: BarOptions & BarHoverOptions;
line: LineOptions & LineHoverOptions;
point: PointOptions & PointHoverOptions;
}
export interface ElementChartOptions {
elements: {
arc: ArcOptions & ArcHoverOptions;
bar: BarOptions & BarHoverOptions;
line: LineOptions & LineHoverOptions;
point: PointOptions & PointHoverOptions;
};
elements: ElementOptions;
}

export class BasePlatform {
/**
* Called at chart construction time, returns a context2d instance implementing
Expand Down