File tree 1 file changed +15
-0
lines changed
1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import PropTypes from 'prop-types'
2
2
import React , {
3
3
forwardRef ,
4
4
HTMLAttributes ,
5
+ MutableRefObject ,
5
6
useEffect ,
6
7
useImperativeHandle ,
7
8
useMemo ,
@@ -110,6 +111,16 @@ export interface CChartProps extends HTMLAttributes<HTMLCanvasElement | HTMLDivE
110
111
wrapper ?: boolean
111
112
}
112
113
114
+ type ForwardedRef < T > = ( ( instance : T | null ) => void ) | MutableRefObject < T | null > | null
115
+
116
+ const reforwardRef = < T , > ( ref : ForwardedRef < T > , value : T ) => {
117
+ if ( typeof ref === 'function' ) {
118
+ ref ( value )
119
+ } else if ( ref ) {
120
+ ref . current = value
121
+ }
122
+ }
123
+
113
124
export const CChart = forwardRef < ChartJS | undefined , CChartProps > (
114
125
(
115
126
{
@@ -180,6 +191,8 @@ export const CChart = forwardRef<ChartJS | undefined, CChartProps>(
180
191
options : computedOptions ,
181
192
plugins,
182
193
} )
194
+
195
+ reforwardRef ( ref , chartRef . current )
183
196
}
184
197
185
198
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -253,6 +266,8 @@ export const CChart = forwardRef<ChartJS | undefined, CChartProps>(
253
266
}
254
267
255
268
const destroyChart = ( ) => {
269
+ reforwardRef ( ref , null )
270
+
256
271
if ( chartRef . current ) {
257
272
chartRef . current . destroy ( )
258
273
chartRef . current = undefined
You can’t perform that action at this time.
0 commit comments