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

97 lines
2.2 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System;
using System.Linq;
using System.Threading.Tasks;
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> OnClicked { get; set; }
2019-12-09 00:25:22 +08:00
public bool IsSelected { get; private set; }
private string _key;
2019-12-09 00:25:22 +08:00
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 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))
Select();
2019-12-09 00:25:22 +08:00
}
public async Task HandleOnClick(MouseEventArgs args)
{
if (!RootMenu.Selectable)
return;
RootMenu.SelectItem(this);
if (OnClicked.HasDelegate)
await OnClicked.InvokeAsync(args);
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;
ParentMenu?.Select();
}
public void Deselect()
{
IsSelected = false;
ParentMenu?.Deselect();
2019-12-09 00:25:22 +08:00
}
}
}