Skip to content

Commit 80b61c9

Browse files
authored
[Tabs] Fix Overflow getting out of sync with tab collection (#3284)
* Replace Dictionary with a List and fix overflow keeping closed items. Fix #3268 * Process review comments. For now we will go with the quick fix * Remove unneccesary null negating
1 parent 46c4f02 commit 80b61c9

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/Core/Components/Tabs/FluentTab.razor.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// ------------------------------------------------------------------------
2+
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
3+
// ------------------------------------------------------------------------
4+
15
using Microsoft.AspNetCore.Components;
26
using Microsoft.FluentUI.AspNetCore.Components.Extensions;
37
using Microsoft.FluentUI.AspNetCore.Components.Utilities;
@@ -130,7 +134,7 @@ public FluentTab()
130134

131135
protected override void OnInitialized()
132136
{
133-
Index = Owner!.RegisterTab(this);
137+
Index = Owner.RegisterTab(this);
134138
}
135139

136140
/// <summary />
@@ -148,7 +152,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
148152
/// <summary />
149153
protected virtual Task CloseClickedAsync()
150154
{
151-
return Owner!.UnregisterTabAsync(this);
155+
if (Id is null)
156+
{
157+
return Task.CompletedTask;
158+
}
159+
return Owner.UnregisterTabAsync(Id);
152160
}
153161

154162
/// <summary />

src/Core/Components/Tabs/FluentTabs.razor.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
33
// ------------------------------------------------------------------------
44

5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Text.Json;
57
using Microsoft.AspNetCore.Components;
68
using Microsoft.FluentUI.AspNetCore.Components.Extensions;
79
using Microsoft.FluentUI.AspNetCore.Components.Utilities;
810
using Microsoft.JSInterop;
9-
using System.Diagnostics.CodeAnalysis;
10-
using System.Text.Json;
1111

1212
namespace Microsoft.FluentUI.AspNetCore.Components;
1313

@@ -16,7 +16,7 @@ public partial class FluentTabs : FluentComponentBase
1616
private const string JAVASCRIPT_FILE = "./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Overflow/FluentOverflow.razor.js";
1717

1818
private const string FLUENT_TAB_TAG = "fluent-tab";
19-
private readonly Dictionary<string, FluentTab> _tabs = [];
19+
private readonly List<FluentTab> _tabs = [];
2020
//private string _activeId = string.Empty;
2121
private DotNetObjectReference<FluentTabs>? _dotNetHelper = null;
2222
private IJSObjectReference _jsModuleOverflow = default!;
@@ -98,7 +98,7 @@ public partial class FluentTabs : FluentComponentBase
9898
/// <summary>
9999
/// Gets the active selected tab.
100100
/// </summary>
101-
public FluentTab ActiveTab => _tabs.FirstOrDefault(i => i.Key == ActiveTabId).Value ?? _tabs.First().Value;
101+
public FluentTab ActiveTab => _tabs.FirstOrDefault(t => t.Id == ActiveTabId) ?? _tabs.First();
102102

103103
[Parameter]
104104
public string ActiveTabId { get; set; } = default!;
@@ -135,7 +135,7 @@ public partial class FluentTabs : FluentComponentBase
135135
/// <summary>
136136
/// Gets all tabs with <see cref="FluentTab.Overflow"/> assigned to True.
137137
/// </summary>
138-
public IEnumerable<FluentTab> TabsOverflow => _tabs.Where(i => i.Value.Overflow == true).Select(v => v.Value);
138+
public IEnumerable<FluentTab> TabsOverflow => _tabs.Where(i => i.Overflow == true);
139139

140140
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(TabChangeEventArgs))]
141141

@@ -154,41 +154,47 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
154154
_jsModuleOverflow = await JSRuntime.InvokeAsync<IJSObjectReference>("import", JAVASCRIPT_FILE.FormatCollocatedUrl(LibraryConfiguration));
155155

156156
var horizontal = Orientation == Orientation.Horizontal;
157-
await _jsModuleOverflow.InvokeVoidAsync("fluentOverflowInitialize", _dotNetHelper, Id, horizontal, FLUENT_TAB_TAG,25);
157+
await _jsModuleOverflow.InvokeVoidAsync("fluentOverflowInitialize", _dotNetHelper, Id, horizontal, FLUENT_TAB_TAG, 25);
158158
}
159159
}
160160

161161
private async Task HandleOnTabChangedAsync(TabChangeEventArgs args)
162162
{
163163
var tabId = args?.ActiveId;
164-
if (tabId is not null && _tabs.TryGetValue(tabId, out FluentTab? tab))
164+
var tab = _tabs.FirstOrDefault(i => i.Id == tabId);
165+
166+
if (tab is not null && _tabs.Contains(tab))
165167
{
166168
await OnTabChange.InvokeAsync(tab);
167-
ActiveTabId = tabId;
168-
await ActiveTabIdChanged.InvokeAsync(tabId);
169+
if (tabId != null)
170+
{
171+
ActiveTabId = tabId;
172+
await ActiveTabIdChanged.InvokeAsync(tabId);
173+
}
169174
}
170175
}
171176

172177
internal int RegisterTab(FluentTab tab)
173178
{
174-
_ = _tabs.TryAdd(tab.Id!, tab);
179+
_tabs.Add(tab);
175180
return _tabs.Count - 1;
176181
}
177182

178-
internal async Task UnregisterTabAsync(FluentTab tab)
183+
internal async Task UnregisterTabAsync(string id)
179184
{
180185
if (OnTabClose.HasDelegate)
181186
{
187+
var tab = _tabs.FirstOrDefault(t => t.Id == id);
182188
await OnTabClose.InvokeAsync(tab);
183189
}
184190

185191
if (_tabs.Count > 0)
186192
{
187-
_tabs.Remove(tab.Id!);
193+
_tabs.RemoveAt(_tabs.Count - 1);
188194
}
189195

190196
// Set the first tab active
191-
FluentTab? firstTab = _tabs.FirstOrDefault().Value;
197+
var firstTab = _tabs.FirstOrDefault();
192198
if (firstTab is not null)
193199
{
194200
await ResizeTabsForOverflowButtonAsync();
@@ -228,9 +234,9 @@ public async Task OverflowRaisedAsync(string value)
228234
}
229235

230236
// Update Item components
231-
foreach (OverflowItem item in items)
237+
foreach (var item in items)
232238
{
233-
FluentTab? tab = _tabs.FirstOrDefault(i => i.Value.Id == item.Id).Value;
239+
var tab = _tabs.FirstOrDefault(i => i.Id == item.Id);
234240
tab?.SetProperties(item.Overflow);
235241
}
236242

0 commit comments

Comments
 (0)