Skip to content

Commit

Permalink
Check when the FluentMenuProvider is included (#2793)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoituron authored Oct 11, 2024
1 parent cfe5163 commit ecaedd6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6319,6 +6319,9 @@
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentMenuItem.OnClickHandlerAsync(Microsoft.AspNetCore.Components.Web.MouseEventArgs)">
<summary />
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentMenuProvider.#ctor">
<summary />
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentMenuProvider.ClassValue">
<summary />
</member>
Expand All @@ -6344,6 +6347,11 @@
Action to be called when the menu is updated.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.IMenuService.ProviderId">
<summary>
Gets or sets the FluentMenuProvider ID.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.IMenuService.Menus">
<summary>
Gets the list of menus.
Expand Down Expand Up @@ -6383,6 +6391,11 @@
<see cref="P:Microsoft.FluentUI.AspNetCore.Components.IMenuService.Menus" />
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.MenuService.ProviderId">
<summary>
<see cref="P:Microsoft.FluentUI.AspNetCore.Components.IMenuService.ProviderId" />
</summary>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.MenuService.Add(Microsoft.FluentUI.AspNetCore.Components.FluentMenu)">
<summary>
<see cref="M:Microsoft.FluentUI.AspNetCore.Components.IMenuService.Add(Microsoft.FluentUI.AspNetCore.Components.FluentMenu)" />
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Components/Menu/FluentMenu.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ protected override void OnInitialized()
_menuService = ServiceProvider?.GetService<IMenuService>();
if (MenuService != null && DrawMenuWithService)
{
if (string.IsNullOrEmpty(MenuService.ProviderId))
{
throw new ArgumentNullException(nameof(UseMenuService), "<FluentMenuProvider /> needs to be added to the main layout of your application/site.");
}

MenuService.Add(this);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Core/Components/Menu/FluentMenuProvider.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public partial class FluentMenuProvider : FluentComponentBase
{
private IMenuService? _menuService = null;

/// <summary />
public FluentMenuProvider()
{
Id = Identifier.NewId();
}

/// <summary />
internal string? ClassValue => new CssBuilder(Class)
.AddClass("fluent-menu-provider")
Expand Down Expand Up @@ -42,6 +48,7 @@ protected override void OnInitialized()

if (MenuService != null)
{
MenuService.ProviderId = Id;
MenuService.OnMenuUpdated = () => InvokeAsync(StateHasChanged);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Components/Menu/Services/IMenuService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public interface IMenuService : IDisposable
/// </summary>
Action OnMenuUpdated { get; set; }

/// <summary>
/// Gets or sets the FluentMenuProvider ID.
/// </summary>
public string? ProviderId { get; set; }

/// <summary>
/// Gets the list of menus.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Components/Menu/Services/MenuService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public IEnumerable<FluentMenu> Menus
}
}

/// <summary>
/// <see cref="IMenuService.ProviderId" />
/// </summary>
public string? ProviderId { get; set; }

/// <summary>
/// <see cref="IMenuService.Add(FluentMenu)" />
/// </summary>
Expand Down

0 comments on commit ecaedd6

Please sign in to comment.