Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

fix(utils): add the ability to prevent the default action of onEvents #236

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,17 @@ export function patchProperty(obj, prop) {
}

if (typeof fn === 'function') {
this[_prop] = fn;
this.addEventListener(eventName, fn, false);

function wrapFn(event) {
var result;
result = fn.apply(this, arguments);

if (result != undefined && !result)
event.preventDefault();
};

this[_prop] = wrapFn;
this.addEventListener(eventName, wrapFn, false);
} else {
this[_prop] = null;
}
Expand Down
29 changes: 29 additions & 0 deletions test/patch/element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,33 @@ describe('element', function () {
});
});

describe('onEvent default behavior', function() {
var checkbox;
beforeEach(function () {
checkbox = document.createElement('input');
checkbox.type = "checkbox";
document.body.appendChild(checkbox);
});

afterEach(function () {
document.body.removeChild(checkbox);
});

it('should be possible to prevent default behavior by returning false', function() {
checkbox.onclick = function() {
return false;
};

checkbox.click();
expect(checkbox.checked).toBe(false);
});

it('should have no effect on default behavior when not returning anything', function() {
checkbox.onclick = function() {};

checkbox.click();
expect(checkbox.checked).toBe(true);
});
});

});