forked from coatue-oss/react2angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
41 lines (39 loc) · 1.24 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
import { IAugmentedJQuery, IComponentOptions } from 'angular'
import fromPairs = require('lodash.frompairs')
import NgComponent from 'ngcomponent'
import * as React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
/**
* 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.ComponentClass<Props> | React.SFC<Props>,
bindingNames?: (keyof Props)[]
): IComponentOptions {
const names = bindingNames
|| (Class.propTypes && Object.keys(Class.propTypes))
|| []
return {
bindings: fromPairs(names.map(_ => [_, '<'])),
controller: ['$element', class extends NgComponent<Props, void> {
constructor(private $element: IAugmentedJQuery) {
super()
}
render() {
// TODO: rm any when https://github.com/Microsoft/TypeScript/pull/13288 is merged
render(<Class {...(this.props as any)} />, this.$element[0])
}
componentWillUnmount() {
unmountComponentAtNode(this.$element[0])
}
}]
}
}