Skip to content

Commit 650d3bf

Browse files
committed
Merge pull request reactjs#211 from jovnas/ContainerClass
ContainerClass
2 parents c80caf5 + 718e731 commit 650d3bf

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/React.AspNet/HtmlHelperExtensions.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private static IReactEnvironment Environment
6868
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
6969
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
7070
/// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param>
71+
/// <param name="containerClass">HTML class(es) to set on the container tag</param>
7172
/// <returns>The component's HTML</returns>
7273
public static IHtmlString React<T>(
7374
this IHtmlHelper htmlHelper,
@@ -76,14 +77,19 @@ public static IHtmlString React<T>(
7677
string htmlTag = null,
7778
string containerId = null,
7879
bool clientOnly = false,
79-
bool serverOnly = false
80+
bool serverOnly = false,
81+
string containerClass = null
8082
)
8183
{
8284
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
8385
if (!string.IsNullOrEmpty(htmlTag))
8486
{
8587
reactComponent.ContainerTag = htmlTag;
8688
}
89+
if (!string.IsNullOrEmpty(containerClass))
90+
{
91+
reactComponent.ContainerClass = containerClass;
92+
}
8793
var result = reactComponent.RenderHtml(clientOnly, serverOnly);
8894
return new HtmlString(result);
8995
}
@@ -100,21 +106,27 @@ public static IHtmlString React<T>(
100106
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
101107
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
102108
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
109+
/// <param name="containerClass">HTML class(es) to set on the container tag</param>
103110
/// <returns>The component's HTML</returns>
104111
public static IHtmlString ReactWithInit<T>(
105112
this IHtmlHelper htmlHelper,
106113
string componentName,
107114
T props,
108115
string htmlTag = null,
109116
string containerId = null,
110-
bool clientOnly = false
117+
bool clientOnly = false,
118+
string containerClass = null
111119
)
112120
{
113121
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
114122
if (!string.IsNullOrEmpty(htmlTag))
115123
{
116124
reactComponent.ContainerTag = htmlTag;
117125
}
126+
if (!string.IsNullOrEmpty(containerClass))
127+
{
128+
reactComponent.ContainerClass = containerClass;
129+
}
118130
var html = reactComponent.RenderHtml(clientOnly);
119131

120132
#if LEGACYASPNET

src/React.Core/IReactComponent.cs

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public interface IReactComponent
3434
/// </summary>
3535
string ContainerTag { get; set; }
3636

37+
/// <summary>
38+
/// Gets or sets the HTML class for the container of this component
39+
/// </summary>
40+
string ContainerClass { get; set; }
41+
3742
/// <summary>
3843
/// Renders the HTML for this component. This will execute the component server-side and
3944
/// return the rendered HTML.

src/React.Core/ReactComponent.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public class ReactComponent : IReactComponent
5252
/// </summary>
5353
public string ContainerTag { get; set; }
5454

55+
/// <summary>
56+
/// Gets or sets the HTML class for the container of this component
57+
/// </summary>
58+
public string ContainerClass { get; set; }
59+
5560
/// <summary>
5661
/// Gets or sets the props for this component
5762
/// </summary>
@@ -94,12 +99,19 @@ public virtual string RenderHtml(bool renderContainerOnly = false, bool renderSe
9499
: string.Format("ReactDOMServer.renderToString({0})", GetComponentInitialiser());
95100
html = _environment.Execute<string>(reactRenderCommand);
96101
}
102+
103+
string attributes = string.Format("id=\"{0}\"", ContainerId);
104+
if (!string.IsNullOrEmpty(ContainerClass))
105+
{
106+
attributes += string.Format(" class=\"{0}\"", ContainerClass);
107+
}
108+
97109
return string.Format(
98-
"<{2} id=\"{0}\">{1}</{2}>",
99-
ContainerId,
110+
"<{2} {0}>{1}</{2}>",
111+
attributes,
100112
html,
101113
ContainerTag
102-
);
114+
);
103115
}
104116
catch (JsRuntimeException ex)
105117
{

src/React.Tests/Core/ReactComponentTest.cs

+19
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ public void RenderHtmlShouldWrapComponentInCustomElement()
117117
Assert.AreEqual(@"<span id=""container"">[HTML]</span>", result);
118118
}
119119

120+
[Test]
121+
public void RenderHtmlShouldAddClassToElement()
122+
{
123+
var config = new Mock<IReactSiteConfiguration>();
124+
var environment = new Mock<IReactEnvironment>();
125+
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
126+
environment.Setup(x => x.Execute<string>(@"ReactDOMServer.renderToString(React.createElement(Foo, {""hello"":""World""}))"))
127+
.Returns("[HTML]");
128+
129+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
130+
{
131+
Props = new { hello = "World" },
132+
ContainerClass="test-class"
133+
};
134+
var result = component.RenderHtml();
135+
136+
Assert.AreEqual(@"<div id=""container"" class=""test-class"">[HTML]</div>", result);
137+
}
138+
120139
[Test]
121140
public void RenderJavaScriptShouldCallRenderComponent()
122141
{

0 commit comments

Comments
 (0)