Skip to content

Commit

Permalink
Minor NavLink cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS committed Apr 4, 2018
1 parent 6c0fe36 commit 9a1494d
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Microsoft.AspNetCore.Blazor/Routing/NavLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace Microsoft.AspNetCore.Blazor.Routing
/// </summary>
public class NavLink : IComponent, IDisposable
{
private const string DefaultActiveClass = "active";

private RenderHandle _renderHandle;
private bool _isActive;

Expand Down Expand Up @@ -61,7 +63,7 @@ public void SetParameters(ParameterCollection parameters)
parameters.TryGetValue(RenderTreeBuilder.ChildContent, out _childContent);
parameters.TryGetValue("class", out _cssClass);
parameters.TryGetValue("href", out string href);
ActiveClass = parameters.GetValueOrDefault(nameof(ActiveClass), "active");
ActiveClass = parameters.GetValueOrDefault(nameof(ActiveClass), DefaultActiveClass);
Match = parameters.GetValueOrDefault(nameof(Match), NavLinkMatch.Prefix);
_allAttributes = parameters.ToDictionary();

Expand Down Expand Up @@ -110,8 +112,8 @@ private void Render(RenderTreeBuilder builder)
builder.OpenElement(0, "a");

// Set class attribute
string classAttrValue = CombineWithSpace(_cssClass, _isActive ? ActiveClass : null);
builder.AddAttribute(0, "class", classAttrValue);
builder.AddAttribute(0, "class",
CombineWithSpace(_cssClass, _isActive ? ActiveClass : null));

// Pass through all other attributes unchanged
foreach (var kvp in _allAttributes.Where(kvp => kvp.Key != "class" && kvp.Key != nameof(RenderTreeBuilder.ChildContent)))
Expand All @@ -126,7 +128,7 @@ private void Render(RenderTreeBuilder builder)
}

private string CombineWithSpace(string str1, string str2)
=> str1 == null ? (str2 ?? string.Empty) // Return an empty string if both str1 and str2 are null
=> str1 == null ? str2
: (str2 == null ? str1 : $"{str1} {str2}");

private static bool StartsWithAndHasSeparator(string value, string prefix)
Expand Down

0 comments on commit 9a1494d

Please sign in to comment.