UNPKG

4.63 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15var __assign = (this && this.__assign) || function () {
16 __assign = Object.assign || function(t) {
17 for (var s, i = 1, n = arguments.length; i < n; i++) {
18 s = arguments[i];
19 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20 t[p] = s[p];
21 }
22 return t;
23 };
24 return __assign.apply(this, arguments);
25};
26var __importStar = (this && this.__importStar) || function (mod) {
27 if (mod && mod.__esModule) return mod;
28 var result = {};
29 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
30 result["default"] = mod;
31 return result;
32};
33var __importDefault = (this && this.__importDefault) || function (mod) {
34 return (mod && mod.__esModule) ? mod : { "default": mod };
35};
36Object.defineProperty(exports, "__esModule", { value: true });
37var React = __importStar(require("react"));
38var case_1 = __importDefault(require("case"));
39var bearer_provider_1 = require("./bearer-provider");
40var BEARER_EVENT_PROP_PREFIX = 'bearer-';
41function fromBearer(TagName) {
42 var _a;
43 var propSetEvent = TagName + "-prop-set";
44 return _a = /** @class */ (function (_super) {
45 __extends(class_1, _super);
46 function class_1(props) {
47 var _this = _super.call(this, props) || this;
48 _this.prophandler = function (e) {
49 if (_this.context.handlePropUpdates) {
50 _this.context.handlePropUpdates(e);
51 }
52 };
53 _this.eventRef = React.createRef();
54 return _this;
55 }
56 class_1.prototype.componentDidMount = function () {
57 var _this = this;
58 if (this.eventRef.current) {
59 this.eventRef.current.addEventListener(propSetEvent, this.prophandler);
60 // NB: This means once a component is mounted we cannot change its event handlers
61 // In the future it would be good to improve this to allow dynamic allocation
62 this.handlers.forEach(function (key) {
63 var anyProps = _this.props;
64 if (anyProps[key]) {
65 _this.eventRef.current.addEventListener(key, anyProps[key]);
66 }
67 });
68 }
69 };
70 class_1.prototype.componentWillUnmount = function () {
71 var _this = this;
72 if (this.eventRef.current) {
73 this.eventRef.current.removeEventListener(propSetEvent, this.prophandler);
74 this.handlers.forEach(function (key) {
75 var anyProps = _this.props;
76 if (anyProps[key]) {
77 _this.eventRef.current.removeEventListener(key, anyProps[key]);
78 }
79 });
80 }
81 };
82 class_1.prototype.render = function () {
83 var combinedProps = __assign({}, this.context.state, this.props);
84 var tranformerdProps = Object.keys(combinedProps).reduce(function (acc, key) {
85 acc[case_1.default.kebab(key)] = combinedProps[key];
86 return acc;
87 }, {});
88 // @ts-ignore
89 return React.createElement(TagName, __assign({}, tranformerdProps, { ref: this.eventRef }));
90 };
91 Object.defineProperty(class_1.prototype, "handlers", {
92 get: function () {
93 return Object.keys(this.props).filter(function (key) { return key.startsWith(BEARER_EVENT_PROP_PREFIX); });
94 },
95 enumerable: true,
96 configurable: true
97 });
98 return class_1;
99 }(React.Component)),
100 _a.displayName = "Bearer(" + TagName + ")",
101 _a.contextType = bearer_provider_1.BearerContext,
102 _a;
103}
104exports.default = fromBearer;