ant-design-blazor/components/menu/MenuLink.cs
ConanYao af7d996633
feat(module: reuse-tabs): make a MenuService for supporting fetching title information across components (#3960)
* Separate 'ShouldMatch' method from 'MenuLink'

* make a `MenuService` for supporting fetching title information across components

* Update MenusService

* change 'MenuHelper' to static class and methods.

* Update MenuHelper

* feat(module: reuse-tabs): support for fetching tab title form menu

* clean up

* fix breadcrumb

---------

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2024-07-07 12:26:22 +08:00

135 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Routing;
namespace AntDesign
{
public class MenuLink : AntDomComponentBase
{
private const string DefaultActiveClass = "active";
private bool _isActive;
private string _hrefAbsolute;
private string _class;
/// <summary>
/// Gets or sets the CSS class name applied to the NavLink when the
/// current route matches the NavLink href.
/// </summary>
[Parameter]
public string ActiveClass { get; set; }
[Parameter]
public string Href { get; set; }
/// <summary>
/// Gets or sets the child content of the component.
/// </summary>
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter(CaptureUnmatchedValues = true)]
public Dictionary<string, object> Attributes { get; set; }
/// <summary>
/// Gets or sets a value representing the URL matching behavior.
/// </summary>
[Parameter]
public NavLinkMatch Match { get; set; } = NavLinkMatch.All;
[CascadingParameter]
public MenuItem MenuItem { get; set; }
[CascadingParameter]
public Menu Menu { get; set; }
[CascadingParameter]
public Button Button { get; set; }
[Inject] private NavigationManager NavigationManger { get; set; }
/// <inheritdoc />
protected override void OnInitialized()
{
// We'll consider re-rendering on each location change
NavigationManger.LocationChanged += OnLocationChanged;
ClassMapper.If(ActiveClass, () => _isActive)
.If(DefaultActiveClass, () => _isActive && string.IsNullOrWhiteSpace(ActiveClass));
}
/// <inheritdoc />
/// <inheritdoc />
protected override void OnParametersSet()
{
if (Match != NavLinkMatch.All && Href == "/")
{
Match = NavLinkMatch.All;
}
// Update computed state
_hrefAbsolute = Href == null ? null : NavigationManger.ToAbsoluteUri(Href).AbsoluteUri;
if (MenuItem.FirstRun)
{
_isActive = MenuHelper.ShouldMatch(Match, NavigationManger.Uri, _hrefAbsolute);
if (MenuItem != null && _isActive && !MenuItem.IsSelected)
{
Menu?.SelectItem(MenuItem);
Menu?.SelectSubmenu(MenuItem.ParentMenu);
}
}
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
// To avoid leaking memory, it's important to detach any event handlers in Dispose()
NavigationManger.LocationChanged -= OnLocationChanged;
base.Dispose(disposing);
}
private void OnLocationChanged(object sender, LocationChangedEventArgs args)
{
// We could just re-render always, but for this component we know the
// only relevant state change is to the _isActive property.
bool shouldBeActiveNow = MenuHelper.ShouldMatch(Match, args.Location, _hrefAbsolute);
if (shouldBeActiveNow != _isActive)
{
_isActive = shouldBeActiveNow;
if (MenuItem != null)
{
if (_isActive && !MenuItem.IsSelected)
{
Menu.SelectItem(MenuItem);
}
else if (!_isActive && MenuItem.IsSelected)
{
MenuItem.Deselect();
}
}
Menu.MarkStateHasChanged();
}
}
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
if (builder != null)
{
builder.OpenElement(0, "a");
builder.AddAttribute(1, "href", Href);
builder.AddAttribute(2, "class", ClassMapper.Class);
builder.AddAttribute(3, "style", Style);
builder.SetKey(MenuItem.Key);
builder.AddMultipleAttributes(5, Attributes);
builder.AddContent(6, ChildContent);
builder.CloseElement();
}
}
}
}