UNPKG

@heap/react-native-heap

Version:

React Native event tracking with Heap.

52 lines (51 loc) 2.36 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import * as React from 'react'; import hoistNonReactStatic from 'hoist-non-react-statics'; import { getBaseComponentProps } from './common'; import { swallowErrors } from '../util/bailer'; import { getComponentDisplayName } from '../util/hocUtil'; export const autotrackSwitchChange = track => (eventType, componentThis, event) => { const autotrackProps = getBaseComponentProps(componentThis); if (!autotrackProps) { // We're not capturing this interaction. return; } track(eventType, autotrackProps); }; export const withHeapSwitchAutocapture = track => SwitchComponent => { class HeapSwitchAutocapture extends React.Component { trackEvent() { const autotrackProps = getBaseComponentProps(this); if (!autotrackProps) { // We're not capturing this interaction. return; } track('change', autotrackProps); } render() { const _a = this.props, { heapForwardedRef, onValueChange } = _a, rest = __rest(_a, ["heapForwardedRef", "onValueChange"]); return (React.createElement(SwitchComponent, Object.assign({ ref: heapForwardedRef, onValueChange: value => { swallowErrors(() => this.trackEvent())(); onValueChange && onValueChange(value); } }, rest), this.props.children)); } } const displayName = getComponentDisplayName(SwitchComponent); HeapSwitchAutocapture.displayName = `withHeapSwitchAutocapture(${displayName})`; const forwardRefHoc = React.forwardRef((props, ref) => { return React.createElement(HeapSwitchAutocapture, Object.assign({}, props, { heapForwardedRef: ref })); }); hoistNonReactStatic(forwardRefHoc, SwitchComponent); forwardRefHoc.displayName = displayName; return forwardRefHoc; };