1 | "use strict";
|
2 | var __importStar = (this && this.__importStar) || function (mod) {
|
3 | if (mod && mod.__esModule) return mod;
|
4 | var result = {};
|
5 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
6 | result["default"] = mod;
|
7 | return result;
|
8 | };
|
9 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
10 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
11 | };
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | const React = __importStar(require("react"));
|
14 | const case_1 = __importDefault(require("case"));
|
15 | const bearer_provider_1 = require("./bearer-provider");
|
16 | const BEARER_EVENT_PROP_PREFIX = 'bearer-';
|
17 | function fromBearer(TagName) {
|
18 | var _a;
|
19 | const propSetEvent = `${TagName}-prop-set`;
|
20 | return _a = class extends React.Component {
|
21 | constructor(props) {
|
22 | super(props);
|
23 | this.prophandler = (e) => {
|
24 | if (this.context.handlePropUpdates) {
|
25 | this.context.handlePropUpdates(e);
|
26 | }
|
27 | };
|
28 | this.eventRef = React.createRef();
|
29 | }
|
30 | componentDidMount() {
|
31 | if (this.eventRef.current) {
|
32 | this.eventRef.current.addEventListener(propSetEvent, this.prophandler);
|
33 |
|
34 |
|
35 | this.handlers.forEach(key => {
|
36 | const anyProps = this.props;
|
37 | if (anyProps[key]) {
|
38 | this.eventRef.current.addEventListener(key, anyProps[key]);
|
39 | }
|
40 | });
|
41 | }
|
42 | }
|
43 | componentWillUnmount() {
|
44 | if (this.eventRef.current) {
|
45 | this.eventRef.current.removeEventListener(propSetEvent, this.prophandler);
|
46 | this.handlers.forEach(key => {
|
47 | const anyProps = this.props;
|
48 | if (anyProps[key]) {
|
49 | this.eventRef.current.removeEventListener(key, anyProps[key]);
|
50 | }
|
51 | });
|
52 | }
|
53 | }
|
54 | render() {
|
55 | const combinedProps = Object.assign({}, this.context.state, this.props);
|
56 | const tranformerdProps = Object.keys(combinedProps).reduce((acc, key) => {
|
57 | acc[case_1.default.kebab(key)] = combinedProps[key];
|
58 | return acc;
|
59 | }, {});
|
60 |
|
61 | return React.createElement(TagName, Object.assign({}, tranformerdProps, { ref: this.eventRef }));
|
62 | }
|
63 | get handlers() {
|
64 | return Object.keys(this.props).filter((key) => key.startsWith(BEARER_EVENT_PROP_PREFIX));
|
65 | }
|
66 | },
|
67 | _a.displayName = `Bearer(${TagName})`,
|
68 | _a.contextType = bearer_provider_1.BearerContext,
|
69 | _a;
|
70 | }
|
71 | exports.default = fromBearer;
|