Skip to content

Commit

Permalink
feat(tooltip): 使用组件新 API setCapture
Browse files Browse the repository at this point in the history
  • Loading branch information
simaQ committed Mar 23, 2020
1 parent 1e4e0ba commit d5f1e21
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/chart/controller/tooltip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CONTAINER_CLASS } from '@antv/component/lib/tooltip/css-const';

import { deepMix, each, find, flatten, get, isArray, isEqual, isFunction, mix } from '@antv/util';
import { deepMix, each, find, flatten, get, isArray, isEqual, isFunction, mix, isUndefined } from '@antv/util';
import { Crosshair, HtmlTooltip, IGroup } from '../../dependents';
import Geometry from '../../geometry/base';
import { MappingDatum, Point, TooltipOption } from '../../interface';
Expand Down Expand Up @@ -163,13 +163,22 @@ export default class Tooltip extends Controller<TooltipOption> {
*/
public lockTooltip() {
this.isLocked = true;
if (this.tooltip) {
// tooltip contianer 可捕获事件
this.tooltip.setCapture(true);
}
}

/**
* unlockTooltip
*/
public unlockTooltip() {
this.isLocked = false;
const cfg = this.getTooltipCfg();
if (this.tooltip) {
// 重置 capture 属性
this.tooltip.setCapture(cfg.capture);
}
}

/**
Expand Down Expand Up @@ -305,14 +314,10 @@ export default class Tooltip extends Controller<TooltipOption> {
const option = view.getOptions().tooltip;
const theme = view.getTheme();
const defaultCfg = get(theme, ['components', 'tooltip'], {});
const pointerEvents = (get(option, 'enterable') || this.isLocked) ? 'auto' : (defaultCfg.enterable ? 'auto' : 'none');
return deepMix({}, defaultCfg, {
domStyles: {
[`${CONTAINER_CLASS}`]: {
pointerEvents,
},
},
}, option);
const enterable = isUndefined(get(option, 'enterable')) ? defaultCfg.enterable : get(option, 'enterable');
return deepMix({}, defaultCfg, option, {
capture: enterable || this.isLocked ? true : false,
});
}

private getTitle(items) {
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/chart/view/index-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,15 @@ describe('View', () => {
it('lockTooltip', () => {
view.lockTooltip();
expect(view.isTooltipLocked()).toBeTrue();
// @ts-ignore
expect(div.getElementsByClassName('g2-tooltip')[0].style['pointer-events']).toBe('auto');
});

it('unlockTooltip', () => {
view.unlockTooltip();
expect(view.isTooltipLocked()).toBeFalse();
// @ts-ignore
expect(div.getElementsByClassName('g2-tooltip')[0].style['pointer-events']).toBe('none');
});

it('filtered group scale values', () => {
Expand Down

0 comments on commit d5f1e21

Please sign in to comment.