-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
133 lines (117 loc) · 3.52 KB
/
Copy pathindex.js
File metadata and controls
133 lines (117 loc) · 3.52 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
"use strict";
/**
* @module ReactToAngularjs
* @author Guima Ferreira
* @description Helps you to migrate yours AngularJs Application to React component by component
*/
const React = require("react");
const reactDom = require("react-dom");
/**
* Generate AngularJs Components
* @param {String} mod AngularJs Module name
* @param {Array} components collection of AngularJs Components to be generated
* @param {ReactComponent} [theme] a react component that returns <ThemeProvider>{children}</ThemProvider>
* @example
* [
* // <my-component name="vm.name" on-change="vm.onChange"></my-component>
* {
* name: 'myComponent',
* react: MyComponent, // a React Component imported
* props: ['name', 'onChange']
* },
* // <simple-component></simple-component>
* {
* name: 'simpleComponent',
* react: SimpleComponent // a React Component imported
* }
* ]
* @example
* // Attention!
* // When passing functions that change its scope,
* // you need to do bind(this) as following:
* class MyController {
* constructor() {
* this.name = "William";
* this.onChange = this.onChange.bind(this);
* }
*
* onChange() {
* this.name = "Bill";
* }
* }
* // Or do this:
* class MyController {
* constructor() {
* this.name = "William";
* }
*
* onChange = () => {
* this.name = "Bill";
* }
* }
*/
function R2AComponents(mod, components, theme) {
components = [].concat(components);
components.forEach((component) => {
const reactComponent = R2AComponent(
component.react,
component.props,
theme
);
angular.module(mod).component(component.name, reactComponent);
});
}
/**
* Returns AngularJs Component config object that renders a React Component
* @param {Class} component React Component Class
* @param {Array} bindingNames AngularJs Component Attributes
* @param {ReactComponent} [theme] a react component that returns <ThemeProvider>{children}</ThemProvider>
* @example
* R2AComponent("myComponent", ['name', 'onChange']);
*/
function R2AComponent(component, bindingNames, theme) {
bindingNames = [].concat(bindingNames);
const controller = class ReactComponent {
constructor($element) {
this.$element = $element;
this.props = {};
}
$onChanges(changes) {
this.props = this._propsGet();
this.render();
}
_propsGet() {
return bindingNames.reduce((r, key) => {
r[key] = this[key];
return r;
}, {});
}
$onDestroy() {
this.isDestroyed = true;
reactDom.unmountComponentAtNode(this.$element[0]);
}
render() {
if (!this.isDestroyed) {
const componentElement = React.createElement(
component,
this.props
);
const element = theme
? React.createElement(theme, {}, componentElement)
: componentElement;
reactDom.render(element, this.$element[0]);
}
}
};
const _bindings = () =>
bindingNames.reduce((r, key) => {
r[key] = "<";
return r;
}, {});
return {
bindings: _bindings(),
controller: ["$element", controller],
};
}
module.exports.R2AComponents = R2AComponents;
module.exports.R2AComponent = R2AComponent;