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

refactor: functional data prop is removed #840

Merged
merged 1 commit into from
Nov 4, 2021
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
38 changes: 11 additions & 27 deletions src/chart.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useRef, useState, forwardRef } from 'react';
import React, { useEffect, useRef, forwardRef } from 'react';
import type { ForwardedRef, MouseEvent } from 'react';
import { Chart as ChartJS } from 'chart.js';
import type { ChartData, ChartType, DefaultDataPoint } from 'chart.js';
import type { ChartType, DefaultDataPoint } from 'chart.js';

import type { ChartProps, TypedChartComponent } from './types';
import {
Expand All @@ -12,10 +12,6 @@ import {
setDatasets,
} from './utils';

const noopData = {
datasets: [],
};

function ChartComponent<
TType extends ChartType = ChartType,
TData = DefaultDataPoint<TType>,
Expand All @@ -26,7 +22,7 @@ function ChartComponent<
width = 300,
redraw = false,
type,
data: dataProp,
data,
options,
plugins = [],
getDatasetAtEvent,
Expand All @@ -39,16 +35,9 @@ function ChartComponent<
ref: ForwardedRef<ChartJS<TType, TData, TLabel>>
) {
type TypedChartJS = ChartJS<TType, TData, TLabel>;
type TypedChartData = ChartData<TType, TData, TLabel>;

const canvasRef = useRef<HTMLCanvasElement>(null);
const chartRef = useRef<TypedChartJS | null>();
/**
* In case `dataProp` is function use internal state
*/
const [computedData, setComputedData] = useState<TypedChartData>();
const data: TypedChartData =
computedData || (typeof dataProp === 'function' ? noopData : dataProp);

const renderChart = () => {
if (!canvasRef.current) return;
Expand Down Expand Up @@ -81,7 +70,7 @@ function ChartComponent<

if (!chart) return;

getDatasetAtEvent &&
if (getDatasetAtEvent) {
getDatasetAtEvent(
chart.getElementsAtEventForMode(
event.nativeEvent,
Expand All @@ -91,7 +80,9 @@ function ChartComponent<
),
event
);
getElementAtEvent &&
}

if (getElementAtEvent) {
getElementAtEvent(
chart.getElementsAtEventForMode(
event.nativeEvent,
Expand All @@ -101,7 +92,9 @@ function ChartComponent<
),
event
);
getElementsAtEvent &&
}

if (getElementsAtEvent) {
getElementsAtEvent(
chart.getElementsAtEventForMode(
event.nativeEvent,
Expand All @@ -111,17 +104,8 @@ function ChartComponent<
),
event
);
};

/**
* In case `dataProp` is function,
* then update internal state
*/
useEffect(() => {
if (typeof dataProp === 'function' && canvasRef.current) {
setComputedData(dataProp(canvasRef.current));
}
}, [dataProp]);
};

useEffect(() => {
if (!redraw && chartRef.current && options) {
Expand Down
7 changes: 1 addition & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ export interface ChartProps<
TLabel = unknown
> extends CanvasHTMLAttributes<HTMLCanvasElement> {
type: TType;
/**
* @todo Remove function variant.
*/
data:
| ChartData<TType, TData, TLabel>
| ((canvas: HTMLCanvasElement) => ChartData<TType, TData, TLabel>);
data: ChartData<TType, TData, TLabel>;
options?: ChartOptions<TType>;
plugins?: Plugin<TType>[];
redraw?: boolean;
Expand Down
20 changes: 0 additions & 20 deletions test/chart.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,6 @@ describe('<Chart />', () => {
expect(chart.config.type).toEqual('bar');
});

it('should pass props onto chart if data is fn', () => {
const dataFn = jest.fn(c =>
c
? data
: {
datasets: [],
}
);

render(<Chart data={dataFn} options={options} type='bar' ref={ref} />);

expect(chart.config.data).toMatchObject(data);
expect(chart.config.options).toMatchObject(options);
expect(chart.config.type).toEqual('bar');

expect(dataFn).toHaveBeenCalledTimes(1);
expect(dataFn).toHaveBeenCalledWith(expect.any(HTMLCanvasElement));
expect(update).toHaveBeenCalledTimes(1);
});

it('should pass new data on data change', () => {
const newData = {
labels: ['red', 'blue'],
Expand Down