-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new HOC for instrumenting generic components
- Loading branch information
1 parent
d1dd84a
commit a3de057
Showing
4 changed files
with
101 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
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
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
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,57 @@ | ||
import hoistNonReactStatic from 'hoist-non-react-statics'; | ||
import * as React from 'react'; | ||
import { bailOnError } from '../util/bailer'; | ||
import { getComponentDisplayName } from '../util/hocUtil'; | ||
import { getBaseComponentProps } from './common'; | ||
|
||
export const withHeapAutocapture = track => (CapturableComponent, propName) => { | ||
class HeapAutocapture extends React.Component { | ||
trackEvent() { | ||
const autotrackProps = getBaseComponentProps(this); | ||
|
||
if (!autotrackProps) { | ||
// We're not capturing this interaction. | ||
return; | ||
} | ||
|
||
track('touch', autotrackProps); | ||
} | ||
|
||
render() { | ||
const { | ||
heapForwardedRef, | ||
[propName]: defaultImplementation, | ||
...rest | ||
} = this.props; | ||
|
||
const instrumentedProps = { | ||
[propName]: e => { | ||
bailOnError(() => this.trackEvent())(); | ||
defaultImplementation && defaultImplementation(e); | ||
}, | ||
}; | ||
|
||
return ( | ||
<CapturableComponent | ||
ref={heapForwardedRef} | ||
{...instrumentedProps} | ||
{...rest} | ||
> | ||
{this.props.children} | ||
</CapturableComponent> | ||
); | ||
} | ||
} | ||
|
||
HeapAutocapture.displayName = `withHeapAutocapture(${getComponentDisplayName( | ||
CapturableComponent | ||
)})`; | ||
|
||
const forwardRefHoc = React.forwardRef((props, ref) => { | ||
return <HeapAutocapture {...props} heapForwardedRef={ref} />; | ||
}); | ||
|
||
hoistNonReactStatic(forwardRefHoc, CapturableComponent); | ||
|
||
return forwardRefHoc; | ||
}; |