Skip to content

Commit a4422c8

Browse files
authored
feat(tooltip):增加onShow与onHide钩子 (#2028)
* feat(tooltip): 增加onShow与onHide钩子 增加onShow与onHide钩子,让调用方可以感知tooltip出现与消失的时机,与V3能力拉齐 * test(tooltip.test): 补充测试用例 * test(tooltip.test): 修改测试用例 改为专用手势模拟与方法断言
1 parent e3659d2 commit a4422c8

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

packages/f2/src/components/tooltip/withTooltip.tsx

+18-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ export interface TooltipProps {
7171
custom?: boolean;
7272
tooltipMarkerStyle?: any;
7373
onChange?: any;
74+
/**
75+
* tooltip 展示回调
76+
*/
77+
onShow?: () => void;
78+
/**
79+
* tooltip 隐藏回调
80+
*/
81+
onHide?: () => void;
7482
showXTip?: boolean;
7583
/**
7684
* x 的位置点类型,record 表示按照数据取位置点,coord 表示按照坐标取位置点
@@ -175,11 +183,13 @@ export default (View) => {
175183
}
176184

177185
showSnapRecords(snapRecords) {
178-
const { chart, onChange } = this.props;
186+
const { chart, onChange, onShow } = this.props;
179187
const legendItems = chart.getLegendItems();
180188
const { xField, yField } = snapRecords[0];
181189
const xScale = chart.getScale(xField);
182190
const yScale = chart.getScale(yField);
191+
// 如果之前没有records,视为首次出现
192+
const isInitShow = !this.state.records;
183193

184194
const records = snapRecords.map((record) => {
185195
const { origin, xField, yField } = record;
@@ -214,15 +224,22 @@ export default (View) => {
214224
this.setState({
215225
records,
216226
});
227+
if(isInitShow && isFunction(onShow)) {
228+
onShow();
229+
}
217230
if (isFunction(onChange)) {
218231
onChange(records);
219232
}
220233
}
221234

222235
hide() {
236+
const { onHide } = this.props;
223237
this.setState({
224238
records: null,
225239
});
240+
if(isFunction(onHide)) {
241+
onHide();
242+
}
226243
}
227244

228245
render() {

packages/f2/test/components/tooltip/tooltip.test.tsx

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Axis, Canvas, Chart, Interval, jsx, Legend, Line, Tooltip } from '../../../src';
1+
import { Axis, Canvas, Chart, Interval, jsx, Legend, Line, Tooltip, createRef } from '../../../src';
22
import { createContext, delay, gestureSimulator } from '../../util';
33

44
const data = [
@@ -587,4 +587,32 @@ describe('tooltip', () => {
587587
await delay(500);
588588
expect(context).toMatchImageSnapshot();
589589
});
590+
591+
it('Tooltip onShow与onHide钩子触发且每次展示消失仅触发一次', async () => {
592+
const context = createContext('Tooltip onShow与onHide钩子触发且每次展示消失仅触发一次');
593+
const onShowCb = jest.fn();
594+
const onHideCb = jest.fn();
595+
const { props } = (
596+
<Canvas context={context} pixelRatio={1}>
597+
<Chart data={data} >
598+
<Axis field="genre" />
599+
<Axis field="sold" />
600+
<Interval x="genre" y="sold" color="genre" />
601+
<Tooltip snap showCrosshairs onShow={onShowCb} onHide={onHideCb} />
602+
</Chart>
603+
</Canvas>
604+
);
605+
606+
const canvas = new Canvas(props);
607+
await canvas.render();
608+
await delay(1000);
609+
await gestureSimulator(context.canvas, 'touchstart', { x: 30, y: 30 });
610+
await delay(1000);
611+
await gestureSimulator(context.canvas, 'touchmove', { x: 31, y: 31 });
612+
await delay(1000);
613+
await gestureSimulator(context.canvas, 'touchend', { x: 32, y: 32 });
614+
615+
expect(onShowCb.mock.calls.length).toBe(1);
616+
expect(onHideCb.mock.calls.length).toBe(1);
617+
});
590618
});

0 commit comments

Comments
 (0)