-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcan-react-component.js
84 lines (71 loc) · 2.02 KB
/
can-react-component.js
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
var React = require("react");
var namespace = require("can-namespace");
var assign = require("can-assign");
var reflect = require("can-reflect");
var canSymbol = require("can-symbol");
var viewCallbacks = require("can-view-callbacks");
var viewModelSymbol = canSymbol.for("can.viewModel");
module.exports = namespace.reactComponent = function canReactComponent(displayName, CanComponent) {
if (arguments.length === 1) {
CanComponent = arguments[0];
displayName = (CanComponent.shortName || "CanComponent") + "Wrapper";
}
const __renderComponent = viewCallbacks._tags[CanComponent.prototype.tag];
viewCallbacks._tags[CanComponent.prototype.tag]= function renderComponent(el){
if(el.getAttribute("auto-mount") !== "false"){
__renderComponent.apply(this, arguments);
}
};
function Wrapper() {
React.Component.call(this);
this.canComponent = null;
this.createComponent = this.createComponent.bind(this);
}
Wrapper.displayName = displayName;
Wrapper.prototype = Object.create(React.Component.prototype);
assign(Wrapper.prototype, {
constructor: Wrapper,
createComponent: function(el) {
if (this.canComponent) {
this.canComponent = null;
}
if (el) {
this.vm = el[viewModelSymbol];
if(this.vm) {
reflect.assign(this.vm, this.props);
} else {
this.canComponent = new CanComponent(el, {viewModel: this.props});
this.vm = this.canComponent.viewModel;
}
}
},
componentDidUpdate: function() {
this.vm && reflect.assign(this.vm, this.props);
},
render: function() { // eslint-disable-line react/display-name
return React.createElement(CanComponent.prototype.tag, {
ref: this.createComponent,
"auto-mount": "false"
});
}
});
Object.defineProperty(Wrapper.prototype, "viewModel", {
enumerable: false,
configurable: true,
get: function() {
return this.vm;
}
});
try {
Object.defineProperty(Wrapper, "name", {
writable: false,
enumerable: false,
configurable: true,
value: displayName
});
}
catch(e) {
//
}
return Wrapper;
};