File tree 2 files changed +47
-0
lines changed
2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ var namespace = require("can-namespace");
3
3
var assign = require ( "can-assign" ) ;
4
4
var reflect = require ( "can-reflect" ) ;
5
5
var canSymbol = require ( "can-symbol" ) ;
6
+ var viewCallbacks = require ( "can-view-callbacks" ) ;
6
7
var viewModelSymbol = canSymbol . for ( "can.viewModel" ) ;
7
8
8
9
module . exports = namespace . reactComponent = function canReactComponent ( displayName , CanComponent ) {
@@ -11,6 +12,13 @@ module.exports = namespace.reactComponent = function canReactComponent(displayNa
11
12
displayName = ( CanComponent . shortName || "CanComponent" ) + "Wrapper" ;
12
13
}
13
14
15
+ const __renderComponent = viewCallbacks . _tags [ CanComponent . prototype . tag ] ;
16
+ viewCallbacks . _tags [ CanComponent . prototype . tag ] = function renderComponent ( el ) {
17
+ if ( el . getAttribute ( "auto-mount" ) !== "false" ) {
18
+ __renderComponent . apply ( this , arguments ) ;
19
+ }
20
+ } ;
21
+
14
22
function Wrapper ( ) {
15
23
React . Component . call ( this ) ;
16
24
@@ -47,6 +55,7 @@ module.exports = namespace.reactComponent = function canReactComponent(displayNa
47
55
render : function ( ) { // eslint-disable-line react/display-name
48
56
return React . createElement ( CanComponent . prototype . tag , {
49
57
ref : this . createComponent ,
58
+ "auto-mount" : "false"
50
59
} ) ;
51
60
}
52
61
} ) ;
Original file line number Diff line number Diff line change @@ -143,4 +143,42 @@ QUnit.module('can-react-component', (moduleHooks) => {
143
143
144
144
} ) ;
145
145
146
+
147
+ QUnit . test ( 'should be rendered only once' , ( assert ) => {
148
+ assert . expect ( 3 ) ;
149
+ const ConsumedComponent = canReactComponent (
150
+ 'ConsumedComponent' ,
151
+ CanComponent . extend ( 'ConsumedComponent' , {
152
+ tag : "consumed-component4" ,
153
+ ViewModel : {
154
+ first : {
155
+ type : 'string' ,
156
+ default : 'Ivo'
157
+ } ,
158
+ last : 'string' ,
159
+ name : {
160
+ get ( ) {
161
+ return this . first + ' ' + this . last ;
162
+ }
163
+ } ,
164
+ connectedCallback ( el ) {
165
+ if ( el . getAttribute ( "auto-mount" ) === "false" ) {
166
+ assert . equal ( this . last , "Pinheiro" , `'last' name should be 'Pinheiro'` ) ;
167
+ }
168
+ }
169
+ } ,
170
+ view : stache ( "<div class='inner'>{{name}}</div>" )
171
+ } )
172
+ ) ;
173
+
174
+ container . innerHTML = "<consumed-component4></consumed-component4>" ;
175
+
176
+ let divComponent = document . querySelector ( 'consumed-component4' ) ;
177
+ assert . equal ( getTextFromFrag ( divComponent ) , 'Ivo undefined' ) ;
178
+
179
+ ReactDOM . render ( < ConsumedComponent last = { "Pinheiro" } /> , container ) ;
180
+ divComponent = document . querySelector ( 'consumed-component4' ) ;
181
+ assert . equal ( getTextFromFrag ( divComponent ) , 'Ivo Pinheiro' ) ;
182
+ } ) ;
183
+
146
184
} ) ;
You can’t perform that action at this time.
0 commit comments