You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 1.1KB

123456789101112131415161718192021222324252627282930313233
  1. /* @flow */
  2. /* eslint-disable flowtype/space-before-type-colon */
  3. /**
  4. * Prevents further propagation of the events to be handler by a specific event
  5. * handler/listener in the capturing and bubbling phases.
  6. *
  7. * @param {Function} eventHandler - The event handler/listener which handles
  8. * events that need to be stopped from propagating.
  9. * @returns {Function} An event handler/listener to be used in place of the
  10. * specified eventHandler in order to stop the events from propagating.
  11. */
  12. export function stopEventPropagation<T>(eventHandler: (ev: Event) => T)
  13. : (ev: Event) => T {
  14. /* eslint-enable flowtype/space-before-type-colon */
  15. return (ev: Event): T => {
  16. const r = eventHandler(ev);
  17. // React Native does not propagate the press event so, for the sake of
  18. // cross-platform compatibility, stop the propagation on Web as well.
  19. // Additionally, use feature checking in order to deal with browser
  20. // differences.
  21. if (ev && ev.stopPropagation) {
  22. ev.stopPropagation();
  23. ev.preventDefault && ev.preventDefault();
  24. }
  25. return r;
  26. };
  27. }