Skip to content

Commit 546aecd

Browse files
gunnimDaniel15
authored andcommitted
Simplify html helper function (reactjs#438)
* Simplify html helper function * Updated for backwards compatibility post release
1 parent 5328481 commit 546aecd

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/React.Router/HtmlHelperExtensions.cs

+39-7
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,40 @@ private static IReactEnvironment Environment
5353
/// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param>
5454
/// <param name="containerClass">HTML class(es) to set on the container tag</param>
5555
/// <returns><see cref="IHtmlString"/> containing the rendered markup for provided React Router component</returns>
56+
[Obsolete("Please use Html.ReactRouter instead")]
5657
public static IHtmlString ReactRouterWithContext<T>(
57-
this IHtmlHelper htmlHelper,
58+
this IHtmlHelper htmlHelper,
59+
string componentName,
60+
T props,
61+
string path = null,
62+
string htmlTag = null,
63+
string containerId = null,
64+
bool clientOnly = false,
65+
bool serverOnly = false,
66+
string containerClass = null,
67+
Action<HttpResponse, RoutingContext> contextHandler = null
68+
)
69+
{
70+
return ReactRouter(htmlHelper, componentName, props, path, htmlTag, containerId, clientOnly, serverOnly, containerClass, contextHandler);
71+
}
72+
73+
/// <summary>
74+
/// Render a React StaticRouter Component with context object.
75+
/// Can optionally be provided with a custom context handler to handle the various status codes.
76+
/// </summary>
77+
/// <param name="htmlHelper">MVC Razor <see cref="IHtmlHelper"/></param>
78+
/// <param name="componentName">Name of React Static Router component. Expose component globally to ReactJS.NET</param>
79+
/// <param name="props">Props to initialise the component with</param>
80+
/// <param name="path">F.x. from Request.Path. Used by React Static Router to determine context and routing.</param>
81+
/// <param name="contextHandler">Optional custom context handler, can be used instead of providing a Response object</param>
82+
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
83+
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
84+
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
85+
/// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param>
86+
/// <param name="containerClass">HTML class(es) to set on the container tag</param>
87+
/// <returns><see cref="IHtmlString"/> containing the rendered markup for provided React Router component</returns>
88+
public static IHtmlString ReactRouter<T>(
89+
this IHtmlHelper htmlHelper,
5890
string componentName,
5991
T props,
6092
string path = null,
@@ -71,12 +103,12 @@ public static IHtmlString ReactRouterWithContext<T>(
71103
var response = htmlHelper.ViewContext.HttpContext.Response;
72104
path = path ?? htmlHelper.ViewContext.HttpContext.Request.Path;
73105

74-
var reactComponent
106+
var reactComponent
75107
= Environment.CreateRouterComponent(
76-
componentName,
77-
props,
78-
path,
79-
containerId,
108+
componentName,
109+
props,
110+
path,
111+
containerId,
80112
clientOnly
81113
);
82114

@@ -88,7 +120,7 @@ var reactComponent
88120
{
89121
reactComponent.ContainerClass = containerClass;
90122
}
91-
123+
92124
var executionResult = reactComponent.RenderRouterWithContext(clientOnly, serverOnly);
93125

94126
if (executionResult.Context?.status != null)

tests/React.Tests/Router/HtmlHelperExtensionsTest.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void EngineIsReturnedToPoolAfterRender()
116116
var htmlHelperMock = new HtmlHelperMocks();
117117

118118
environment.Verify(x => x.ReturnEngineToPool(), Times.Never);
119-
var result = HtmlHelperExtensions.ReactRouterWithContext(
119+
var result = HtmlHelperExtensions.ReactRouter(
120120
htmlHelper: htmlHelperMock.htmlHelper.Object,
121121
componentName: "ComponentName",
122122
props: new { },
@@ -137,7 +137,7 @@ public void ReactWithClientOnlyTrueShouldCallRenderHtmlWithTrue()
137137
var environment = ConfigureMockEnvironment();
138138
var routerMocks = new ReactRouterMocks(config, environment);
139139

140-
var result = HtmlHelperExtensions.ReactRouterWithContext(
140+
var result = HtmlHelperExtensions.ReactRouter(
141141
htmlHelper: htmlHelperMock.htmlHelper.Object,
142142
componentName: "ComponentName",
143143
props: new { },
@@ -158,7 +158,7 @@ public void ReactWithServerOnlyTrueShouldCallRenderHtmlWithTrue()
158158
var environment = ConfigureMockEnvironment();
159159
var routerMocks = new ReactRouterMocks(config, environment);
160160

161-
var result = HtmlHelperExtensions.ReactRouterWithContext(
161+
var result = HtmlHelperExtensions.ReactRouter(
162162
htmlHelper: htmlHelperMock.htmlHelper.Object,
163163
componentName: "ComponentName",
164164
props: new { },
@@ -181,7 +181,7 @@ public void ShouldModifyStatusCode()
181181

182182
var htmlHelperMock = new HtmlHelperMocks();
183183

184-
HtmlHelperExtensions.ReactRouterWithContext(
184+
HtmlHelperExtensions.ReactRouter(
185185
htmlHelper: htmlHelperMock.htmlHelper.Object,
186186
componentName: "ComponentName",
187187
props: new { },
@@ -201,7 +201,7 @@ public void ShouldRunCustomContextHandler()
201201

202202
var htmlHelperMock = new HtmlHelperMocks();
203203

204-
HtmlHelperExtensions.ReactRouterWithContext(
204+
HtmlHelperExtensions.ReactRouter(
205205
htmlHelper: htmlHelperMock.htmlHelper.Object,
206206
componentName: "ComponentName",
207207
props: new { },
@@ -222,7 +222,7 @@ public void ShouldRedirectPermanent()
222222

223223
var htmlHelperMock = new HtmlHelperMocks();
224224

225-
HtmlHelperExtensions.ReactRouterWithContext(
225+
HtmlHelperExtensions.ReactRouter(
226226
htmlHelper: htmlHelperMock.htmlHelper.Object,
227227
componentName: "ComponentName",
228228
props: new { },
@@ -244,7 +244,7 @@ public void ShouldFailRedirectWithNoUrl()
244244

245245
Assert.Throws<ReactRouterException>(() =>
246246

247-
HtmlHelperExtensions.ReactRouterWithContext(
247+
HtmlHelperExtensions.ReactRouter(
248248
htmlHelper: htmlHelperMock.htmlHelper.Object,
249249
componentName: "ComponentName",
250250
props: new { },

0 commit comments

Comments
 (0)