forked from coatue-oss/angular2react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
136 lines (121 loc) · 3.51 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
136
import { IComponentOptions, IScope } from 'angular'
import kebabCase = require('lodash.kebabcase')
import { $injector as defaultInjector } from 'ngimport'
import * as React from 'react'
interface Scope<Props> extends IScope {
props: Props
}
interface State<Props> {
didInitialCompile: boolean
scope?: Scope<Props>
}
/**
* 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={...} />
* ```
*/
export function angular2react<Props extends object>(
componentName: string,
component: IComponentOptions,
$injector = defaultInjector
): React.ComponentClass<Props> {
return class extends React.Component<Props, State<Props>> {
state: State<Props> = {
didInitialCompile: false
}
componentWillMount() {
this.setState({
scope: Object.assign($injector.get('$rootScope').$new(true), { props: writable(this.props) })
})
}
componentWillUnmount() {
if (!this.state.scope) {
return
}
this.state.scope.$destroy()
}
shouldComponentUpdate(): boolean {
return false
}
// called only once to set up DOM, after componentWillMount
render() {
const bindings: {[key: string]: string} = {}
if (component.bindings) {
for (const binding in component.bindings) {
bindings[kebabCase(binding)] = `props.${binding}`
}
}
return React.createElement(kebabCase(componentName),
{ ...bindings, ref: this.compile.bind(this) }
)
}
// makes angular aware of changed props
// if we're not inside a digest cycle, kicks off a digest cycle before setting.
componentWillReceiveProps(props: Props) {
if (!this.state.scope) {
return
}
this.state.scope.props = writable(props)
this.digest()
}
private compile(element: HTMLElement) {
if (this.state.didInitialCompile || !this.state.scope) {
return
}
$injector.get('$compile')(element)(this.state.scope)
this.digest()
this.setState({ didInitialCompile: true })
}
private digest() {
if (!this.state.scope) {
return
}
try { this.state.scope.$digest() } catch (e) { }
}
}
}
/**
* 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) {
if (Object.getOwnPropertyDescriptor(object, key).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
}