forked from coatue-oss/react2angular
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
64 lines (62 loc) · 1.94 KB
/
index.tsx
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
import { IAugmentedJQuery, IComponentOptions } from 'angular'
import fromPairs = require('lodash.frompairs')
import NgComponent from 'ngcomponent'
import * as React from 'react'
import { flushSync } from 'react-dom'
import { createRoot, Root } from 'react-dom/client'
/**
* Wraps a React component in Angular. Returns a new Angular component.
*
* Usage:
*
* ```ts
* type Props = { foo: number }
* class ReactComponent extends React.Component<Props, S> {}
* const AngularComponent = react2angular(ReactComponent, ['foo'])
* ```
*/
export function react2angular<Props>(
Class: React.ComponentType<Props>,
bindingNames: (keyof Props)[] | null = null,
injectNames: string[] = []
): IComponentOptions {
const names = bindingNames
|| (Class.propTypes && Object.keys(Class.propTypes) as (keyof Props)[])
|| []
return {
bindings: fromPairs(names.map(_ => [_, '<'])),
controller: ['$element', ...injectNames, class extends NgComponent<Props> {
static get $$ngIsClass() {
return true
}
isDestroyed: boolean = false
injectedProps: { [name: string]: any }
private _root: Root | null = null
private _renderToRoot: () => void = () => this._root!.render(<Class {...this.props} {...this.injectedProps as any} />)
constructor(private $element: IAugmentedJQuery, ...injectedProps: any[]) {
super()
this.injectedProps = {}
for (let i = 0; i < injectNames.length; i++) {
const name = injectNames[i]
this.injectedProps[name] = injectedProps[i]
}
}
render() {
if (this.isDestroyed) {
return
}
if (this._root === null) {
this._root = createRoot(this.$element[0])
}
flushSync(this._renderToRoot)
}
componentWillUnmount() {
this.isDestroyed = true
if (this._root !== null) {
this._root.unmount()
this._root = null
}
}
}]
}
}