UNPKG

2.12 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import { invariant } from "../../utilities/globals/index.js";
3import * as React from 'react';
4import hoistNonReactStatics from 'hoist-non-react-statics';
5import { ApolloConsumer } from "../context/index.js";
6function getDisplayName(WrappedComponent) {
7 return WrappedComponent.displayName || WrappedComponent.name || 'Component';
8}
9export function withApollo(WrappedComponent, operationOptions) {
10 if (operationOptions === void 0) { operationOptions = {}; }
11 var withDisplayName = "withApollo(".concat(getDisplayName(WrappedComponent), ")");
12 var WithApollo = (function (_super) {
13 __extends(WithApollo, _super);
14 function WithApollo(props) {
15 var _this = _super.call(this, props) || this;
16 _this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
17 return _this;
18 }
19 WithApollo.prototype.getWrappedInstance = function () {
20 __DEV__ ? invariant(operationOptions.withRef, "To access the wrapped instance, you need to specify " +
21 "{ withRef: true } in the options") : invariant(operationOptions.withRef, 31);
22 return this.wrappedInstance;
23 };
24 WithApollo.prototype.setWrappedInstance = function (ref) {
25 this.wrappedInstance = ref;
26 };
27 WithApollo.prototype.render = function () {
28 var _this = this;
29 return (React.createElement(ApolloConsumer, null, function (client) {
30 var props = Object.assign({}, _this.props, {
31 client: client,
32 ref: operationOptions.withRef
33 ? _this.setWrappedInstance
34 : undefined
35 });
36 return React.createElement(WrappedComponent, __assign({}, props));
37 }));
38 };
39 WithApollo.displayName = withDisplayName;
40 WithApollo.WrappedComponent = WrappedComponent;
41 return WithApollo;
42 }(React.Component));
43 return hoistNonReactStatics(WithApollo, WrappedComponent, {});
44}
45//# sourceMappingURL=withApollo.js.map
\No newline at end of file