Skip to content

Commit

Permalink
fix(D3 plugin): fix onRender callback (#297)
Browse files Browse the repository at this point in the history
* fix(D3 plugin): call onRender callback

* fix onLoad
  • Loading branch information
kuzmadom authored Sep 20, 2023
1 parent 05ea715 commit daefbfd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {Meta, Story} from '@storybook/react';
import {action} from '@storybook/addon-actions';
import {Button} from '@gravity-ui/uikit';
import {settings} from '../../../../libs';
import {ChartKit} from '../../../../components/ChartKit';
Expand Down Expand Up @@ -42,8 +43,7 @@ const Template: Story = () => {

return (
<div style={{height: '300px', width: '100%'}}>
{/* @ts-ignore */}
<ChartKit ref={chartkitRef} type="d3" data={widgetData} />
<ChartKit ref={chartkitRef} type="d3" data={widgetData} onRender={action('onRender')} />
</div>
);
};
Expand Down
14 changes: 13 additions & 1 deletion src/plugins/d3/renderer/D3Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ type ChartDimensions = {

const D3Widget = React.forwardRef<ChartKitWidgetRef | undefined, ChartKitProps<'d3'>>(
function D3Widget(props, forwardedRef) {
const {data, onLoad, onRender} = props;
const ref = React.useRef<HTMLDivElement>(null);
const debounced = React.useRef<DebouncedFunc<() => void> | undefined>();
const [dimensions, setDimensions] = React.useState<Partial<ChartDimensions>>();

//FIXME: add chartPerfomance data to callbacks;
React.useLayoutEffect(() => {
if (onLoad) {
onLoad({});
}

if (onRender) {
onRender({});
}
}, []);

const handleResize = React.useCallback(() => {
const parentElement = ref.current?.parentElement;

Expand Down Expand Up @@ -78,7 +90,7 @@ const D3Widget = React.forwardRef<ChartKitWidgetRef | undefined, ChartKitProps<'
left={dimensions.left || 0}
width={dimensions.width}
height={dimensions.height}
data={props.data}
data={data}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {Meta, Story} from '@storybook/react';
import {action} from '@storybook/addon-actions';
import {Button} from '@gravity-ui/uikit';
import {settings} from '../../../../libs';
import {ChartKit} from '../../../../components/ChartKit';
Expand Down Expand Up @@ -44,7 +45,12 @@ const Template: Story = () => {

return (
<div style={{height: '300px', width: '100%'}}>
<ChartKit ref={chartkitRef} type="highcharts" data={widgetData} />
<ChartKit
ref={chartkitRef}
type="highcharts"
data={widgetData}
onLoad={action('onLoad')}
/>
</div>
);
};
Expand Down
9 changes: 8 additions & 1 deletion src/plugins/indicator/__stories__/Indicator.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {Meta, Story} from '@storybook/react';
import {action} from '@storybook/addon-actions';
import {withKnobs, boolean, color as colorKnob, radios, text} from '@storybook/addon-knobs';
import {cloneDeep} from 'lodash';
import {Button} from '@gravity-ui/uikit';
Expand Down Expand Up @@ -48,7 +49,13 @@ const Template: Story = () => {

return (
<div style={{height: 300, width: '100%'}}>
<ChartKit ref={chartkitRef} id="1" type="indicator" data={resultData} />
<ChartKit
ref={chartkitRef}
id="1"
type="indicator"
data={resultData}
onLoad={action('onLoad')}
/>
</div>
);
};
Expand Down

0 comments on commit daefbfd

Please sign in to comment.