Skip to content

Commit

Permalink
[Tabs] Fix Overflow getting out of sync with tab collection (#3284)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vnbaaij authored Feb 7, 2025
1 parent 46c4f02 commit 80b61c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
12 changes: 10 additions & 2 deletions src/Core/Components/Tabs/FluentTab.razor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// ------------------------------------------------------------------------
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------------------

using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components.Extensions;
using Microsoft.FluentUI.AspNetCore.Components.Utilities;
Expand Down Expand Up @@ -130,7 +134,7 @@ public FluentTab()

protected override void OnInitialized()
{
Index = Owner!.RegisterTab(this);
Index = Owner.RegisterTab(this);
}

/// <summary />
Expand All @@ -148,7 +152,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
/// <summary />
protected virtual Task CloseClickedAsync()
{
return Owner!.UnregisterTabAsync(this);
if (Id is null)
{
return Task.CompletedTask;
}
return Owner.UnregisterTabAsync(Id);
}

/// <summary />
Expand Down
36 changes: 21 additions & 15 deletions src/Core/Components/Tabs/FluentTabs.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------------------

using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components.Extensions;
using Microsoft.FluentUI.AspNetCore.Components.Utilities;
using Microsoft.JSInterop;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;

namespace Microsoft.FluentUI.AspNetCore.Components;

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

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

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

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

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

var horizontal = Orientation == Orientation.Horizontal;
await _jsModuleOverflow.InvokeVoidAsync("fluentOverflowInitialize", _dotNetHelper, Id, horizontal, FLUENT_TAB_TAG,25);
await _jsModuleOverflow.InvokeVoidAsync("fluentOverflowInitialize", _dotNetHelper, Id, horizontal, FLUENT_TAB_TAG, 25);
}
}

private async Task HandleOnTabChangedAsync(TabChangeEventArgs args)
{
var tabId = args?.ActiveId;
if (tabId is not null && _tabs.TryGetValue(tabId, out FluentTab? tab))
var tab = _tabs.FirstOrDefault(i => i.Id == tabId);

if (tab is not null && _tabs.Contains(tab))
{
await OnTabChange.InvokeAsync(tab);
ActiveTabId = tabId;
await ActiveTabIdChanged.InvokeAsync(tabId);
if (tabId != null)
{
ActiveTabId = tabId;
await ActiveTabIdChanged.InvokeAsync(tabId);
}
}
}

internal int RegisterTab(FluentTab tab)
{
_ = _tabs.TryAdd(tab.Id!, tab);
_tabs.Add(tab);
return _tabs.Count - 1;
}

internal async Task UnregisterTabAsync(FluentTab tab)
internal async Task UnregisterTabAsync(string id)
{
if (OnTabClose.HasDelegate)
{
var tab = _tabs.FirstOrDefault(t => t.Id == id);
await OnTabClose.InvokeAsync(tab);
}

if (_tabs.Count > 0)
{
_tabs.Remove(tab.Id!);
_tabs.RemoveAt(_tabs.Count - 1);
}

// Set the first tab active
FluentTab? firstTab = _tabs.FirstOrDefault().Value;
var firstTab = _tabs.FirstOrDefault();
if (firstTab is not null)
{
await ResizeTabsForOverflowButtonAsync();
Expand Down Expand Up @@ -228,9 +234,9 @@ public async Task OverflowRaisedAsync(string value)
}

// Update Item components
foreach (OverflowItem item in items)
foreach (var item in items)
{
FluentTab? tab = _tabs.FirstOrDefault(i => i.Value.Id == item.Id).Value;
var tab = _tabs.FirstOrDefault(i => i.Id == item.Id);
tab?.SetProperties(item.Overflow);
}

Expand Down

0 comments on commit 80b61c9

Please sign in to comment.