feat(heatmap): move onBrushEnd from config to Settings#1369
feat(heatmap): move onBrushEnd from config to Settings#1369rshen91 merged 9 commits intoelastic:masterfrom
Conversation
7148acf to
604b153
Compare
| export type BrushArea = XYBrushArea | HeatmapBrushEvent; | ||
|
|
||
| /** @public */ | ||
| export type BrushEndListener = (brushArea: XYBrushArea) => void; | ||
| export type BrushEndListener = (brushArea: BrushArea) => void; |
There was a problem hiding this comment.
@nickofthyme @monfera @rshen91
I'd like to have your opinion here: this is not the only place where we have an union type as an argument of a chart callback.
It happens on the onClick event, but also in other situations.
Most chart consumers automatically cast to the right type depending on their needs: e.g. I'm using a bar chart so I'm cast with as XYBrushArea the brushArea.
Right now we don't offer a Generic type for the chart and I also don't fully know if and how this can work.
I'm wondering if we can, in the meantime, find a simpler and cleaner solution to the problem.
Tagging the callback argument type with the chart type could be a solution:
type HeatmapBrushEvent = {
type: 'heatmap';
cells: Cell[];
x: (string | number)[];
y: (string | number)[];
};
interface XYBrushArea {
type: 'cartesian';
x?: [number, number];
y?: Array<GroupBrushExtent>;
}what do you think?
There was a problem hiding this comment.
So I was thinking one way to do this would be in the function somehow like this...
<Settings
onBrushEnd={('heatmap', event: HeatmapBrushEvent) => null}
// or with multiple
onBrushEnd={(['line', 'area'], event: LineBrushEvent | AreaBrushEvent) => null}
/>The issue with this is that they could pass 'heatmap when there is no HeatmapSpec defined.
<Settings
onBrushEnd={{
types: ['heatmap'],
callback: (event: HeatmapBrushEvent) => null
}}
/>The idea behind the types is something like below... (see TS Playground)
Screen.Recording.2021-09-14.at.08.38.04.AM.mp4
There was a problem hiding this comment.
I found a slightly simpler way to avoid the type recursion. See demo
interface HeatmapEvent {
type: "heatmap"
getHeatmap: () => {}
}
interface XYEvent {
type: "xy"
getXY: () => {}
}
interface EventMap {
'heatmap': HeatmapEvent;
'xy': XYEvent;
}
// There may be a less verbose way of doing this...
type Callback<Type> = Type extends keyof EventMap ?
EventMap[Type] : never
type HeatmapCb = Callback<'heatmap'>; // HeatmapEvent
type XYCb = Callback<'xy'>; // XYEvent
type AnyCb = Callback<'xy'|'heatmap'>; // HeatmapEvent | XYEvent| export type BrushArea = XYBrushArea | HeatmapBrushEvent; | ||
|
|
||
| /** @public */ | ||
| export type BrushEndListener = (brushArea: XYBrushArea) => void; | ||
| export type BrushEndListener = (brushArea: BrushArea) => void; |
There was a problem hiding this comment.
So I was thinking one way to do this would be in the function somehow like this...
<Settings
onBrushEnd={('heatmap', event: HeatmapBrushEvent) => null}
// or with multiple
onBrushEnd={(['line', 'area'], event: LineBrushEvent | AreaBrushEvent) => null}
/>The issue with this is that they could pass 'heatmap when there is no HeatmapSpec defined.
<Settings
onBrushEnd={{
types: ['heatmap'],
callback: (event: HeatmapBrushEvent) => null
}}
/>The idea behind the types is something like below... (see TS Playground)
Screen.Recording.2021-09-14.at.08.38.04.AM.mp4
db968f6 to
bd1c02d
Compare
CHANGELOG.md
Outdated
There was a problem hiding this comment.
Ugh sorry for got about git checkout would also wipe any auto changes in between. I just git reset to the good pr with the unit test fix on top 🍒 and force pushed 🙊
|
jenkins test this please |
BREAKING CHANGE: remove onBrushEnd from heatmap config and merge onBrushEnd in Settings with cartesian onBrushEnd
# [36.0.0](v35.0.0...v36.0.0) (2021-09-15) ### Features * **heatmap:** move onBrushEnd from config to Settings ([#1369](#1369)) ([409a0c4](409a0c4)) ### BREAKING CHANGES * **heatmap:** remove onBrushEnd from heatmap config and merge onBrushEnd in Settings with cartesian onBrushEnd
Summary
BREAKING CHANGE
The
onBrushEndprop is removed from the heatmap config. It can now be pulled off the Setting to match the other chart types.Details
In
settings.tsxI created a new typeBrushArea:This satisfies
chart_state.interactions.test.tsxandon_brushed_highlighted_shapes.test.tsIssues
Closes #1214
Checklist
:xy,:partition)closes #123,fixes #123)packages/charts/src/index.ts