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

Commit

Permalink
fix(utils): add the ability to prevent the default action of onEvent …
Browse files Browse the repository at this point in the history
…(onclick, onpaste,etc..) by returning false.

Closes #236
  • Loading branch information
poxrud authored and mhevery committed Mar 21, 2016
1 parent f25b087 commit 99940c3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/browser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ export function patchProperty(obj, prop) {
}

if (typeof fn === 'function') {
this[_prop] = fn;
this.addEventListener(eventName, fn, false);
var wrapFn = function (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/browser/element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,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);
});
});

});

0 comments on commit 99940c3

Please sign in to comment.