2020-05-10 15:42:02 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2019-12-10 15:03:50 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-07-14 22:32:12 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components.Routing;
|
2019-12-09 00:25:22 +08:00
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2019-12-09 00:25:22 +08:00
|
|
|
|
{
|
2020-05-18 14:46:42 +08:00
|
|
|
|
public partial class MenuItem : AntDomComponentBase
|
2019-12-09 00:25:22 +08:00
|
|
|
|
{
|
2020-05-18 14:46:42 +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]
|
2020-07-14 22:32:12 +08:00
|
|
|
|
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
|
|
|
|
|
2020-11-16 12:57:42 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Title { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool IsSelected { get; private set; }
|
2020-05-18 14:46:42 +08:00
|
|
|
|
private string _key;
|
2019-12-09 00:25:22 +08:00
|
|
|
|
|
2020-06-04 14:48:13 +08:00
|
|
|
|
private int PaddingLeft => RootMenu.InternalMode == MenuMode.Inline ? ((ParentMenu?.Level ?? 0) + 1) * 24 : 0;
|
|
|
|
|
|
2020-05-10 15:42:02 +08:00
|
|
|
|
private void SetClass()
|
|
|
|
|
{
|
2020-05-18 14:46:42 +08:00
|
|
|
|
string prefixCls = $"{RootMenu.PrefixCls}-item";
|
2019-12-09 00:25:22 +08:00
|
|
|
|
|
2020-05-10 15:42:02 +08:00
|
|
|
|
ClassMapper.Add(prefixCls)
|
|
|
|
|
.If($"{prefixCls}-selected", () => IsSelected)
|
|
|
|
|
.If($"{prefixCls}-disabled", () => Disabled);
|
|
|
|
|
}
|
2019-12-09 00:25:22 +08:00
|
|
|
|
|
2020-07-21 23:48:43 +08:00
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
RootMenu.MenuItems.Remove(this);
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-10 15:42:02 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
2019-12-09 00:25:22 +08:00
|
|
|
|
|
2020-05-18 14:46:42 +08:00
|
|
|
|
SetClass();
|
|
|
|
|
|
2020-05-10 15:42:02 +08:00
|
|
|
|
RootMenu.MenuItems.Add(this);
|
2020-05-18 14:46:42 +08:00
|
|
|
|
|
|
|
|
|
if (RootMenu.DefaultSelectedKeys.Contains(Key))
|
|
|
|
|
Select();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnParametersSet()
|
|
|
|
|
{
|
|
|
|
|
base.OnParametersSet();
|
|
|
|
|
|
|
|
|
|
if (RootMenu.SelectedKeys.Contains(Key))
|
|
|
|
|
Select();
|
2019-12-09 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-10 15:42:02 +08:00
|
|
|
|
public async Task HandleOnClick(MouseEventArgs args)
|
2019-12-10 15:03:50 +08:00
|
|
|
|
{
|
2020-11-16 12:57:42 +08:00
|
|
|
|
if (Disabled)
|
2020-05-10 15:42:02 +08:00
|
|
|
|
return;
|
2019-12-10 15:03:50 +08:00
|
|
|
|
|
2020-07-14 22:32:12 +08:00
|
|
|
|
if (OnClick.HasDelegate)
|
|
|
|
|
await OnClick.InvokeAsync(args);
|
2020-05-10 15:42:02 +08:00
|
|
|
|
|
2020-11-16 12:57:42 +08:00
|
|
|
|
if (!RootMenu.Selectable)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (IsSelected && RootMenu?.Multiple == true)
|
|
|
|
|
{
|
|
|
|
|
Deselect();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RootMenu.SelectItem(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-10 15:42:02 +08:00
|
|
|
|
if (ParentMenu == null)
|
|
|
|
|
return;
|
2019-12-09 00:25:22 +08:00
|
|
|
|
|
2020-05-18 14:46:42 +08:00
|
|
|
|
if (RootMenu.Mode != MenuMode.Inline)
|
2019-12-09 00:25:22 +08:00
|
|
|
|
{
|
2020-05-10 15:42:02 +08:00
|
|
|
|
await ParentMenu?.Collapse();
|
2019-12-09 00:25:22 +08:00
|
|
|
|
}
|
2020-05-10 15:42:02 +08:00
|
|
|
|
}
|
2019-12-09 00:25:22 +08:00
|
|
|
|
|
2020-05-10 15:42:02 +08:00
|
|
|
|
public void Select()
|
|
|
|
|
{
|
|
|
|
|
IsSelected = true;
|
2020-05-19 20:35:48 +08:00
|
|
|
|
ParentMenu?.Select();
|
2020-05-10 15:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Deselect()
|
|
|
|
|
{
|
|
|
|
|
IsSelected = false;
|
2020-05-19 20:35:48 +08:00
|
|
|
|
ParentMenu?.Deselect();
|
2019-12-09 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|