@@ -2,10 +2,9 @@ import type { AppTheme } from '../../App';
22
33import * as echarts from 'echarts' ;
44import { cloneDeep , merge } from 'lodash' ;
5- import React , { useCallback , useEffect , useRef , useState } from 'react' ;
5+ import React , { useEffect , useImperativeHandle , useRef , useState } from 'react' ;
66
77import { useAsync , useResize , useStorage } from '@react-devui/hooks' ;
8- import { setRef } from '@react-devui/hooks/useForkRef' ;
98import { getClassName } from '@react-devui/utils' ;
109
1110import { STORAGE_KEY } from '../../../config/storage' ;
@@ -14,11 +13,11 @@ import chartTheme from './theme.json';
1413echarts . registerTheme ( 'light' , chartTheme . light ) ;
1514echarts . registerTheme ( 'dark' , merge ( cloneDeep ( chartTheme . light ) , chartTheme . dark ) ) ;
1615
17- export interface AppEChartsProps < O extends echarts . EChartsOption > extends Omit < React . HTMLAttributes < HTMLDivElement > , 'children' > {
16+ export interface AppChartProps < O extends echarts . EChartsOption > extends Omit < React . HTMLAttributes < HTMLDivElement > , 'children' > {
1817 aOption : O | null ;
1918}
2019
21- function ECharts < O extends echarts . EChartsOption > ( props : AppEChartsProps < O > , ref : React . ForwardedRef < echarts . ECharts > ) : JSX . Element | null {
20+ function Chart < O extends echarts . EChartsOption > ( props : AppChartProps < O > , ref : React . ForwardedRef < echarts . ECharts > ) : JSX . Element | null {
2221 const {
2322 aOption,
2423
@@ -27,6 +26,7 @@ function ECharts<O extends echarts.EChartsOption>(props: AppEChartsProps<O>, ref
2726
2827 //#region Ref
2928 const elRef = useRef < HTMLDivElement > ( null ) ;
29+ const containerRef = useRef < HTMLDivElement > ( null ) ;
3030 //#endregion
3131
3232 const dataRef = useRef < {
@@ -38,17 +38,14 @@ function ECharts<O extends echarts.EChartsOption>(props: AppEChartsProps<O>, ref
3838 const themeStorage = useStorage < AppTheme > ( ...STORAGE_KEY . theme ) ;
3939
4040 const [ instance , setInstance ] = useState < echarts . ECharts | null > ( null ) ;
41- const containerRef = useCallback (
42- ( el : HTMLElement | null ) => {
43- setInstance ( ( draft ) => {
44- draft ?. dispose ( ) ;
45- const instance = el ? echarts . init ( el , themeStorage . value , { renderer : 'svg' } ) : null ;
46- setRef ( ref , instance ) ;
47- return instance ;
48- } ) ;
49- } ,
50- [ ref , themeStorage . value ]
51- ) ;
41+
42+ useEffect ( ( ) => {
43+ const instance = containerRef . current ? echarts . init ( containerRef . current , themeStorage . value , { renderer : 'svg' } ) : null ;
44+ setInstance ( instance ) ;
45+ return ( ) => {
46+ instance ?. dispose ( ) ;
47+ } ;
48+ } , [ themeStorage . value ] ) ;
5249
5350 useEffect ( ( ) => {
5451 if ( instance && aOption ) {
@@ -66,11 +63,13 @@ function ECharts<O extends echarts.EChartsOption>(props: AppEChartsProps<O>, ref
6663 }
6764 } ) ;
6865
66+ useImperativeHandle < echarts . ECharts | null , echarts . ECharts | null > ( ref , ( ) => instance , [ instance ] ) ;
67+
6968 return (
70- < div { ...restProps } ref = { elRef } className = { getClassName ( restProps . className , 'app-echarts ' ) } >
71- < div ref = { containerRef } className = "app-echarts__container " > </ div >
69+ < div { ...restProps } ref = { elRef } className = { getClassName ( restProps . className , 'app-chart ' ) } >
70+ < div ref = { containerRef } className = "app-chart__container " > </ div >
7271 </ div >
7372 ) ;
7473}
7574
76- export const AppECharts = React . forwardRef ( ECharts ) ;
75+ export const AppChart = React . forwardRef ( Chart ) ;
0 commit comments