From 88c1f75d0beb5f027b957e76f26c1861bf74caaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnar=20M=C3=A1r=20=C3=93ttarsson?= Date: Sun, 17 Sep 2017 14:05:46 +0000 Subject: [PATCH 1/2] Simplify html helper function --- src/React.Router/HtmlHelperExtensions.cs | 4 ++-- .../React.Tests/Router/HtmlHelperExtensionsTest.cs | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/React.Router/HtmlHelperExtensions.cs b/src/React.Router/HtmlHelperExtensions.cs index cabd14c6f..dc9429ac9 100644 --- a/src/React.Router/HtmlHelperExtensions.cs +++ b/src/React.Router/HtmlHelperExtensions.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2014-Present, Facebook, Inc. * All rights reserved. * @@ -56,7 +56,7 @@ private static IReactEnvironment Environment /// Skip rendering React specific data-attributes during server side rendering. Defaults to false /// HTML class(es) to set on the container tag /// containing the rendered markup for provided React Router component - public static IHtmlString ReactRouterWithContext( + public static IHtmlString ReactRouter( this IHtmlHelper htmlHelper, string componentName, T props, diff --git a/tests/React.Tests/Router/HtmlHelperExtensionsTest.cs b/tests/React.Tests/Router/HtmlHelperExtensionsTest.cs index 7ccaf6230..9f34b5d68 100644 --- a/tests/React.Tests/Router/HtmlHelperExtensionsTest.cs +++ b/tests/React.Tests/Router/HtmlHelperExtensionsTest.cs @@ -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 { }, @@ -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 { }, @@ -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 { }, @@ -181,7 +181,7 @@ public void ShouldModifyStatusCode() var htmlHelperMock = new HtmlHelperMocks(); - HtmlHelperExtensions.ReactRouterWithContext( + HtmlHelperExtensions.ReactRouter( htmlHelper: htmlHelperMock.htmlHelper.Object, componentName: "ComponentName", props: new { }, @@ -201,7 +201,7 @@ public void ShouldRunCustomContextHandler() var htmlHelperMock = new HtmlHelperMocks(); - HtmlHelperExtensions.ReactRouterWithContext( + HtmlHelperExtensions.ReactRouter( htmlHelper: htmlHelperMock.htmlHelper.Object, componentName: "ComponentName", props: new { }, @@ -222,7 +222,7 @@ public void ShouldRedirectPermanent() var htmlHelperMock = new HtmlHelperMocks(); - HtmlHelperExtensions.ReactRouterWithContext( + HtmlHelperExtensions.ReactRouter( htmlHelper: htmlHelperMock.htmlHelper.Object, componentName: "ComponentName", props: new { }, @@ -244,7 +244,7 @@ public void ShouldFailRedirectWithNoUrl() Assert.Throws(() => - HtmlHelperExtensions.ReactRouterWithContext( + HtmlHelperExtensions.ReactRouter( htmlHelper: htmlHelperMock.htmlHelper.Object, componentName: "ComponentName", props: new { }, From 7a55142a4d6c5dc05679dc9d47a227a1d0c5861c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnar=20M=C3=A1r=20=C3=93ttarsson?= Date: Wed, 15 Nov 2017 21:19:35 +0000 Subject: [PATCH 2/2] Updated for backwards compatibility post release --- src/React.Router/HtmlHelperExtensions.cs | 46 ++++++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/React.Router/HtmlHelperExtensions.cs b/src/React.Router/HtmlHelperExtensions.cs index c7669c4ea..7e8df2c33 100644 --- a/src/React.Router/HtmlHelperExtensions.cs +++ b/src/React.Router/HtmlHelperExtensions.cs @@ -38,6 +38,38 @@ private static IReactEnvironment Environment } } + /// + /// Render a React StaticRouter Component with context object. + /// Can optionally be provided with a custom context handler to handle the various status codes. + /// + /// MVC Razor + /// Name of React Static Router component. Expose component globally to ReactJS.NET + /// Props to initialise the component with + /// F.x. from Request.Path. Used by React Static Router to determine context and routing. + /// Optional custom context handler, can be used instead of providing a Response object + /// HTML tag to wrap the component in. Defaults to <div> + /// ID to use for the container HTML tag. Defaults to an auto-generated ID + /// Skip rendering server-side and only output client-side initialisation code. Defaults to false + /// Skip rendering React specific data-attributes during server side rendering. Defaults to false + /// HTML class(es) to set on the container tag + /// containing the rendered markup for provided React Router component + [Obsolete("Please use Html.ReactRouter instead")] + public static IHtmlString ReactRouterWithContext( + 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 contextHandler = null + ) + { + return ReactRouter(htmlHelper, componentName, props, path, htmlTag, containerId, clientOnly, serverOnly, containerClass, contextHandler); + } + /// /// Render a React StaticRouter Component with context object. /// Can optionally be provided with a custom context handler to handle the various status codes. @@ -54,7 +86,7 @@ private static IReactEnvironment Environment /// HTML class(es) to set on the container tag /// containing the rendered markup for provided React Router component public static IHtmlString ReactRouter( - this IHtmlHelper htmlHelper, + this IHtmlHelper htmlHelper, string componentName, T props, string path = null, @@ -71,12 +103,12 @@ public static IHtmlString ReactRouter( 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 ); @@ -88,7 +120,7 @@ var reactComponent { reactComponent.ContainerClass = containerClass; } - + var executionResult = reactComponent.RenderRouterWithContext(clientOnly, serverOnly); if (executionResult.Context?.status != null)