Skip to content

Commit 61d1dc8

Browse files
committed
feat: add onReady callback support
1 parent 8af84bd commit 61d1dc8

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

__tests__/plots/line.spec.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ describe('LineChart', () => {
2121
expect(chartRef.current).toBeDefined()
2222
})
2323

24+
test('onReady should be called', () => {
25+
const onReady = (plot: BasePlot<LineOptions>) => {
26+
expect(plot).toBeDefined()
27+
}
28+
const div = document.createElement('div')
29+
ReactDOM.render(<LineChart data={[]} onReady={onReady} />, div)
30+
})
31+
2432
test('function ref should be called', () => {
2533
// let chart
2634
const getChart = (instance) => {

src/components/base/index.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface BaseChartProps<C extends Options>
5555
| RefCallback<BasePlot<C> | null>
5656
| MutableRefObject<BasePlot<C> | null>
5757
data?: Record<string, any> | Record<string, any>[]
58+
onReady?: (plot: BasePlot<C>) => void
5859
}
5960

6061
const BaseChart = <C extends Options>(
@@ -66,6 +67,7 @@ const BaseChart = <C extends Options>(
6667
style,
6768
className,
6869
chartRef: chart,
70+
onReady,
6971
...restProps
7072
} = props
7173
const chartRef = useRef<BasePlot<C> | null>(null)
@@ -92,6 +94,9 @@ const BaseChart = <C extends Options>(
9294
chartRef.current.render()
9395
}
9496
syncRef(chartRef, chart)
97+
if (chartRef.current) {
98+
onReady?.(chartRef.current)
99+
}
95100
return () => {
96101
/* istanbul ignore else */
97102
if (chartRef.current) {

0 commit comments

Comments
 (0)