Skip to content

Simplify html helper function #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions src/React.Router/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,40 @@ private static IReactEnvironment Environment
/// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param>
/// <param name="containerClass">HTML class(es) to set on the container tag</param>
/// <returns><see cref="IHtmlString"/> containing the rendered markup for provided React Router component</returns>
[Obsolete("Please use Html.ReactRouter instead")]
public static IHtmlString ReactRouterWithContext<T>(
this IHtmlHelper htmlHelper,
this IHtmlHelper htmlHelper,
string componentName,
T props,
string path = null,
string htmlTag = null,
string containerId = null,
bool clientOnly = false,
bool serverOnly = false,
string containerClass = null,
Action<HttpResponse, RoutingContext> contextHandler = null
)
{
return ReactRouter(htmlHelper, componentName, props, path, htmlTag, containerId, clientOnly, serverOnly, containerClass, contextHandler);
}

/// <summary>
/// Render a React StaticRouter Component with context object.
/// Can optionally be provided with a custom context handler to handle the various status codes.
/// </summary>
/// <param name="htmlHelper">MVC Razor <see cref="IHtmlHelper"/></param>
/// <param name="componentName">Name of React Static Router component. Expose component globally to ReactJS.NET</param>
/// <param name="props">Props to initialise the component with</param>
/// <param name="path">F.x. from Request.Path. Used by React Static Router to determine context and routing.</param>
/// <param name="contextHandler">Optional custom context handler, can be used instead of providing a Response object</param>
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
/// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param>
/// <param name="containerClass">HTML class(es) to set on the container tag</param>
/// <returns><see cref="IHtmlString"/> containing the rendered markup for provided React Router component</returns>
public static IHtmlString ReactRouter<T>(
this IHtmlHelper htmlHelper,
string componentName,
T props,
string path = null,
Expand All @@ -71,12 +103,12 @@ public static IHtmlString ReactRouterWithContext<T>(
var response = htmlHelper.ViewContext.HttpContext.Response;
path = path ?? htmlHelper.ViewContext.HttpContext.Request.Path;

var reactComponent
var reactComponent
= Environment.CreateRouterComponent(
componentName,
props,
path,
containerId,
componentName,
props,
path,
containerId,
clientOnly
);

Expand All @@ -88,7 +120,7 @@ var reactComponent
{
reactComponent.ContainerClass = containerClass;
}

var executionResult = reactComponent.RenderRouterWithContext(clientOnly, serverOnly);

if (executionResult.Context?.status != null)
Expand Down
14 changes: 7 additions & 7 deletions tests/React.Tests/Router/HtmlHelperExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void EngineIsReturnedToPoolAfterRender()
var htmlHelperMock = new HtmlHelperMocks();

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

var result = HtmlHelperExtensions.ReactRouterWithContext(
var result = HtmlHelperExtensions.ReactRouter(
htmlHelper: htmlHelperMock.htmlHelper.Object,
componentName: "ComponentName",
props: new { },
Expand All @@ -158,7 +158,7 @@ public void ReactWithServerOnlyTrueShouldCallRenderHtmlWithTrue()
var environment = ConfigureMockEnvironment();
var routerMocks = new ReactRouterMocks(config, environment);

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

var htmlHelperMock = new HtmlHelperMocks();

HtmlHelperExtensions.ReactRouterWithContext(
HtmlHelperExtensions.ReactRouter(
htmlHelper: htmlHelperMock.htmlHelper.Object,
componentName: "ComponentName",
props: new { },
Expand All @@ -201,7 +201,7 @@ public void ShouldRunCustomContextHandler()

var htmlHelperMock = new HtmlHelperMocks();

HtmlHelperExtensions.ReactRouterWithContext(
HtmlHelperExtensions.ReactRouter(
htmlHelper: htmlHelperMock.htmlHelper.Object,
componentName: "ComponentName",
props: new { },
Expand All @@ -222,7 +222,7 @@ public void ShouldRedirectPermanent()

var htmlHelperMock = new HtmlHelperMocks();

HtmlHelperExtensions.ReactRouterWithContext(
HtmlHelperExtensions.ReactRouter(
htmlHelper: htmlHelperMock.htmlHelper.Object,
componentName: "ComponentName",
props: new { },
Expand All @@ -244,7 +244,7 @@ public void ShouldFailRedirectWithNoUrl()

Assert.Throws<ReactRouterException>(() =>

HtmlHelperExtensions.ReactRouterWithContext(
HtmlHelperExtensions.ReactRouter(
htmlHelper: htmlHelperMock.htmlHelper.Object,
componentName: "ComponentName",
props: new { },
Expand Down