This repository was archived by the owner on Feb 26, 2024. It is now read-only.
File tree 2 files changed +39
-2
lines changed
2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change @@ -61,8 +61,16 @@ export function patchProperty(obj, prop) {
61
61
}
62
62
63
63
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 ) ;
66
74
} else {
67
75
this [ _prop ] = null ;
68
76
}
Original file line number Diff line number Diff line change @@ -266,4 +266,33 @@ describe('element', function () {
266
266
} ) ;
267
267
} ) ;
268
268
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
+
269
298
} ) ;
You can’t perform that action at this time.
0 commit comments