Skip to content

Commit bf0538f

Browse files
authored
feat: assure redraw occurs when type changes (#1054) (#1055)
1 parent 9ce951c commit bf0538f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/chart.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ function ChartComponent<
8787
}
8888
}, [redraw, options, data.labels, data.datasets, updateMode]);
8989

90+
useEffect(() => {
91+
if (!chartRef.current) return;
92+
93+
destroyChart();
94+
setTimeout(renderChart);
95+
}, [type]);
96+
9097
useEffect(() => {
9198
renderChart();
9299

test/chart.test.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ describe('<Chart />', () => {
108108
expect(chart.id).toEqual(id);
109109
});
110110

111+
it('should properly update with a new chart type', () => {
112+
const newType = 'line';
113+
114+
const { rerender } = render(
115+
<Chart data={data} options={options} type='bar' ref={ref} />
116+
);
117+
118+
const originalChartDestroy = Object.assign({}, destroy);
119+
120+
rerender(<Chart data={data} options={options} type={newType} ref={ref} />);
121+
122+
expect(originalChartDestroy).toHaveBeenCalled();
123+
});
124+
111125
it('should properly maintain order with new data', () => {
112126
const oldData = {
113127
labels: ['red', 'blue'],

0 commit comments

Comments
 (0)