From a9533935338553e932d84e8dc54e1e4f15405313 Mon Sep 17 00:00:00 2001 From: dangreen Date: Thu, 4 Nov 2021 14:59:24 +0700 Subject: [PATCH] refactor: functional data prop is removed Functional data prop is removed BREAKING CHANGE: Functional data prop is removed --- src/chart.tsx | 38 +++++++++++--------------------------- src/types.ts | 7 +------ test/chart.test.tsx | 20 -------------------- 3 files changed, 12 insertions(+), 53 deletions(-) diff --git a/src/chart.tsx b/src/chart.tsx index 612cf500f..6b8391b43 100644 --- a/src/chart.tsx +++ b/src/chart.tsx @@ -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 { @@ -12,10 +12,6 @@ import { setDatasets, } from './utils'; -const noopData = { - datasets: [], -}; - function ChartComponent< TType extends ChartType = ChartType, TData = DefaultDataPoint, @@ -26,7 +22,7 @@ function ChartComponent< width = 300, redraw = false, type, - data: dataProp, + data, options, plugins = [], getDatasetAtEvent, @@ -39,16 +35,9 @@ function ChartComponent< ref: ForwardedRef> ) { type TypedChartJS = ChartJS; - type TypedChartData = ChartData; const canvasRef = useRef(null); const chartRef = useRef(); - /** - * In case `dataProp` is function use internal state - */ - const [computedData, setComputedData] = useState(); - const data: TypedChartData = - computedData || (typeof dataProp === 'function' ? noopData : dataProp); const renderChart = () => { if (!canvasRef.current) return; @@ -81,7 +70,7 @@ function ChartComponent< if (!chart) return; - getDatasetAtEvent && + if (getDatasetAtEvent) { getDatasetAtEvent( chart.getElementsAtEventForMode( event.nativeEvent, @@ -91,7 +80,9 @@ function ChartComponent< ), event ); - getElementAtEvent && + } + + if (getElementAtEvent) { getElementAtEvent( chart.getElementsAtEventForMode( event.nativeEvent, @@ -101,7 +92,9 @@ function ChartComponent< ), event ); - getElementsAtEvent && + } + + if (getElementsAtEvent) { getElementsAtEvent( chart.getElementsAtEventForMode( event.nativeEvent, @@ -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) { diff --git a/src/types.ts b/src/types.ts index faa8212a4..b6badbc37 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,12 +20,7 @@ export interface ChartProps< TLabel = unknown > extends CanvasHTMLAttributes { type: TType; - /** - * @todo Remove function variant. - */ - data: - | ChartData - | ((canvas: HTMLCanvasElement) => ChartData); + data: ChartData; options?: ChartOptions; plugins?: Plugin[]; redraw?: boolean; diff --git a/test/chart.test.tsx b/test/chart.test.tsx index 89aa97200..7fb8254ae 100644 --- a/test/chart.test.tsx +++ b/test/chart.test.tsx @@ -65,26 +65,6 @@ describe('', () => { 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(); - - 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'],