@@ -6,35 +6,43 @@ import React, {
66 useMemo ,
77 forwardRef ,
88} from 'react' ;
9- import Chart from 'chart.js/auto' ;
10- import type { ChartData } from 'chart.js' ;
9+ import type { Ref , MouseEvent } from 'react' ;
10+ import ChartJS from 'chart.js/auto' ;
11+ import type { ChartData , ChartType , DefaultDataPoint } from 'chart.js' ;
1112import merge from 'lodash/merge' ;
1213import assign from 'lodash/assign' ;
1314import find from 'lodash/find' ;
1415
15- import { Props } from './types' ;
16+ import { Props , ChartJSOrUndefined , TypedChartComponent } from './types' ;
1617
17- const ChartComponent = forwardRef < Chart | undefined , Props > ( ( props , ref ) => {
18- const {
19- id,
20- className,
18+ function ChartComponent <
19+ TType extends ChartType = ChartType ,
20+ TData = DefaultDataPoint < TType > ,
21+ TLabel = unknown
22+ > (
23+ {
2124 height = 150 ,
2225 width = 300 ,
2326 redraw = false ,
2427 type,
2528 data,
26- options = { } ,
29+ options,
2730 plugins = [ ] ,
2831 getDatasetAtEvent,
2932 getElementAtEvent,
3033 getElementsAtEvent,
3134 fallbackContent,
32- ...rest
33- } = props ;
35+ onClick : onClickProp ,
36+ ...props
37+ } : Props < TType , TData , TLabel > ,
38+ ref : Ref < ChartJS < TType , TData , TLabel > >
39+ ) {
40+ type TypedChartJS = ChartJSOrUndefined < TType , TData , TLabel > ;
41+ type TypedChartData = ChartData < TType , TData , TLabel > ;
3442
3543 const canvas = useRef < HTMLCanvasElement > ( null ) ;
3644
37- const computedData = useMemo < ChartData > ( ( ) => {
45+ const computedData = useMemo < TypedChartData > ( ( ) => {
3846 if ( typeof data === 'function' ) {
3947 return canvas . current
4048 ? data ( canvas . current )
@@ -44,17 +52,15 @@ const ChartComponent = forwardRef<Chart | undefined, Props>((props, ref) => {
4452 } else return merge ( { } , data ) ;
4553 } , [ data , canvas . current ] ) ;
4654
47- const [ chart , setChart ] = useState < Chart > ( ) ;
55+ const [ chart , setChart ] = useState < TypedChartJS > ( ) ;
4856
49- useImperativeHandle < Chart | undefined , Chart | undefined > ( ref , ( ) => chart , [
50- chart ,
51- ] ) ;
57+ useImperativeHandle < TypedChartJS , TypedChartJS > ( ref , ( ) => chart , [ chart ] ) ;
5258
5359 const renderChart = ( ) => {
5460 if ( ! canvas . current ) return ;
5561
5662 setChart (
57- new Chart ( canvas . current , {
63+ new ChartJS ( canvas . current , {
5864 type,
5965 data : computedData ,
6066 options,
@@ -63,38 +69,42 @@ const ChartComponent = forwardRef<Chart | undefined, Props>((props, ref) => {
6369 ) ;
6470 } ;
6571
66- const onClick = ( e : React . MouseEvent < HTMLCanvasElement > ) => {
72+ const onClick = ( event : MouseEvent < HTMLCanvasElement > ) => {
73+ if ( onClickProp ) {
74+ onClickProp ( event ) ;
75+ }
76+
6777 if ( ! chart ) return ;
6878
6979 getDatasetAtEvent &&
7080 getDatasetAtEvent (
7181 chart . getElementsAtEventForMode (
72- e as unknown as Event ,
82+ event . nativeEvent ,
7383 'dataset' ,
7484 { intersect : true } ,
7585 false
7686 ) ,
77- e
87+ event
7888 ) ;
7989 getElementAtEvent &&
8090 getElementAtEvent (
8191 chart . getElementsAtEventForMode (
82- e as unknown as Event ,
92+ event . nativeEvent ,
8393 'nearest' ,
8494 { intersect : true } ,
8595 false
8696 ) ,
87- e
97+ event
8898 ) ;
8999 getElementsAtEvent &&
90100 getElementsAtEvent (
91101 chart . getElementsAtEventForMode (
92- e as unknown as Event ,
102+ event . nativeEvent ,
93103 'index' ,
94104 { intersect : true } ,
95105 false
96106 ) ,
97- e
107+ event
98108 ) ;
99109 } ;
100110
@@ -128,8 +138,10 @@ const ChartComponent = forwardRef<Chart | undefined, Props>((props, ref) => {
128138 if ( ! currentDataSet || ! newDataSet . data ) return { ...newDataSet } ;
129139
130140 if ( ! currentDataSet . data ) {
141+ // @ts -expect-error Need to refactor
131142 currentDataSet . data = [ ] ;
132143 } else {
144+ // @ts -expect-error Need to refactor
133145 currentDataSet . data . length = newDataSet . data . length ;
134146 }
135147
@@ -163,23 +175,20 @@ const ChartComponent = forwardRef<Chart | undefined, Props>((props, ref) => {
163175 } else {
164176 updateChart ( ) ;
165177 }
166- } , [ props , computedData ] ) ;
178+ } ) ;
167179
168180 return (
169181 < canvas
170- { ...rest }
182+ ref = { canvas }
183+ role = 'img'
171184 height = { height }
172185 width = { width }
173- ref = { canvas }
174- id = { id }
175- className = { className }
176186 onClick = { onClick }
177- data-testid = 'canvas'
178- role = 'img'
187+ { ...props }
179188 >
180189 { fallbackContent }
181190 </ canvas >
182191 ) ;
183- } ) ;
192+ }
184193
185- export default ChartComponent ;
194+ export const Chart = forwardRef ( ChartComponent ) as TypedChartComponent ;
0 commit comments