@@ -38,14 +38,19 @@ public class ReactComponent : IReactComponent
38
38
private readonly IReactSiteConfiguration _configuration ;
39
39
40
40
/// <summary>
41
- /// Name of the component
41
+ /// Gets or sets the name of the component
42
42
/// </summary>
43
- private readonly string _componentName ;
43
+ public string ComponentName { get ; set ; }
44
44
45
45
/// <summary>
46
- /// Unique ID for the DIV container of this component
46
+ /// Gets or sets the unique ID for the DIV container of this component
47
47
/// </summary>
48
- private readonly string _containerId ;
48
+ public string ContainerId { get ; set ; }
49
+
50
+ /// <summary>
51
+ /// Gets or sets the HTML tag the component is wrapped in
52
+ /// </summary>
53
+ public string ContainerTag { get ; set ; }
49
54
50
55
/// <summary>
51
56
/// Gets or sets the props for this component
@@ -64,8 +69,9 @@ public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration con
64
69
EnsureComponentNameValid ( componentName ) ;
65
70
_environment = environment ;
66
71
_configuration = configuration ;
67
- _componentName = componentName ;
68
- _containerId = containerId ;
72
+ ComponentName = componentName ;
73
+ ContainerId = containerId ;
74
+ ContainerTag = "div" ;
69
75
}
70
76
71
77
/// <summary>
@@ -81,20 +87,20 @@ public string RenderHtml()
81
87
var html = _environment . Execute < string > (
82
88
string . Format ( "React.renderToString({0})" , GetComponentInitialiser ( ) )
83
89
) ;
84
- // TODO: Allow changing of the wrapper tag element from a DIV to something else
85
90
return string . Format (
86
- "<div id=\" {0}\" >{1}</div>" ,
87
- _containerId ,
88
- html
89
- ) ;
91
+ "<{2} id=\" {0}\" >{1}</{2}>" ,
92
+ ContainerId ,
93
+ html ,
94
+ ContainerTag
95
+ ) ;
90
96
}
91
97
catch ( JsRuntimeException ex )
92
98
{
93
99
throw new ReactServerRenderingException ( string . Format (
94
100
"Error while rendering \" {0}\" to \" {2}\" : {1}" ,
95
- _componentName ,
101
+ ComponentName ,
96
102
ex . Message ,
97
- _containerId
103
+ ContainerId
98
104
) ) ;
99
105
}
100
106
}
@@ -110,7 +116,7 @@ public string RenderJavaScript()
110
116
return string . Format (
111
117
"React.render({0}, document.getElementById({1}))" ,
112
118
GetComponentInitialiser ( ) ,
113
- JsonConvert . SerializeObject ( _containerId , _configuration . JsonSerializerSettings ) // SerializeObject accepts null settings
119
+ JsonConvert . SerializeObject ( ContainerId , _configuration . JsonSerializerSettings ) // SerializeObject accepts null settings
114
120
) ;
115
121
}
116
122
@@ -122,14 +128,14 @@ private void EnsureComponentExists()
122
128
// This is safe as componentName was validated via EnsureComponentNameValid()
123
129
var componentExists = _environment . Execute < bool > ( string . Format (
124
130
"typeof {0} !== 'undefined'" ,
125
- _componentName
131
+ ComponentName
126
132
) ) ;
127
133
if ( ! componentExists )
128
134
{
129
135
throw new ReactInvalidComponentException ( string . Format (
130
136
"Could not find a component named '{0}'. Did you forget to add it to " +
131
137
"App_Start\\ ReactConfig.cs?" ,
132
- _componentName
138
+ ComponentName
133
139
) ) ;
134
140
}
135
141
}
@@ -143,7 +149,7 @@ private string GetComponentInitialiser()
143
149
var encodedProps = JsonConvert . SerializeObject ( Props , _configuration . JsonSerializerSettings ) ; // SerializeObject accepts null settings
144
150
return string . Format (
145
151
"{0}({1})" ,
146
- _componentName ,
152
+ ComponentName ,
147
153
encodedProps
148
154
) ;
149
155
}
0 commit comments