From ecaedd65e4d6929e12a4215bd1ac6eefecf9f814 Mon Sep 17 00:00:00 2001 From: Denis Voituron Date: Fri, 11 Oct 2024 15:47:05 +0200 Subject: [PATCH] Check when the FluentMenuProvider is included (#2793) --- .../Microsoft.FluentUI.AspNetCore.Components.xml | 13 +++++++++++++ src/Core/Components/Menu/FluentMenu.razor.cs | 5 +++++ .../Components/Menu/FluentMenuProvider.razor.cs | 7 +++++++ src/Core/Components/Menu/Services/IMenuService.cs | 5 +++++ src/Core/Components/Menu/Services/MenuService.cs | 5 +++++ 5 files changed, 35 insertions(+) diff --git a/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml b/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml index 8355e338cc..a46bbfc4e3 100644 --- a/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml +++ b/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml @@ -6319,6 +6319,9 @@ + + + @@ -6344,6 +6347,11 @@ Action to be called when the menu is updated. + + + Gets or sets the FluentMenuProvider ID. + + Gets the list of menus. @@ -6383,6 +6391,11 @@ + + + + + diff --git a/src/Core/Components/Menu/FluentMenu.razor.cs b/src/Core/Components/Menu/FluentMenu.razor.cs index 03aa3413e9..36488f7b2d 100644 --- a/src/Core/Components/Menu/FluentMenu.razor.cs +++ b/src/Core/Components/Menu/FluentMenu.razor.cs @@ -183,6 +183,11 @@ protected override void OnInitialized() _menuService = ServiceProvider?.GetService(); if (MenuService != null && DrawMenuWithService) { + if (string.IsNullOrEmpty(MenuService.ProviderId)) + { + throw new ArgumentNullException(nameof(UseMenuService), " needs to be added to the main layout of your application/site."); + } + MenuService.Add(this); } diff --git a/src/Core/Components/Menu/FluentMenuProvider.razor.cs b/src/Core/Components/Menu/FluentMenuProvider.razor.cs index 60d247e277..471e16918e 100644 --- a/src/Core/Components/Menu/FluentMenuProvider.razor.cs +++ b/src/Core/Components/Menu/FluentMenuProvider.razor.cs @@ -12,6 +12,12 @@ public partial class FluentMenuProvider : FluentComponentBase { private IMenuService? _menuService = null; + /// + public FluentMenuProvider() + { + Id = Identifier.NewId(); + } + /// internal string? ClassValue => new CssBuilder(Class) .AddClass("fluent-menu-provider") @@ -42,6 +48,7 @@ protected override void OnInitialized() if (MenuService != null) { + MenuService.ProviderId = Id; MenuService.OnMenuUpdated = () => InvokeAsync(StateHasChanged); } } diff --git a/src/Core/Components/Menu/Services/IMenuService.cs b/src/Core/Components/Menu/Services/IMenuService.cs index e8ea445066..93c0198405 100644 --- a/src/Core/Components/Menu/Services/IMenuService.cs +++ b/src/Core/Components/Menu/Services/IMenuService.cs @@ -11,6 +11,11 @@ public interface IMenuService : IDisposable /// Action OnMenuUpdated { get; set; } + /// + /// Gets or sets the FluentMenuProvider ID. + /// + public string? ProviderId { get; set; } + /// /// Gets the list of menus. /// diff --git a/src/Core/Components/Menu/Services/MenuService.cs b/src/Core/Components/Menu/Services/MenuService.cs index 957feb0d97..2de36c747c 100644 --- a/src/Core/Components/Menu/Services/MenuService.cs +++ b/src/Core/Components/Menu/Services/MenuService.cs @@ -34,6 +34,11 @@ public IEnumerable Menus } } + /// + /// + /// + public string? ProviderId { get; set; } + /// /// ///