Skip to content

Commit

Permalink
feat: add OnMenuItemClicked,SelectedKeys,SelectedKeysChanged (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderJames authored Mar 27, 2024
1 parent aa95cb1 commit ae5337d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Theme="MenuTheme.Light"
MenuAccordion
@bind-OpenKeys="openKeys"
@bind-Collapsed="collapsed">
@bind-Collapsed="collapsed"
@bind-SelectedKeys=selectedKeys>
<HeaderContentRender>
<Menu Mode="MenuMode.Horizontal" Theme="MenuTheme.Dark">
<MenuItem OnClick="ResetMenu">Refresh Menu</MenuItem>
Expand Down Expand Up @@ -49,6 +50,7 @@

private bool collapsed;
private string[] openKeys = [];
private string[] selectedKeys = ["welcome"];

protected override async Task OnInitializedAsync()
{
Expand All @@ -57,7 +59,7 @@
data[0].IconFont = "icon-tuichu";
data.Insert(0, new MenuDataItem
{
TitleTemplate =@<span><Icon Type="user"></Icon> Custom Menu </span>,
TitleTemplate =@<span><Icon Type="user"></Icon><span> Custom Menu </span</span>,
});
MenuData = data.ToArray();
}
Expand Down
7 changes: 6 additions & 1 deletion src/layout/src/BasicLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
SiderWidth="SiderWidth"
Accordion="MenuAccordion"
OnCollapse="HandleCollapse"
OpenKeysChanged="OpenKeysChanged">
OpenKeysChanged="OpenKeysChanged"
OnMenuHeaderClick="@OnMenuHeaderClick"
OnMenuItemClicked="OnMenuItemClicked"
SelectedKeys="SelectedKeys"
SelectedKeysChanged="SelectedKeysChanged"
>
</SiderMenu>
</CascadingValue>
</CascadingValue>
Expand Down
7 changes: 4 additions & 3 deletions src/layout/src/BasicLayout.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ public partial class BasicLayout : IBasicLayout
[Parameter] public string ColSize { get; set; } = "lg";
[Parameter] public RenderFragment RightContentRender { get; set; }
[Parameter] public RenderFragment HeaderContentRender { get; set; }
[Inject] public ILogger<BasicLayout> Logger { get; set; }
[Parameter] public string[] SelectedKeys { get; set; }
[Parameter] public EventCallback<string[]> SelectedKeysChanged { get; set; }
[Parameter] public EventCallback<MenuItem> OnMenuItemClicked { get; set; }

protected override void OnInitialized()
{
Logger.LogInformation("BasicLayout initialized.");
base.OnInitialized();
SetStyle();
SetClassMap();
Expand All @@ -80,7 +81,7 @@ protected void SetStyle()
{
var hasLeftPadding = FixSiderbar && Layout != Layout.Top && !IsMobile;
var paddingLeft = hasLeftPadding ? Collapsed ? 48 : SiderWidth : 0;
Logger.LogInformation($"HasLeftPadding: {hasLeftPadding}, Collapsed: {Collapsed}, Padding: {paddingLeft}");

_genLayoutStyle = MenuRender ? $"padding-left: {paddingLeft}px; position: relative;" : "";
_weakModeStyle = ColorWeak ? "filter: invert(80%);" : "";
}
Expand Down
5 changes: 4 additions & 1 deletion src/layout/src/SiderMenu/BaseMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
Style="@Style"
Accordion="@Accordion"
Class="@ClassMapper.Class"
OpenKeysChanged="OpenKeysChanged">
OpenKeysChanged="OpenKeysChanged"
OnMenuItemClicked="OnMenuItemClicked"
SelectedKeys="SelectedKeys"
SelectedKeysChanged="SelectedKeysChanged">
@menuItemTemplate(MenuData, false)
</Menu>

Expand Down
16 changes: 11 additions & 5 deletions src/layout/src/SiderMenu/BaseMenu.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ internal interface IBaseMenu : IPureSettings
EventCallback<bool> OnCollapse { get; }
string[] OpenKeys { get; }
EventCallback<string[]> OpenKeysChanged { get; set; }

string[] SelectedKeys { get; }
EventCallback<string[]> SelectedKeysChanged { get; set; }

EventCallback<MenuItem> OnMenuItemClicked { get; set; }
}

public partial class BaseMenu : IBaseMenu
Expand All @@ -28,11 +33,12 @@ public partial class BaseMenu : IBaseMenu

[Parameter] public bool Accordion { get; set; }

[Inject] public ILogger<BaseMenu> Logger { get; set; }
[Parameter] public EventCallback<MenuItem> OnMenuItemClicked { get; set; }

[Parameter]
public string[] SelectedKeys { get; set; } = [];

protected override void OnInitialized()
{
Logger.LogInformation("BaseMenu initialized.");
}
[Parameter]
public EventCallback<string[]> SelectedKeysChanged { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/layout/src/SiderMenu/SiderMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
OpenKeys="OpenKeys"
Accordion="@Accordion"
OpenKeysChanged="OpenKeysChanged"
OnMenuItemClicked="OnMenuItemClicked"
SelectedKeys="SelectedKeys"
SelectedKeysChanged="SelectedKeysChanged"
Style="@("width: '100%'")" />
</div>

Expand Down
9 changes: 9 additions & 0 deletions src/layout/src/SiderMenu/SiderMenu.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public SiderTheme SiderTheme
set => NavTheme = value == SiderTheme.Light ? MenuTheme.Light : MenuTheme.Dark;
}

[Parameter]
public string[] SelectedKeys { get; set; }

[Parameter]
public EventCallback<string[]> SelectedKeysChanged { get; set; }

[Parameter]
public EventCallback<MenuItem> OnMenuItemClicked { get; set; }

private async Task HandleOnCollapse(bool collapsed)
{
if (!IsMobile && OnCollapse.HasDelegate)
Expand Down
3 changes: 3 additions & 0 deletions src/layout/src/TopNavHeader/TopNavHeader.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public partial class TopNavHeader : ITopNavHeader
[Parameter] public bool Hide { get; set; }
[Parameter] public List<RenderFragment> Links { get; set; }
[Parameter] public EventCallback<string[]> OpenKeysChanged { get; set; }
[Parameter] public string[] SelectedKeys { get; set; }
[Parameter] public EventCallback<string[]> SelectedKeysChanged { get; set; }
[Parameter] public EventCallback<MenuItem> OnMenuItemClicked { get; set; }

protected override void OnInitialized()
{
Expand Down

0 comments on commit ae5337d

Please sign in to comment.