ant-design-blazor/components/menu/AntMenuItemBase.cs

88 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
2019-12-09 00:25:22 +08:00
namespace AntBlazor
{
public class AntMenuItemBase : AntDomComponentBase
{
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter]
2020-03-10 17:38:25 +08:00
public bool disabled { get; set; } = false;
2019-12-09 00:25:22 +08:00
[Parameter]
2020-03-10 17:38:25 +08:00
public bool selected { get; set; } = false;
2019-12-09 00:25:22 +08:00
[Parameter]
2020-03-10 17:38:25 +08:00
public int? paddingLeft { get; set; }
2019-12-09 00:25:22 +08:00
[Parameter]
2020-03-10 17:38:25 +08:00
public bool matchRouterExact { get; set; } = false;
2019-12-09 00:25:22 +08:00
[Parameter]
2020-03-10 17:38:25 +08:00
public bool matchRouter { get; set; } = false;
2019-12-09 00:25:22 +08:00
[CascadingParameter]
public AntMenu Menu { get; set; }
[CascadingParameter]
public AntSubMenu SubMenu { get; set; }
private int originalPadding;
private void SetClassMap()
{
string prefixName = Menu.isInDropDown ? "ant-dropdown-menu-item" : "ant-menu-item";
ClassMapper.Clear()
.Add(prefixName)
2020-03-10 17:38:25 +08:00
.If($"{prefixName}-selected", () => selected)
.If($"{prefixName}-disabled", () => disabled);
2019-12-09 00:25:22 +08:00
}
internal void SelectedChanged(bool value)
{
2020-03-10 17:38:25 +08:00
this.selected = value;
}
2019-12-09 00:25:22 +08:00
protected override void OnInitialized()
{
base.OnInitialized();
if (this is AntMenuItem item)
{
2019-12-12 00:14:13 +08:00
Menu?.MenuItems.Add(item);
SubMenu?.Items.Add(item);
}
if (Attributes?.TryGetValue("style", out var style) == true)
{
}
2019-12-09 00:25:22 +08:00
int? padding = null;
2020-03-10 17:38:25 +08:00
if (Menu.mode == AntDirectionVHIType.inline)
2019-12-09 00:25:22 +08:00
{
2020-03-10 17:38:25 +08:00
if (paddingLeft != null)
2019-12-09 00:25:22 +08:00
{
2020-03-10 17:38:25 +08:00
padding = paddingLeft;
2019-12-09 00:25:22 +08:00
}
else
{
int level = SubMenu?.Level + 1 ?? 1;
2020-03-10 17:38:25 +08:00
padding = level * this.Menu.inlineIndent;
2019-12-09 00:25:22 +08:00
}
}
else
{
padding = originalPadding;
}
if (padding != null)
{
Style += $"padding-left:{padding}px;";
}
SetClassMap();
}
}
}