forked from coatue-oss/angular2react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
135 lines (122 loc) · 3.52 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
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
import { IScope } from 'angular'
import * as angular from 'angular'
import kebabCase = require('lodash.kebabcase')
import { $injector as defaultInjector } from 'ngimport'
import * as React from 'react'
/**
* Wraps an Angular component in React. Returns a new React component.
*
* Usage:
*
* ```ts
* const Bar = { bindings: {...}, template: '...', ... }
*
* angular
* .module('foo', [])
* .component('bar', Bar)
*
* type Props = {
* onChange(value: number): void
* }
*
* const Bar = angular2react<Props>('bar', Bar, $compile)
*
* <Bar onChange={...} />
* ```
*/
function digest(scope: IScope): void{
if (!scope) {
return
}
try {scope.$digest() } catch (e) { }
}
export function angular2react<Props extends object>(
componentName: string,
component: angular.IComponentOptions,
$injector = defaultInjector
): React.FunctionComponent<Props> {
function getInjector() {
return $injector
|| angular.element(document.querySelectorAll('[ng-app]')[0]).injector()
|| angular.element(document.querySelectorAll('#angular-main-app')[0]).injector()
}
return function Component(props: Props): any {
const [didInitialCompile, setDidInitialCompile] = React.useState<Boolean>(false)
const scope = React.useMemo<IScope>(() => {
let s = getInjector().get('$rootScope').$new(true);
Object.assign(s, {props: writable(props)})
return s;
}, [])
React.useEffect(() => {
return () => {
if (!scope) {
return
}
scope.$destroy()
}
}, [scope])
// @ts-ignore
React.useEffect(() => {
if (!scope) {
return null
}
// @ts-ignore
scope.props = writable(props)
digest(scope)
})
const bindings: { [key: string]: string } = {}
if (component.bindings) {
for (const binding in component.bindings) {
if (component.bindings[binding].includes('@')) {
// @ts-ignore
bindings[kebabCase(binding)] = props[binding]
} else {
bindings[kebabCase(binding)] = `props.${binding}`
}
}
}
function compile(element: HTMLElement) {
if (didInitialCompile || !scope) {
return
}
const $injector = getInjector()
$injector.get('$compile')(element)(scope)
digest(scope)
setDidInitialCompile(true)
}
return React.createElement(
kebabCase(componentName),
{ ...bindings, ref: compile }
)
}
}
/**
* Angular may try to bind back a value via 2-way binding, but React marks all
* properties on `props` as non-configurable and non-writable.
*
* If we use a `Proxy` to intercept writes to these non-writable properties,
* we run into an issue where the proxy throws when trying to write anyway,
* even if we `return false`.
*
* Instead, we use the below ad-hoc proxy to catch writes to non-writable
* properties in `object`, and log a helpful warning when it happens.
*/
function writable<T extends object>(object: T): T {
const _object = {} as T
for (const key in object) {
if (object.hasOwnProperty(key)) {
Object.defineProperty(_object, key, {
get() { return object[key] },
set(value: any) {
let d = Object.getOwnPropertyDescriptor(object, key)
if (d && d.writable) {
return object[key] = value
} else {
console.warn(`Tried to write to non-writable property "${key}" of`, object, `. Consider using a callback instead of 2-way binding.`)
}
}
})
}
}
return _object
}