ant-design-blazor/components/menu/MenuItem.razor.cs

128 lines
3.1 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
2019-12-09 00:25:22 +08:00
namespace AntDesign
2019-12-09 00:25:22 +08:00
{
public partial class MenuItem : AntDomComponentBase
2019-12-09 00:25:22 +08:00
{
[CascadingParameter]
public Menu RootMenu { get; set; }
[CascadingParameter]
public SubMenu ParentMenu { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter]
public string Key
{
get => _key ?? Id;
set => _key = value;
}
[Parameter]
public bool Disabled { get; set; }
[Parameter]
public EventCallback<MouseEventArgs> OnClick { get; set; }
[Parameter]
public string RouterLink { get; set; }
[Parameter]
public NavLinkMatch RouterMatch { get; set; }
2019-12-09 00:25:22 +08:00
[Parameter]
public string Title { get; set; }
internal bool IsSelected { get; private set; }
internal bool FirstRun { get; set; } = true;
private string _key;
2019-12-09 00:25:22 +08:00
private int PaddingLeft => RootMenu.InternalMode == MenuMode.Inline ? ((ParentMenu?.Level ?? 0) + 1) * 24 : 0;
private void SetClass()
{
string prefixCls = $"{RootMenu.PrefixCls}-item";
2019-12-09 00:25:22 +08:00
ClassMapper.Add(prefixCls)
.If($"{prefixCls}-selected", () => IsSelected)
.If($"{prefixCls}-disabled", () => Disabled);
}
2019-12-09 00:25:22 +08:00
protected override void Dispose(bool disposing)
{
RootMenu.MenuItems.Remove(this);
base.Dispose(disposing);
}
protected override void OnInitialized()
{
base.OnInitialized();
2019-12-09 00:25:22 +08:00
SetClass();
RootMenu.MenuItems.Add(this);
if (RootMenu.DefaultSelectedKeys.Contains(Key))
Select();
}
protected override void OnParametersSet()
{
base.OnParametersSet();
if (RootMenu.SelectedKeys.Contains(Key) && !IsSelected)
Select();
2019-12-09 00:25:22 +08:00
}
public async Task HandleOnClick(MouseEventArgs args)
{
if (Disabled)
return;
if (OnClick.HasDelegate)
await OnClick.InvokeAsync(args);
if (!RootMenu.Selectable)
return;
if (IsSelected && RootMenu?.Multiple == true)
{
Deselect();
}
else
{
RootMenu.SelectItem(this);
}
if (ParentMenu == null)
return;
2019-12-09 00:25:22 +08:00
if (RootMenu.Mode != MenuMode.Inline)
2019-12-09 00:25:22 +08:00
{
await ParentMenu?.Collapse();
2019-12-09 00:25:22 +08:00
}
}
2019-12-09 00:25:22 +08:00
public void Select()
{
IsSelected = true;
FirstRun = false;
ParentMenu?.Select();
}
public void Deselect()
{
IsSelected = false;
FirstRun = false;
ParentMenu?.Deselect();
2019-12-09 00:25:22 +08:00
}
}
}