From 82c4fb7fcc9b548dde1c8c692b223b190b6d1704 Mon Sep 17 00:00:00 2001 From: capdiem Date: Tue, 16 Apr 2024 15:15:13 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E2=99=BB=20refactor(Tabs):=20add=20support?= =?UTF-8?q?=20for=20custom=20the=20default=20item=20value=20in=20sub=20cla?= =?UTF-8?q?sses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/ItemGroup/ItemGroupBase.cs | 15 ++++++++++++--- .../Components/Window/BWindow.razor.cs | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Component/BlazorComponent/Components/ItemGroup/ItemGroupBase.cs b/src/Component/BlazorComponent/Components/ItemGroup/ItemGroupBase.cs index f9bf36053..c2b7bb591 100644 --- a/src/Component/BlazorComponent/Components/ItemGroup/ItemGroupBase.cs +++ b/src/Component/BlazorComponent/Components/ItemGroup/ItemGroupBase.cs @@ -72,9 +72,14 @@ private void RefreshItemsState() Items.ForEach(item => item.RefreshState()); } - public virtual void Register(IGroupable item) + protected virtual StringNumber InitDefaultItemValue() { - item.Value ??= _registeredItemsIndex++; + return _registeredItemsIndex++; + } + + internal virtual void Register(IGroupable item) + { + item.Value ??= InitDefaultItemValue(); Items.Add(item); @@ -109,7 +114,11 @@ public virtual void Register(IGroupable item) public virtual void Unregister(IGroupable item) { Items.Remove(item); - _registeredItemsIndex--; + + if (_registeredItemsIndex > 0) + { + _registeredItemsIndex--; + } } private async Task UpdateMandatoryAsync(bool last = false) diff --git a/src/Component/BlazorComponent/Components/Window/BWindow.razor.cs b/src/Component/BlazorComponent/Components/Window/BWindow.razor.cs index f1c0055ce..32f65a0ed 100644 --- a/src/Component/BlazorComponent/Components/Window/BWindow.razor.cs +++ b/src/Component/BlazorComponent/Components/Window/BWindow.razor.cs @@ -68,7 +68,7 @@ protected override void RegisterWatchers(PropertyWatcher watcher) true); } - public override void Register(IGroupable item) + internal override void Register(IGroupable item) { base.Register(item); From 277eb0c604b4b68165f4f4f86fdd2416bab0df37 Mon Sep 17 00:00:00 2001 From: capdiem Date: Tue, 16 Apr 2024 15:17:49 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9A=B0=20refactor:=20remove=20unnecessar?= =?UTF-8?q?y=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/ItemGroup/BRoutableGroupItem.razor.cs | 3 --- .../BlazorComponent/Components/Tabs/BTab.razor.cs | 7 ------- 2 files changed, 10 deletions(-) diff --git a/src/Component/BlazorComponent/Components/ItemGroup/BRoutableGroupItem.razor.cs b/src/Component/BlazorComponent/Components/ItemGroup/BRoutableGroupItem.razor.cs index f516fbff3..6e9c87611 100644 --- a/src/Component/BlazorComponent/Components/ItemGroup/BRoutableGroupItem.razor.cs +++ b/src/Component/BlazorComponent/Components/ItemGroup/BRoutableGroupItem.razor.cs @@ -82,14 +82,11 @@ private async Task UpdateActiveForRoutable() if (matched && ItemGroup is not null && !isActive) { await ItemGroup.ToggleAsync(Value); - await OnActiveUpdatedForRoutable(); } return isActive != matched; } - protected virtual Task OnActiveUpdatedForRoutable() => Task.CompletedTask; - protected override ValueTask DisposeAsyncCore() { NavigationManager.LocationChanged -= OnLocationChanged; diff --git a/src/Component/BlazorComponent/Components/Tabs/BTab.razor.cs b/src/Component/BlazorComponent/Components/Tabs/BTab.razor.cs index 3c616ccba..5baa9bbc2 100644 --- a/src/Component/BlazorComponent/Components/Tabs/BTab.razor.cs +++ b/src/Component/BlazorComponent/Components/Tabs/BTab.razor.cs @@ -13,13 +13,6 @@ public BTab() : base(GroupType.SlideGroup) protected override bool IsRoutable => Href != null && HasRoutableAncestor; - protected override async Task OnActiveUpdatedForRoutable() - { - if (Tabs == null) return; - - await Tabs.CallSlider(); - } - protected override bool AfterHandleEventShouldRender() => false; private async Task HandleOnClick(MouseEventArgs args) From 3f8c7172e782123290e7fdb45ab222e4440937d5 Mon Sep 17 00:00:00 2001 From: capdiem Date: Tue, 16 Apr 2024 15:18:37 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=86=95=20feat(Tabs):=20Add=20CallSlid?= =?UTF-8?q?erAfterRender=20public=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Tabs/BTabs.razor.cs | 67 ++++++++++++++----- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/src/Component/BlazorComponent/Components/Tabs/BTabs.razor.cs b/src/Component/BlazorComponent/Components/Tabs/BTabs.razor.cs index 8a0fafc97..59c320eb9 100644 --- a/src/Component/BlazorComponent/Components/Tabs/BTabs.razor.cs +++ b/src/Component/BlazorComponent/Components/Tabs/BTabs.razor.cs @@ -70,6 +70,8 @@ public EventCallback ValueChanged private StringNumber? _prevValue; private int _registeredTabItemsIndex; + private bool _callSliderOnAfterRender; + private CancellationTokenSource? _callSliderCts; private List TabItems { get; set; } = new(); @@ -98,7 +100,7 @@ public bool IsDark } List ITabs.TabItems => TabItems; - + protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); @@ -106,12 +108,17 @@ protected override async Task OnAfterRenderAsync(bool firstRender) if (firstRender) { await ResizeJSModule.ObserverAsync(Ref, OnResize); - - await CallSlider(); + _callSliderOnAfterRender = true; } else if (_prevValue != Value) { _prevValue = Value; + _callSliderOnAfterRender = true; + } + + if (_callSliderOnAfterRender) + { + _callSliderOnAfterRender = false; await CallSlider(); } } @@ -134,28 +141,45 @@ public void UnregisterTabItem(ITabItem tabItem) TabItems.Remove(tabItem); } + /// + /// Re-render slider immediately. For the case of deleting tabs, + /// it is recommended to use . + /// + [MasaApiPublicMethod] public async Task CallSlider() { if (HideSlider) return; - var item = Instance?.Items?.FirstOrDefault(item => item.Value == Instance.Value); - if (item?.Ref.Context == null) - { - Slider = (0, 0, 0, 0, 0); - } - else + _callSliderCts?.Cancel(); + _callSliderCts = new(); + + try { - var el = await JsInvokeAsync(JsInteropConstants.GetDomInfo, item.Ref); - var height = !Vertical ? SliderSize.TryGetNumber().number : el.ScrollHeight; - var left = Vertical ? 0 : el.OffsetLeft; - var right = Vertical ? 0 : el.OffsetLeft + el.OffsetWidth; - var top = el.OffsetTop; + await Task.Delay(16, _callSliderCts.Token); + + var item = Instance?.Items?.FirstOrDefault(item => item.Value == Instance.Value); + if (item?.Ref.Context == null) + { + Slider = (0, 0, 0, 0, 0); + } + else + { + var el = await JsInvokeAsync(JsInteropConstants.GetDomInfo, item.Ref); + var height = !Vertical ? SliderSize.TryGetNumber().number : el.ScrollHeight; + var left = Vertical ? 0 : el.OffsetLeft; + var right = Vertical ? 0 : el.OffsetLeft + el.OffsetWidth; + var top = el.OffsetTop; var width = Vertical ? SliderSize.TryGetNumber().number : el.ClientWidth; // REVIEW: el.ScrollWidth was used in Vuetify2 - Slider = (height, left, right, top, width); - } + Slider = (height, left, right, top, width); + } - StateHasChanged(); + StateHasChanged(); + } + catch (TaskCanceledException) + { + // ignored + } } private async Task OnResize() @@ -168,6 +192,15 @@ private async Task OnResize() await CallSlider(); } + /// + /// Re-render slider in + /// + [MasaApiPublicMethod] + public void CallSliderAfterRender() + { + _callSliderOnAfterRender = true; + } + async ValueTask IAsyncDisposable.DisposeAsync() { try