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

Commit 6c0b4d5

Browse files
poxrudmhevery
authored andcommitted
fix(utils): add the ability to prevent the default action of onEvent (onclick, onpaste,etc..) by returning false.
Closes #236
1 parent f25b087 commit 6c0b4d5

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Diff for: lib/browser/utils.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,16 @@ export function patchProperty(obj, prop) {
6161
}
6262

6363
if (typeof fn === 'function') {
64-
this[_prop] = fn;
65-
this.addEventListener(eventName, fn, false);
64+
var wrapFn = (event) => {
65+
var result;
66+
result = fn.apply(this, arguments);
67+
68+
if (result != undefined && !result)
69+
event.preventDefault();
70+
};
71+
72+
this[_prop] = wrapFn;
73+
this.addEventListener(eventName, wrapFn, false);
6674
} else {
6775
this[_prop] = null;
6876
}

Diff for: test/browser/element.spec.ts

+29
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,33 @@ describe('element', function () {
266266
});
267267
});
268268

269+
describe('onEvent default behavior', function() {
270+
var checkbox;
271+
beforeEach(function () {
272+
checkbox = document.createElement('input');
273+
checkbox.type = "checkbox";
274+
document.body.appendChild(checkbox);
275+
});
276+
277+
afterEach(function () {
278+
document.body.removeChild(checkbox);
279+
});
280+
281+
it('should be possible to prevent default behavior by returning false', function() {
282+
checkbox.onclick = function() {
283+
return false;
284+
};
285+
286+
checkbox.click();
287+
expect(checkbox.checked).toBe(false);
288+
});
289+
290+
it('should have no effect on default behavior when not returning anything', function() {
291+
checkbox.onclick = function() {};
292+
293+
checkbox.click();
294+
expect(checkbox.checked).toBe(true);
295+
});
296+
});
297+
269298
});

0 commit comments

Comments
 (0)