Skip to content

Commit

Permalink
feat(interaction): Intent to ship interaction.onout
Browse files Browse the repository at this point in the history
Implement onout to keep selected state when is away from chart area

Fix #3887
  • Loading branch information
netil authored Sep 30, 2024
1 parent 68966b1 commit 9c668e6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/ChartInternal/interactions/eventrect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export default {
return $$.isWithinShape(this, d);
});

if (shapeAtIndex.empty() && !isTooltipGrouped) {
if (shapeAtIndex.empty() && !isTooltipGrouped && config.interaction_onout) {
$$.hideGridFocus?.();
$$.hideTooltip();

Expand Down Expand Up @@ -591,7 +591,10 @@ export default {
state.event = event;

// chart is destroyed
if (!config || $$.hasArcType() || eventReceiver.currentIdx === -1) {
if (
!config || $$.hasArcType() || eventReceiver.currentIdx === -1 ||
!config.interaction_onout
) {
return;
}

Expand Down Expand Up @@ -637,7 +640,7 @@ export default {
*/
generateEventRectsForMultipleXs(eventRectEnter): void {
const $$ = this;
const {state} = $$;
const {config, state} = $$;

eventRectEnter
.on("click", function(event) {
Expand All @@ -656,7 +659,7 @@ export default {
state.event = event;

// chart is destroyed
if (!$$.config || $$.hasArcType()) {
if (!$$.config || $$.hasArcType() || !config.interaction_onout) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ChartInternal/shape/arc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ export default {
$$.setOverOut(true, arcData);
})
.on("mouseout", (event, d) => {
if (state.transiting) { // skip while transiting
if (state.transiting || !config.interaction_onout) { // skip while transiting
return;
}

Expand Down
6 changes: 4 additions & 2 deletions src/ChartInternal/shape/funnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,10 @@ export default {
.on(isTouch ? "touchend" : "mouseout", event => {
const data = getTarget(event);

$$.hideTooltip();
$$.setOverOut(false, data);
if (config.interaction_onout) {
$$.hideTooltip();
$$.setOverOut(false, data);
}
});
}
},
Expand Down
6 changes: 5 additions & 1 deletion src/ChartInternal/shape/radar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,18 @@ export default {

bindRadarEvent(): void {
const $$ = this;
const {state, $el: {radar, svg}} = $$;
const {config, state, $el: {radar, svg}} = $$;
const focusOnly = $$.isPointFocusOnly();
const {inputType, transiting} = state;
const isMouse = inputType === "mouse";

const hide = event => {
state.event = event;

if (!config.interaction_onout) {
return;
}

// const index = getIndex(event);

const index = $$.getDataIndexFromEvent(event);
Expand Down
6 changes: 4 additions & 2 deletions src/ChartInternal/shape/treemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ export default {
.on(isTouch ? "touchend" : "mouseout", event => {
const data = getTarget(event);

$$.hideTooltip();
$$.setOverOut(false, data);
if (config.interaction_onout) {
$$.hideTooltip();
$$.setOverOut(false, data);
}
});
}
},
Expand Down
10 changes: 8 additions & 2 deletions src/config/Options/interaction/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export default {
* @property {boolean} [interaction.inputType.mouse=true] enable or disable mouse interaction
* @property {boolean} [interaction.inputType.touch=true] enable or disable touch interaction
* @property {boolean|number} [interaction.inputType.touch.preventDefault=false] enable or disable to call event.preventDefault on touchstart & touchmove event. It's usually used to prevent document scrolling.
* @property {boolean} [interaction.onout=true] Enable or disable "onout" event.<br>
* When is disabled, defocus(hiding tooltip, focused gridline, etc.) event won't work.
* @see [Demo: touch.preventDefault](https://naver.github.io/billboard.js/demo/#Interaction.PreventScrollOnTouch)
* @example
* interaction: {
Expand All @@ -35,11 +37,15 @@ export default {
* // or threshold pixel value (pixel moved from touchstart to touchmove)
* preventDefault: 5
* }
* }
* },
*
* // disable "onout" event
* onout: false
* }
*/
interaction_enabled: true,
interaction_brighten: true,
interaction_inputType_mouse: true,
interaction_inputType_touch: <boolean | {preventDefault?: boolean | number}>{}
interaction_inputType_touch: <boolean | {preventDefault?: boolean | number}>{},
interaction_onout: true
};
27 changes: 27 additions & 0 deletions test/interactions/interaction-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1454,4 +1454,31 @@ describe("INTERACTION", () => {
expect(+point.attr("r")).to.be.above(r);
});
});

describe("interaction.onover", () => {
const spy = sinon.spy();

beforeAll(() => {
args = {
data: {
columns: [
["data1", 300, 350, 300, 0, 0, 0],
["data2", 130, 100, 140, 200, 150, 50]
],
onout: spy
},
interaction: {
onout: false
}
}
});

it("should maintain 'selected' state", () => {
util.hoverChart(chart, "mousemove", {clientX: 250, clientY: 311});
util.hoverChart(chart, "mouseout", {clientX: -100, clientY: -100});

expect(chart.$.tooltip.style("display")).to.be.equal("block");
expect(spy.called).to.be.false;
});
});
});
8 changes: 7 additions & 1 deletion types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,13 @@ export interface ChartOptions {
*/
preventDefault?: boolean | number;
};
}
},

/**
* Enable or disable "onout" event.
* When is disabled, defocus(hiding tooltip, focused gridline, etc.) event won't work.
*/
onout?: boolean;
};

transition?: {
Expand Down

0 comments on commit 9c668e6

Please sign in to comment.