Skip to content

Commit

Permalink
fix: fix preventClickOnDrag option not working when changed (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
malangfox committed Oct 25, 2022
1 parent 1169aca commit c49817d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/axes/src/inputType/PanInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export class PanInput implements InputType {
protected _onPanmove(event: InputEventType) {
const {
iOSEdgeSwipeThreshold,
preventClickOnDrag,
releaseOnScroll,
inputKey,
inputButton,
Expand Down Expand Up @@ -339,7 +340,7 @@ export class PanInput implements InputType {
}
panEvent.preventSystemEvent = prevent;
if (prevent && (this._isOverThreshold || distance >= threshold)) {
this._dragged = true;
this._dragged = preventClickOnDrag;
this._isOverThreshold = true;
this._observer.change(this, panEvent, toAxis(this.axes, offset));
}
Expand Down Expand Up @@ -415,9 +416,7 @@ export class PanInput implements InputType {
this._observer = observer;
this._enabled = true;
this._activeEvent = activeEvent;
if (this.options.preventClickOnDrag) {
element.addEventListener("click", this._preventClickWhenDragged, true);
}
element.addEventListener("click", this._preventClickWhenDragged, true);
activeEvent.start.forEach((event) => {
element.addEventListener(event, this._onPanstart);
});
Expand All @@ -431,9 +430,7 @@ export class PanInput implements InputType {
const activeEvent = this._activeEvent;
const element = this.element;
if (element) {
if (this.options.preventClickOnDrag) {
element.removeEventListener("click", this._preventClickWhenDragged, true);
}
element.removeEventListener("click", this._preventClickWhenDragged, true);
activeEvent?.start.forEach((event) => {
element.removeEventListener(event, this._onPanstart);
});
Expand Down
30 changes: 30 additions & 0 deletions packages/axes/test/unit/inputType/PanInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,36 @@ describe("PanInput", () => {
}
);
});

it("should apply changes in preventClickOnDrag option after connected", (done) => {
// Given
const click = sinon.spy();
el.addEventListener("click", click);
input = new PanInput(el, {
inputType: ["touch", "mouse"],
preventClickOnDrag: true,
});
inst.connect(["x", "y"], input);
input.options.preventClickOnDrag = false;

// When
Simulator.gestures.pan(
el,
{
pos: [0, 0],
deltaX: 50,
deltaY: 50,
duration: 200,
easing: "linear",
},
() => {
el.click();
// Then
expect(click.called).to.be.true;
done();
}
);
});
});

describe("threshold", () => {
Expand Down

0 comments on commit c49817d

Please sign in to comment.