Skip to content

Commit 8ca07d5

Browse files
committed
Allow a custom container ID to be provided. Closes #50
1 parent 6a681d1 commit 8ca07d5

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

src/React.Tests/Core/ReactEnvironmentTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99

1010
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
1113
using JavaScriptEngineSwitcher.Core;
1214
using Moq;
1315
using NUnit.Framework;
@@ -66,6 +68,32 @@ public void ExecuteWithLargerStackIfRequiredShouldBubbleExceptions()
6668
});
6769
}
6870

71+
[Test]
72+
public void GeneratesContainerIdIfNotProvided()
73+
{
74+
var mocks = new Mocks();
75+
var environment = mocks.CreateReactEnvironment();
76+
mocks.Config.Setup(x => x.Scripts).Returns(new List<string>());
77+
78+
var component1 = environment.CreateComponent("ComponentName", new { });
79+
var component2 = environment.CreateComponent("ComponentName", new { });
80+
Assert.AreEqual("react1", component1.ContainerId);
81+
Assert.AreEqual("react2", component2.ContainerId);
82+
}
83+
84+
[Test]
85+
public void UsesProvidedContainerId()
86+
{
87+
var mocks = new Mocks();
88+
var environment = mocks.CreateReactEnvironment();
89+
mocks.Config.Setup(x => x.Scripts).Returns(new List<string>());
90+
91+
var component1 = environment.CreateComponent("ComponentName", new { }, "foo");
92+
var component2 = environment.CreateComponent("ComponentName", new { });
93+
Assert.AreEqual("foo", component1.ContainerId);
94+
Assert.AreEqual("react1", component2.ContainerId);
95+
}
96+
6997
private class Mocks
7098
{
7199
public Mock<IJsEngine> Engine { get; private set; }

src/React.Web.Mvc4/HtmlHelperExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ private static IReactEnvironment Environment
3636
/// <param name="componentName">Name of the component</param>
3737
/// <param name="props">Props to initialise the component with</param>
3838
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
39+
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
3940
/// <returns>The component's HTML</returns>
4041
public static IHtmlString React<T>(
4142
this HtmlHelper htmlHelper,
4243
string componentName,
4344
T props,
44-
string htmlTag = null
45+
string htmlTag = null,
46+
string containerId = null
4547
)
4648
{
47-
var reactComponent = Environment.CreateComponent(componentName, props);
49+
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
4850
if (!string.IsNullOrEmpty(htmlTag))
4951
{
5052
reactComponent.ContainerTag = htmlTag;

src/React/IReactEnvironment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ public interface IReactEnvironment
8181
/// <typeparam name="T">Type of the props</typeparam>
8282
/// <param name="componentName">Name of the component</param>
8383
/// <param name="props">Props to use</param>
84+
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
8485
/// <returns>The component</returns>
85-
IReactComponent CreateComponent<T>(string componentName, T props);
86+
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null);
8687

8788
/// <summary>
8889
/// Renders the JavaScript required to initialise all components client-side. This will

src/React/ReactEnvironment.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,17 @@ public virtual bool HasVariable(string name)
291291
/// <typeparam name="T">Type of the props</typeparam>
292292
/// <param name="componentName">Name of the component</param>
293293
/// <param name="props">Props to use</param>
294+
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
294295
/// <returns>The component</returns>
295-
public virtual IReactComponent CreateComponent<T>(string componentName, T props)
296+
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null)
296297
{
297298
EnsureUserScriptsLoaded();
298-
_maxContainerId++;
299-
var containerId = string.Format(CONTAINER_ELEMENT_NAME, _maxContainerId);
299+
if (string.IsNullOrEmpty(containerId))
300+
{
301+
_maxContainerId++;
302+
containerId = string.Format(CONTAINER_ELEMENT_NAME, _maxContainerId);
303+
}
304+
300305
var component = new ReactComponent(this, _config, componentName, containerId)
301306
{
302307
Props = props

0 commit comments

Comments
 (0)