-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathMgt.ts
More file actions
157 lines (134 loc) · 4.39 KB
/
Mgt.ts
File metadata and controls
157 lines (134 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import React, { ReactNode, ReactElement } from 'react';
import { createRoot } from 'react-dom/client';
import { Wc, WcProps, WcTypeProps } from 'wc-react';
import { customElementHelper, TemplateRenderedData } from '@microsoft/mgt-element';
export class Mgt extends Wc {
private _templates: Record<string, ReactElement>;
constructor(props: WcTypeProps) {
super(props);
}
protected getTag(): string {
let tag: string = super.getTag() as string;
const tagPrefix = `${customElementHelper.prefix}-`;
if (!tag.startsWith(tagPrefix)) {
tag = tagPrefix + tag;
}
return tag;
}
// type mismatch due to version drift
// @ts-expect-error - TS2416: Property 'render' in type 'Mgt' is not assignable to the same property in base type 'Wc'
public render(): React.DOMElement<React.DOMAttributes<HTMLElement>, HTMLElement> {
const tag = this.getTag();
if (!tag) {
throw new Error('"wcType" must be set!');
}
this.processTemplates(this.props.children);
const templateElements = [];
if (this._templates) {
for (const t in this._templates) {
if (Object.prototype.hasOwnProperty.call(this._templates, t)) {
const element = React.createElement('template', { key: t, 'data-type': t }, null);
templateElements.push(element);
}
}
}
return React.createElement(tag, { ref: (element: HTMLElement) => this.setRef(element) }, templateElements);
}
/**
* Sets the web component reference and syncs the props
*
* @protected
* @param {HTMLElement} element
* @memberof Wc
*/
protected setRef(component: HTMLElement) {
if (component) {
component.addEventListener('templateRendered', this.handleTemplateRendered);
}
super.setRef(component);
}
/**
* Removes all event listeners from web component element
*
* @protected
* @returns
* @memberof Mgt
*/
protected cleanUp() {
if (!this.element) {
return;
}
this.element.removeEventListener('templateRendered', this.handleTemplateRendered);
super.cleanUp();
}
/**
* Renders a template
*
* @protected
* @param {*} e
* @returns
* @memberof Mgt
*/
protected handleTemplateRendered = (e: CustomEvent<TemplateRenderedData>) => {
if (!this._templates) {
return;
}
const templateType = e.detail.templateType;
const dataContext = e.detail.context;
const element = e.detail.element;
let template = this._templates[templateType];
if (template) {
template = React.cloneElement(template, { dataContext });
const root = createRoot(element);
root.render(template);
}
};
/**
* Prepares templates for rendering
*
* @protected
* @param {ReactNode} children
* @returns
* @memberof Mgt
*/
protected processTemplates(children: ReactNode) {
if (!children) {
return;
}
const templates: Record<string, ReactElement> = {};
React.Children.forEach(children, child => {
const element = child as ReactElement<{ template: string }>;
const template = element?.props?.template;
if (template) {
templates[template] = element;
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/dot-notation
templates['default'] = element;
}
});
this._templates = templates;
}
}
/**
* Creates a new React Functional Component that wraps the
* web component with the specified tag name
*
* @template T - optional props type for component
* @param {(string | Function)} tag
* @returns React component
*/
export const wrapMgt = <T = WcProps>(tag: string, registerElementFunction: () => void) => {
registerElementFunction();
const WrapMgt = (props: T, ref: React.ForwardedRef<unknown>): React.CElement<WcTypeProps, Mgt> =>
React.createElement(Mgt, { wcType: tag, innerRef: ref, ...props });
const component: React.ForwardRefExoticComponent<
React.PropsWithoutRef<T & React.HTMLAttributes<unknown>> & React.RefAttributes<unknown>
> = React.forwardRef(WrapMgt);
return component;
};