@@ -23,7 +23,7 @@ import {usePress} from '../';
23
23
function Example ( props ) {
24
24
let { elementType : ElementType = 'div' , style, draggable, ...otherProps } = props ;
25
25
let { pressProps} = usePress ( otherProps ) ;
26
- return < ElementType { ...pressProps } style = { style } tabIndex = "0" draggable = { draggable } > { ElementType !== 'input' ? 'test' : undefined } </ ElementType > ;
26
+ return < ElementType { ...pressProps } style = { style } tabIndex = "0" draggable = { draggable } > { ElementType !== 'input' ? props . children || 'test' : undefined } </ ElementType > ;
27
27
}
28
28
29
29
function pointerEvent ( type , opts ) {
@@ -320,6 +320,85 @@ describe('usePress', function () {
320
320
] ) ;
321
321
} ) ;
322
322
323
+ it ( 'should cancel press if onClick propagation is stopped' , function ( ) {
324
+ let events = [ ] ;
325
+ let addEvent = ( e ) => events . push ( e ) ;
326
+ let res = render (
327
+ < Example
328
+ onPressStart = { addEvent }
329
+ onPressEnd = { addEvent }
330
+ onPressChange = { pressed => addEvent ( { type : 'presschange' , pressed} ) }
331
+ onPress = { addEvent }
332
+ onPressUp = { addEvent } >
333
+ { /* eslint-disable-next-line */ }
334
+ < div data-testid = "inner" onClick = { e => e . stopPropagation ( ) } />
335
+ </ Example >
336
+ ) ;
337
+
338
+ let el = res . getByTestId ( 'inner' ) ;
339
+ fireEvent ( el , pointerEvent ( 'pointerover' , { pointerId : 1 , pointerType : 'mouse' , clientX : 0 , clientY : 0 } ) ) ;
340
+
341
+ let shouldFireMouseEvents = fireEvent ( el , pointerEvent ( 'pointerdown' , { pointerId : 1 , pointerType : 'mouse' , clientX : 0 , clientY : 0 } ) ) ;
342
+ expect ( shouldFireMouseEvents ) . toBe ( true ) ;
343
+
344
+ let shouldFocus = fireEvent . mouseDown ( el ) ;
345
+ expect ( shouldFocus ) . toBe ( true ) ;
346
+ act ( ( ) => el . focus ( ) ) ;
347
+
348
+ fireEvent ( el , pointerEvent ( 'pointerup' , { pointerId : 1 , pointerType : 'mouse' , clientX : 0 , clientY : 0 } ) ) ;
349
+ fireEvent . mouseUp ( el ) ;
350
+
351
+ let shouldClick = fireEvent . click ( el ) ;
352
+ expect ( shouldClick ) . toBe ( true ) ;
353
+ fireEvent ( el , pointerEvent ( 'pointerout' , { pointerId : 1 , pointerType : 'mouse' , clientX : 0 , clientY : 0 } ) ) ;
354
+
355
+ act ( ( ) => jest . advanceTimersByTime ( 10 ) ) ;
356
+
357
+ expect ( events ) . toEqual ( [
358
+ {
359
+ type : 'pressstart' ,
360
+ target : el . parentElement ,
361
+ pointerType : 'mouse' ,
362
+ ctrlKey : false ,
363
+ metaKey : false ,
364
+ shiftKey : false ,
365
+ altKey : false ,
366
+ x : 0 ,
367
+ y : 0
368
+ } ,
369
+ {
370
+ type : 'presschange' ,
371
+ pressed : true
372
+ } ,
373
+ {
374
+ type : 'pressup' ,
375
+ target : el . parentElement ,
376
+ pointerType : 'mouse' ,
377
+ ctrlKey : false ,
378
+ metaKey : false ,
379
+ shiftKey : false ,
380
+ altKey : false ,
381
+ x : 0 ,
382
+ y : 0
383
+ } ,
384
+ {
385
+ type : 'pressend' ,
386
+ target : el . parentElement ,
387
+ pointerType : 'mouse' ,
388
+ ctrlKey : false ,
389
+ metaKey : false ,
390
+ shiftKey : false ,
391
+ altKey : false ,
392
+ x : 0 ,
393
+ y : 0
394
+ } ,
395
+ {
396
+ type : 'presschange' ,
397
+ pressed : false
398
+ }
399
+ ] ) ;
400
+ } ) ;
401
+
323
402
it ( 'should fire press change events when moving pointer outside target' , function ( ) {
324
403
let events = [ ] ;
325
404
let addEvent = ( e ) => events . push ( e ) ;
0 commit comments