Skip to content

Commit a45c27f

Browse files
committed
feat: add onReady callback support
1 parent 93946e9 commit a45c27f

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

__tests__/plots/line.spec.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { ref } from 'vue'
22
import { mount } from '@vue/test-utils'
33
import LineChart from '../../src/plots/line'
4+
import { LineOptions, Plot } from '@antv/g2plot'
45

56
describe('LineChart', () => {
67
test('render without crashed', () => {
78
const chartRef = ref(null)
8-
mount(<LineChart data={[]} chartRef={chartRef} />)
9+
const onReady = (plot: Plot<LineOptions>) => {
10+
expect(plot).toBeDefined()
11+
}
12+
mount(<LineChart data={[]} chartRef={chartRef} onReady={onReady} />)
913

1014
expect(chartRef.value).toBeDefined()
1115
})

src/components/base.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface BaseChartProps<C extends Options>
2121
chart: any
2222
data: Data
2323
chartRef?: Ref<BasePlot<C> | null>
24+
onReady?: (plot: BasePlot<C>) => void
2425
}
2526

2627
interface ComputedOptions<C extends Options> {
@@ -57,14 +58,15 @@ const BaseChart = defineComponent<
5758
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5859
// @ts-ignore
5960
style,
61+
onReady,
6062
...config
6163
} = this.attrConfig
6264
return config
6365
},
6466
},
6567
mounted() {
6668
const chartRef = this.$attrs.chartRef as Ref<BasePlot<any> | null>
67-
const { chart: Chart } = this.attrConfig
69+
const { chart: Chart, onReady } = this.attrConfig
6870
const plot = new Chart(this.$el as HTMLElement, {
6971
data: this.chartData,
7072
...this.chartConfig,
@@ -74,6 +76,7 @@ const BaseChart = defineComponent<
7476
chartRef.value = plot
7577
}
7678
this.plot = plot
79+
onReady?.(plot)
7780
},
7881
beforeUnmount() {
7982
/* istanbul ignore else */

0 commit comments

Comments
 (0)