From ab608c2e02000a72c833f11a81a6b1015b0bceff Mon Sep 17 00:00:00 2001 From: Artyom M Date: Mon, 25 Mar 2024 11:55:17 +0200 Subject: [PATCH] MudExpansionPanel: Use ParameterState, remove obsolete API and change to async API (#8446) --- .../ExpansionPanelSimpleExample.razor | 2 +- ...tipleWithoutMultipleExpansionSetTest.razor | 1 - .../ExpansionPanelExpansionsTest.razor | 3 - .../ExpansionPanelMultiExpansionTest.razor | 3 - ...ansionPanelStartExpandedMultipleTest.razor | 9 +- .../ExpansionPanelStartExpandedTest.razor | 12 +- .../ExpansionPanelTwoWayBIndingTest.razor | 46 +++++ .../Components/ExpansionPanelTests.cs | 164 +++++++++++++----- .../ExpansionPanel/MudExpansionPanel.razor | 6 +- .../ExpansionPanel/MudExpansionPanel.razor.cs | 104 +++++------ .../MudExpansionPanels.razor.cs | 51 +++--- 11 files changed, 250 insertions(+), 151 deletions(-) create mode 100644 src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelTwoWayBIndingTest.razor diff --git a/src/MudBlazor.Docs/Pages/Components/ExpansionPanel/Examples/ExpansionPanelSimpleExample.razor b/src/MudBlazor.Docs/Pages/Components/ExpansionPanel/Examples/ExpansionPanelSimpleExample.razor index 21a351de12d9..420d906cdbaa 100644 --- a/src/MudBlazor.Docs/Pages/Components/ExpansionPanel/Examples/ExpansionPanelSimpleExample.razor +++ b/src/MudBlazor.Docs/Pages/Components/ExpansionPanel/Examples/ExpansionPanelSimpleExample.razor @@ -2,7 +2,7 @@ - + Panel One Content diff --git a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelExpandedMultipleWithoutMultipleExpansionSetTest.razor b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelExpandedMultipleWithoutMultipleExpansionSetTest.razor index bce4be908509..f68fb4d8e880 100644 --- a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelExpandedMultipleWithoutMultipleExpansionSetTest.razor +++ b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelExpandedMultipleWithoutMultipleExpansionSetTest.razor @@ -8,4 +8,3 @@ Panel Two Content - diff --git a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelExpansionsTest.razor b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelExpansionsTest.razor index ff60a2c2abde..9be2623f793c 100644 --- a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelExpansionsTest.razor +++ b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelExpansionsTest.razor @@ -11,6 +11,3 @@ Panel Three Content - - - diff --git a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelMultiExpansionTest.razor b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelMultiExpansionTest.razor index 8990f27f618e..cd402937e9bb 100644 --- a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelMultiExpansionTest.razor +++ b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelMultiExpansionTest.razor @@ -11,6 +11,3 @@ Panel Three Content - - - diff --git a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelStartExpandedMultipleTest.razor b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelStartExpandedMultipleTest.razor index 0f04e526fc0a..d9a53f81a17b 100644 --- a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelStartExpandedMultipleTest.razor +++ b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelStartExpandedMultipleTest.razor @@ -1,16 +1,13 @@ @namespace MudBlazor.UnitTests.TestComponents - + Panel One Content - + Panel Two Content - + Panel Three Content - - - diff --git a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelStartExpandedTest.razor b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelStartExpandedTest.razor index 51658d757b57..ef5eccc48894 100644 --- a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelStartExpandedTest.razor +++ b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelStartExpandedTest.razor @@ -1,16 +1,22 @@ @namespace MudBlazor.UnitTests.TestComponents - + Panel One Content - + Panel Two Content - + Panel Three Content +@code +{ + public bool Panel1IsExpanded { get; set; } + public bool Panel2IsExpanded { get; set; } = true; + public bool Panel3IsExpanded { get; set; } +} diff --git a/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelTwoWayBIndingTest.razor b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelTwoWayBIndingTest.razor new file mode 100644 index 000000000000..48c36398e7c0 --- /dev/null +++ b/src/MudBlazor.UnitTests.Viewer/TestComponents/ExpansionPanel/ExpansionPanelTwoWayBIndingTest.razor @@ -0,0 +1,46 @@ +@namespace MudBlazor.UnitTests.TestComponents + + + + Panel One Content + + + Panel Two Content + + + Panel Three Content + + + Panel Four Content + + + +@code { + public bool[] Expansion = [true, false, false, false]; + + public void ToggleExpansion1() + { + // Simulating MudSwitch toggle, we need StateHasChanged. + // Usually it would be updated from an EventCallback (or bind syntax) that updates the UI automatically but we doing it directly. + Expansion[0] = !Expansion[0]; + StateHasChanged(); + } + + public void ToggleExpansion2() + { + Expansion[1] = !Expansion[1]; + StateHasChanged(); + } + + public void ToggleExpansion3() + { + Expansion[2] = !Expansion[2]; + StateHasChanged(); + } + + public void ToggleExpansion4() + { + Expansion[3] = !Expansion[3]; + StateHasChanged(); + } +} \ No newline at end of file diff --git a/src/MudBlazor.UnitTests/Components/ExpansionPanelTests.cs b/src/MudBlazor.UnitTests/Components/ExpansionPanelTests.cs index 86d273510bc0..8caeff6ba78c 100644 --- a/src/MudBlazor.UnitTests/Components/ExpansionPanelTests.cs +++ b/src/MudBlazor.UnitTests/Components/ExpansionPanelTests.cs @@ -143,85 +143,157 @@ public async Task MudExpansionPanel_Other() { var comp = Context.RenderComponent(); var panel = comp.FindComponent(); -#pragma warning disable BL0005 - await comp.InvokeAsync(() => panel.Instance.Disabled = true); - await comp.InvokeAsync(() => panel.Instance.ToggleExpansion()); - await comp.InvokeAsync(() => panel.Instance.Expand()); - comp.WaitForAssertion(() => panel.Instance.IsExpanded.Should().BeTrue()); - await comp.InvokeAsync(() => panel.Instance.Collapse()); - comp.WaitForAssertion(() => panel.Instance.IsExpanded.Should().BeFalse()); + panel.SetParametersAndRender(parameters => parameters.Add(parameter => parameter.Disabled, true)); + + comp.WaitForAssertion(() => comp.Instance.Panel1IsExpanded.Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Panel2IsExpanded.Should().BeTrue()); + comp.WaitForAssertion(() => comp.Instance.Panel3IsExpanded.Should().BeFalse()); + await comp.InvokeAsync(panel.Instance.ToggleExpansionAsync); + comp.WaitForAssertion(() => comp.Instance.Panel1IsExpanded.Should().BeFalse()); // ToggleExpansionAsync checks for Disabled, so nothing happens + comp.WaitForAssertion(() => comp.Instance.Panel2IsExpanded.Should().BeTrue()); + comp.WaitForAssertion(() => comp.Instance.Panel3IsExpanded.Should().BeFalse()); + await comp.InvokeAsync(panel.Instance.ExpandAsync); + comp.WaitForAssertion(() => comp.Instance.Panel1IsExpanded.Should().BeTrue()); // ExpandAsync ignores Disabled + comp.WaitForAssertion(() => comp.Instance.Panel2IsExpanded.Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Panel3IsExpanded.Should().BeFalse()); + await comp.InvokeAsync(panel.Instance.CollapseAsync); + comp.WaitForAssertion(() => comp.Instance.Panel1IsExpanded.Should().BeFalse()); // ExpandAsync ignores Disabled + comp.WaitForAssertion(() => comp.Instance.Panel2IsExpanded.Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Panel3IsExpanded.Should().BeFalse()); } /// /// Tests that ExpandAll method expands all panels. /// [Test] - public void MudExpansionPanel_ExpandAll() + public async Task MudExpansionPanel_ExpandAllAync() { var panels = Context.RenderComponent(); var panel1 = new MudExpansionPanel(); var panel2 = new MudExpansionPanel(); var panel3 = new MudExpansionPanel(); - panels.Instance.AddPanel(panel1); - panels.Instance.AddPanel(panel2); - panels.Instance.AddPanel(panel3); - panel1.IsExpanded.Should().BeFalse(); - panel2.IsExpanded.Should().BeFalse(); - panel3.IsExpanded.Should().BeFalse(); - panels.Instance.ExpandAll(); - panel1.IsExpanded.Should().BeTrue(); - panel2.IsExpanded.Should().BeTrue(); - panel3.IsExpanded.Should().BeTrue(); + await panels.Instance.AddPanelAsync(panel1); + await panels.Instance.AddPanelAsync(panel2); + await panels.Instance.AddPanelAsync(panel3); + // We check _isExpandedState because we do not modify IsExpanded directly, therefore the parameter doesn't change. + // For parameter to change you need to bind panels IsExpansion it via razor syntax, so that parent would update it. + panel1._isExpandedState.Value.Should().BeFalse(); + panel2._isExpandedState.Value.Should().BeFalse(); + panel3._isExpandedState.Value.Should().BeFalse(); + await panels.Instance.ExpandAllAsync(); + panel1._isExpandedState.Value.Should().BeTrue(); + panel2._isExpandedState.Value.Should().BeTrue(); + panel3._isExpandedState.Value.Should().BeTrue(); } /// /// Tests that CollapseAll method collapses all panels. /// [Test] - public void MudExpansionPanel_CollapseAll() + public async Task MudExpansionPanel_CollapseAllAsync() { var panels = Context.RenderComponent(); var panel1 = new MudExpansionPanel(); var panel2 = new MudExpansionPanel(); var panel3 = new MudExpansionPanel(); - panels.Instance.AddPanel(panel1); - panels.Instance.AddPanel(panel2); - panels.Instance.AddPanel(panel3); - panel1.Expand(false); - panel2.Expand(false); - panel3.Expand(false); - panel1.IsExpanded.Should().BeTrue(); - panel2.IsExpanded.Should().BeTrue(); - panel3.IsExpanded.Should().BeTrue(); - panels.Instance.CollapseAll(); - panel1.IsExpanded.Should().BeFalse(); - panel2.IsExpanded.Should().BeFalse(); - panel3.IsExpanded.Should().BeFalse(); + await panels.Instance.AddPanelAsync(panel1); + await panels.Instance.AddPanelAsync(panel2); + await panels.Instance.AddPanelAsync(panel3); + await panel1.ExpandAsync(); + await panel2.ExpandAsync(); + await panel3.ExpandAsync(); + // We check _isExpandedState because we do not modify IsExpanded directly, therefore the parameter doesn't change. + // For parameter to change you need to bind panels IsExpansion it via razor syntax, so that parent would update it. + panel1._isExpandedState.Value.Should().BeTrue(); + panel2._isExpandedState.Value.Should().BeTrue(); + panel3._isExpandedState.Value.Should().BeTrue(); + await panels.Instance.CollapseAllAsync(); + panel1._isExpandedState.Value.Should().BeFalse(); + panel2._isExpandedState.Value.Should().BeFalse(); + panel3._isExpandedState.Value.Should().BeFalse(); } /// /// Tests that CollapseAllExcept method collapses all panels except one. /// [Test] - public void MudExpansionPanel_CollapseAllExcept() + public async Task MudExpansionPanel_CollapseAllExceptAsync() { var panels = Context.RenderComponent(); var panel1 = new MudExpansionPanel(); var panel2 = new MudExpansionPanel(); var panel3 = new MudExpansionPanel(); - panels.Instance.AddPanel(panel1); - panels.Instance.AddPanel(panel2); - panels.Instance.AddPanel(panel3); - panel1.Expand(false); - panel2.Expand(false); - panel3.Expand(false); - panel1.IsExpanded.Should().BeTrue(); - panel2.IsExpanded.Should().BeTrue(); - panel3.IsExpanded.Should().BeTrue(); - panels.Instance.CollapseAllExcept(panel2); - panel1.IsExpanded.Should().BeFalse(); - panel2.IsExpanded.Should().BeTrue(); - panel3.IsExpanded.Should().BeFalse(); + await panels.Instance.AddPanelAsync(panel1); + await panels.Instance.AddPanelAsync(panel2); + await panels.Instance.AddPanelAsync(panel3); + await panel1.ExpandAsync(); + await panel2.ExpandAsync(); + await panel3.ExpandAsync(); + // We check _isExpandedState because we do not modify IsExpanded directly, therefore the parameter doesn't change. + // For parameter to change you need to bind panels IsExpansion it via razor syntax, so that parent would update it. + panel1._isExpandedState.Value.Should().BeTrue(); + panel2._isExpandedState.Value.Should().BeTrue(); + panel3._isExpandedState.Value.Should().BeTrue(); + await panels.Instance.CollapseAllExceptAsync(panel2); + panel1._isExpandedState.Value.Should().BeFalse(); + panel2._isExpandedState.Value.Should().BeTrue(); + panel3._isExpandedState.Value.Should().BeFalse(); + } + + /// + /// Test for https://github.com/MudBlazor/MudBlazor/issues/8429 + /// + [Test] + public async Task MudExpansionPanel_TwoWayBinding() + { + var comp = Context.RenderComponent(); + + comp.WaitForAssertion(() => comp.Instance.Expansion[0].Should().BeTrue()); + comp.WaitForAssertion(() => comp.Instance.Expansion[1].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[2].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[3].Should().BeFalse()); + + await comp.InvokeAsync(comp.Instance.ToggleExpansion2); + + comp.WaitForAssertion(() => comp.Instance.Expansion[0].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[1].Should().BeTrue()); + comp.WaitForAssertion(() => comp.Instance.Expansion[2].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[3].Should().BeFalse()); + + await comp.InvokeAsync(comp.Instance.ToggleExpansion3); + + comp.WaitForAssertion(() => comp.Instance.Expansion[0].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[1].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[2].Should().BeTrue()); + comp.WaitForAssertion(() => comp.Instance.Expansion[3].Should().BeFalse()); + + await comp.InvokeAsync(comp.Instance.ToggleExpansion4); + + comp.WaitForAssertion(() => comp.Instance.Expansion[0].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[1].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[2].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[3].Should().BeTrue()); + + await comp.InvokeAsync(comp.Instance.ToggleExpansion3); + + comp.WaitForAssertion(() => comp.Instance.Expansion[0].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[1].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[2].Should().BeTrue()); + comp.WaitForAssertion(() => comp.Instance.Expansion[3].Should().BeFalse()); + + await comp.InvokeAsync(comp.Instance.ToggleExpansion2); + + comp.WaitForAssertion(() => comp.Instance.Expansion[0].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[1].Should().BeTrue()); + comp.WaitForAssertion(() => comp.Instance.Expansion[2].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[3].Should().BeFalse()); + + await comp.InvokeAsync(comp.Instance.ToggleExpansion1); + + comp.WaitForAssertion(() => comp.Instance.Expansion[0].Should().BeTrue()); + comp.WaitForAssertion(() => comp.Instance.Expansion[1].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[2].Should().BeFalse()); + comp.WaitForAssertion(() => comp.Instance.Expansion[3].Should().BeFalse()); } } } diff --git a/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanel.razor b/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanel.razor index 1a21a73b15c9..7e8be8ccf606 100644 --- a/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanel.razor +++ b/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanel.razor @@ -2,7 +2,7 @@ @inherits MudComponentBase
-
+
@if (TitleContent is not null) { @@ -15,10 +15,10 @@
@if (!HideIcon) { - + }
- +
@ChildContent
diff --git a/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanel.razor.cs b/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanel.razor.cs index ace093b69ec7..0da406ca2223 100644 --- a/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanel.razor.cs +++ b/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanel.razor.cs @@ -1,5 +1,7 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Components; +using MudBlazor.State; using MudBlazor.Utilities; namespace MudBlazor @@ -7,15 +9,14 @@ namespace MudBlazor #nullable enable public partial class MudExpansionPanel : MudComponentBase, IDisposable { - private bool _isExpanded; - private bool _collapseIsExpanded; + internal IParameterState _isExpandedState; [CascadingParameter] private MudExpansionPanels? Parent { get; set; } protected string Classname => new CssBuilder("mud-expand-panel") - .AddClass("mud-panel-expanded", IsExpanded) + .AddClass("mud-panel-expanded", _isExpandedState.Value) .AddClass("mud-panel-next-expanded", NextPanelExpanded) .AddClass("mud-disabled", Disabled) .AddClass($"mud-elevation-{Parent?.Elevation.ToString()}") @@ -84,40 +85,12 @@ public partial class MudExpansionPanel : MudComponentBase, IDisposable [Parameter] public EventCallback IsExpandedChanged { get; set; } - internal event Action? NotifyIsExpandedChanged; /// /// Expansion state of the panel (two-way bindable) /// [Parameter] [Category(CategoryTypes.ExpansionPanel.Behavior)] - public bool IsExpanded - { - get => _isExpanded; - set - { - if (_isExpanded == value) - return; - _isExpanded = value; - - NotifyIsExpandedChanged?.Invoke(this); - IsExpandedChanged.InvokeAsync(_isExpanded).ContinueWith(t => - { - if (_collapseIsExpanded != _isExpanded) - { - _collapseIsExpanded = _isExpanded; - InvokeAsync(StateHasChanged); - } - }); - } - } - - /// - /// Sets the initial expansion state. Do not use in combination with IsExpanded. - /// Combine with MultiExpansion to have more than one panel start open. - /// - [Parameter] - [Category(CategoryTypes.ExpansionPanel.Behavior)] - public bool IsInitiallyExpanded { get; set; } + public bool IsExpanded { get; set; } /// /// If true, the component will be disabled. @@ -135,58 +108,77 @@ public bool IsExpanded public bool NextPanelExpanded { get; set; } - public void ToggleExpansion() + public MudExpansionPanel() + { + _isExpandedState = RegisterParameter(nameof(IsExpanded), () => IsExpanded, () => IsExpandedChanged, IsExpandedParameterChangedAsync); + } + + private Task IsExpandedParameterChangedAsync(ParameterChangedEventArgs args) + { + if (Parent is null) + { + return Task.CompletedTask; + } + + return Parent.NotifyPanelsChanged(this); + } + + public async Task ToggleExpansionAsync() { if (Disabled) { return; } - IsExpanded = !IsExpanded; + await _isExpandedState.SetValueAsync(!_isExpandedState.Value); + if (Parent is not null) + { + await Parent.NotifyPanelsChanged(this); + } } - public void Expand(bool updateParent = true) + public async Task ExpandAsync() { - if (updateParent) - IsExpanded = true; - else + await _isExpandedState.SetValueAsync(true); + if (Parent is not null) { - _isExpanded = true; - _collapseIsExpanded = true; - IsExpandedChanged.InvokeAsync(_isExpanded); + await Parent.NotifyPanelsChanged(this); } } - public void Collapse(bool updateParent = true) + public async Task CollapseAsync() { - if (updateParent) - IsExpanded = false; - else + await _isExpandedState.SetValueAsync(false); + if (Parent is not null) { - _isExpanded = false; - _collapseIsExpanded = false; - IsExpandedChanged.InvokeAsync(_isExpanded); + await Parent.NotifyPanelsChanged(this); } } - protected override void OnInitialized() + protected override async Task OnInitializedAsync() { // NOTE: we can't throw here because we need to be able to instantiate the type for the API Docs to infer default values //if (Parent == null) // throw new ArgumentNullException(nameof(Parent), "ExpansionPanel must exist within a ExpansionPanels component"); - base.OnInitialized(); - if (!IsExpanded && IsInitiallyExpanded) + await base.OnInitializedAsync(); + if (Parent is not null) { - _isExpanded = true; - _collapseIsExpanded = true; + await Parent.AddPanelAsync(this); } - - Parent?.AddPanel(this); } public void Dispose() { - Parent?.RemovePanel(this); + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + Parent?.RemovePanel(this); + } } } } diff --git a/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanels.razor.cs b/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanels.razor.cs index b4cef49dc3c8..a1532a77e6e5 100644 --- a/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanels.razor.cs +++ b/src/MudBlazor/Components/ExpansionPanel/MudExpansionPanels.razor.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using MudBlazor.Utilities; @@ -14,7 +14,7 @@ public partial class MudExpansionPanels : MudComponentBase protected string Classname => new CssBuilder("mud-expansion-panels") - .AddClass($"mud-expansion-panels-square", Square) + .AddClass("mud-expansion-panels-square", Square) .AddClass(Class) .Build(); @@ -67,20 +67,18 @@ public partial class MudExpansionPanels : MudComponentBase [Category(CategoryTypes.ExpansionPanel.Behavior)] public RenderFragment? ChildContent { get; set; } - internal void AddPanel(MudExpansionPanel panel) + internal async Task AddPanelAsync(MudExpansionPanel panel) { - if (!MultiExpansion && _panels.Any(p => p.IsExpanded)) + if (!MultiExpansion && _panels.Any(p => p._isExpandedState.Value)) { - panel.Collapse(updateParent: false); + await panel.CollapseAsync(); } - panel.NotifyIsExpandedChanged += UpdatePanelsOnPanelsChanged; _panels.Add(panel); } - public void RemovePanel(MudExpansionPanel panel) + internal void RemovePanel(MudExpansionPanel panel) { - panel.NotifyIsExpandedChanged -= UpdatePanelsOnPanelsChanged; _panels.Remove(panel); try { @@ -89,44 +87,39 @@ public void RemovePanel(MudExpansionPanel panel) catch (InvalidOperationException) { /* this happens on page reload, probably a Blazor bug */ } } - internal void UpdatePanelsOnPanelsChanged(MudExpansionPanel panel) + internal async Task NotifyPanelsChanged(MudExpansionPanel panel) { - if (!MultiExpansion && panel.IsExpanded) + if (!MultiExpansion && panel._isExpandedState.Value) { - CollapseAllExcept(panel); + await CollapseAllExceptAsync(panel); return; } - UpdateAll(); + await UpdateAllAsync(); } - public void UpdateAll() + public Task UpdateAllAsync() { MudExpansionPanel? last = null; foreach (var panel in _panels) { if (last is not null) { - last.NextPanelExpanded = panel.IsExpanded; + last.NextPanelExpanded = panel._isExpandedState.Value; } last = panel; } StateHasChanged(); - } - [Obsolete("Use CollapseAllExcept instead.")] - [ExcludeFromCodeCoverage] - public void CloseAllExcept(MudExpansionPanel panel) - { - CollapseAllExcept(panel); + return Task.CompletedTask; } /// /// Collapses all panels except the given one. /// /// The panel not to collapse. - public void CollapseAllExcept(MudExpansionPanel panel) + public async Task CollapseAllExceptAsync(MudExpansionPanel panel) { foreach (var expansionPanel in _panels) { @@ -135,33 +128,33 @@ public void CollapseAllExcept(MudExpansionPanel panel) continue; } - expansionPanel.Collapse(updateParent: false); + await expansionPanel.CollapseAsync(); } - InvokeAsync(UpdateAll); + await InvokeAsync(UpdateAllAsync); } /// /// Collapses all panels. /// - public void CollapseAll() + public async Task CollapseAllAsync() { foreach (var expansionPanel in _panels) { - expansionPanel.Collapse(updateParent: false); + await expansionPanel.CollapseAsync(); } - InvokeAsync(UpdateAll); + await InvokeAsync(UpdateAllAsync); } /// /// Expands all panels. /// - public void ExpandAll() + public async Task ExpandAllAsync() { foreach (var expansionPanel in _panels) { - expansionPanel.Expand(updateParent: false); + await expansionPanel.ExpandAsync(); } - InvokeAsync(UpdateAll); + await InvokeAsync(UpdateAllAsync); } } }