-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
React events: extract common helper functions (#15449)
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type { | ||
ReactResponderEvent, | ||
ReactResponderContext, | ||
} from 'shared/ReactTypes'; | ||
|
||
export function getEventCurrentTarget( | ||
event: ReactResponderEvent, | ||
context: ReactResponderContext, | ||
) { | ||
const target: any = event.target; | ||
let currentTarget = target; | ||
while ( | ||
currentTarget.parentNode && | ||
currentTarget.parentNode.nodeType === Node.ELEMENT_NODE && | ||
context.isTargetWithinEventComponent(currentTarget.parentNode) | ||
) { | ||
currentTarget = currentTarget.parentNode; | ||
} | ||
return currentTarget; | ||
} | ||
|
||
export function getEventPointerType(event: ReactResponderEvent) { | ||
const nativeEvent: any = event.nativeEvent; | ||
const {type, pointerType} = nativeEvent; | ||
if (pointerType != null) { | ||
return pointerType; | ||
} | ||
if (type.indexOf('mouse') === 0) { | ||
return 'mouse'; | ||
} | ||
if (type.indexOf('touch') === 0) { | ||
return 'touch'; | ||
} | ||
if (type.indexOf('key') === 0) { | ||
return 'keyboard'; | ||
} | ||
return ''; | ||
} | ||
|
||
export function isEventPositionWithinTouchHitTarget( | ||
event: ReactResponderEvent, | ||
context: ReactResponderContext, | ||
) { | ||
const nativeEvent: any = event.nativeEvent; | ||
const target: any = event.target; | ||
return context.isPositionWithinTouchHitTarget( | ||
target.ownerDocument, | ||
nativeEvent.x, | ||
nativeEvent.y, | ||
); | ||
} |