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

feat: add onRender & onChartLoad calbacks #111

Merged
merged 5 commits into from
Jan 23, 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
11 changes: 2 additions & 9 deletions src/components/ChartKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ChartKitComponentProps<T extends ChartKitType> = Omit<ChartKitProps<T>, 'on

const ChartKitComponent = <T extends ChartKitType>(props: ChartKitComponentProps<T>) => {
const widgetRef = React.useRef<ChartKitWidgetRef>();
const {instanceRef, id: propsId, type, data, onLoad, isMobile, ...restProps} = props;
const {instanceRef, id: propsId, type, isMobile, ...restProps} = props;

const ckId = React.useMemo(() => getRandomCKId(), []);
const id = propsId || ckId;
Expand Down Expand Up @@ -50,14 +50,7 @@ const ChartKitComponent = <T extends ChartKitType>(props: ChartKitComponentProps
return (
<React.Suspense fallback={<Loader />}>
<div className={b({mobile: isMobile}, 'chartkit-theme_common')}>
<ChartComponent
ref={widgetRef}
id={id}
lang={lang}
data={data}
onLoad={onLoad}
{...restProps}
/>
<ChartComponent ref={widgetRef} id={id} lang={lang} {...restProps} />
</div>
</React.Suspense>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,35 @@ export class HighchartsComponent extends React.PureComponent<Props, State> {
}>();

componentDidMount() {
this.onLoad();
if (!this.props.onChartLoad) {
this.onLoad();
return;
}

const needCallbacks = !this.state.isError && !this.props.splitTooltip;
if (!needCallbacks) {
return;
}
const widget = this.chartComponent.current ? this.chartComponent.current.chart : null;

if (this.state.callback && widget) {
this.state.callback(widget);
}

this.props.onChartLoad?.({
widget,
});
}

componentDidUpdate() {
const needRenderCallback =
this.props.onRender && !this.state.isError && !this.props.splitTooltip;
if (needRenderCallback) {
this.props.onRender?.({
renderTime: getChartPerformanceDuration(this.getId()),
});
return;
}
this.onLoad();
}

Expand Down
1 change: 1 addition & 0 deletions src/plugins/indicator/renderer/IndicatorWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const IndicatorWidget = React.forwardRef<ChartKitWidgetRef | undefined, ChartKit
React.useLayoutEffect(() => {
// TODO: swap to onRender after https://github.com/gravity-ui/chartkit/issues/33
onLoad?.();
// TODO: add onRender with renderTime Issue #114
});

if (isEmpty(data)) {
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/yagr/renderer/YagrWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const YagrWidget = React.forwardRef<ChartKitWidgetRef | undefined, Props>((props
id,
data: {data},
onLoad,
onRender,
onChartLoad,
} = props;
const yagrRef = React.useRef<YagrComponent>(null);

Expand All @@ -34,6 +36,7 @@ const YagrWidget = React.forwardRef<ChartKitWidgetRef | undefined, Props>((props
const handleChartLoading: NonNullable<YagrChartProps['onChartLoad']> = React.useCallback(
(chart, {renderTime}) => {
onLoad?.({...data, widget: chart, widgetRendering: renderTime});
onRender?.({renderTime});
},
[onLoad, data],
);
Expand Down Expand Up @@ -95,6 +98,10 @@ const YagrWidget = React.forwardRef<ChartKitWidgetRef | undefined, Props>((props
});
});

React.useLayoutEffect(() => {
onChartLoad?.({widget: yagrRef.current?.chart});
}, []);

return (
<YagrComponent
ref={yagrRef}
Expand Down
25 changes: 25 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react';

import type {ChartKitWidget} from './widget';

export type {ChartKitHolidays} from './misc';
Expand All @@ -19,14 +21,37 @@ export type ChartKitOnLoadData<T extends ChartKitType> = {
widgetRendering?: number;
};

export type ChartKitOnRenderData = {
renderTime?: number;
};

export type ChartKitOnChartLoad<T extends ChartKitType> = {
widget?: ChartKitWidget[T]['widget'] | null;
};

export type ChartKitOnError = (data: {error: any}) => void;

export type ChartKitProps<T extends ChartKitType> = {
type: T;
data: ChartKitWidget[T]['data'];
id?: string;
isMobile?: boolean;
/**
* @depricated please use onRender & onChartLoad instead
* @param data
*/
onLoad?: (data?: ChartKitOnLoadData<T>) => void;
/**
* called on each render
* @param data
*/
onRender?: (data: ChartKitOnRenderData) => void;
/**
* called on chart mount
* @param data
*/
onChartLoad?: (data: ChartKitOnChartLoad<T>) => void;

onError?: ChartKitOnError;
} & {[key in keyof Omit<ChartKitWidget[T], 'data' | 'widget'>]: ChartKitWidget[T][key]};

Expand Down