UNPKG

2.34 kBJavaScriptView Raw
1import nullthrows from 'nullthrows';
2import React from 'react';
3import { TouchableOpacity } from 'react-native';
4import { AdTriggerViewContext } from './withNativeAd';
5export default class AdTriggerView extends React.Component {
6 constructor() {
7 super(...arguments);
8 this._trigger = null;
9 }
10 render() {
11 return (React.createElement(AdTriggerViewContext.Consumer, null, (contextValue) => {
12 const context = nullthrows(contextValue);
13 // Compute the context-dependent props to pass to the interactive component
14 const forwardedProps = this._getForwardedProps();
15 const props = Object.assign({}, forwardedProps, {
16 // Register the trigger component with the ad manager when it is mounted and unmounted
17 ref: (component) => {
18 if (component) {
19 this._trigger = component;
20 context.registerComponent(component);
21 }
22 else {
23 context.unregisterComponent(nullthrows(this._trigger));
24 this._trigger = null;
25 }
26 },
27 // Notify the ad manager to trigger the ad
28 onPress(...args) {
29 context.onTriggerAd();
30 if (forwardedProps.onPress) {
31 return forwardedProps.onPress(...args);
32 }
33 },
34 });
35 return this.props.renderInteractiveComponent
36 ? this.props.renderInteractiveComponent(props)
37 : this._renderDefaultInteractiveComponent(props);
38 }));
39 }
40 // NOTE: This is a helper method to extract the props to forward to the interactive component
41 // because TypeScript does not currently support rest objects with generic types in some cases,
42 // hence the type assertions
43 _getForwardedProps() {
44 const { renderInteractiveComponent, ...props } = this.props;
45 return props;
46 }
47 // TODO: change from TouchableOpacity to a Gesture Handler BorderlessButton
48 _renderDefaultInteractiveComponent(props) {
49 return React.createElement(TouchableOpacity, Object.assign({}, props, { collapsable: false }));
50 }
51}
52//# sourceMappingURL=AdTriggerView.js.map
\No newline at end of file