mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-15 01:11:52 +08:00
4b04f09832
* perf: avoid memory leak #1834 Avoid memory leak by remove the exclusive parameter and logic in the code block on AddEventListener method in DomEventService class. The following are the components affected: components/affix/Affix.razor.cs components/anchor/Anchor.razor.cs components/carousel/Carousel.razor.cs components/core/Component/Overlay/Overlay.razor.cs components/core/Component/Overlay/OverlayTrigger.razor.cs components/core/JsInterop/DomEventService.cs components/descriptions/Descriptions.razor.cs components/dropdown/DropdownButton.cs components/grid/Row.razor.cs components/input/Input.cs components/input/TextArea.razor.cs components/layout/Sider.razor.cs components/list/ListItem.razor.cs components/select/Select.razor.cs components/select/internal/SelectContent.razor.cs components/slider/Slider.razor.cs components/table/Table.razor.cs components/tabs/Tabs.razor.cs * fix override AddEventListener method in AntDesign.TestKit project * add register/remove event listerner for exclusive use in DomEventService class * move _dotNetObjects to DomEventListerner class/service, so that users not required to maintain it in each component. * * move share/reuse dom event listerner methods to DomEventListerner class * remove method 'AddEventListener' that no longer exists in DomEventService class in AntDesign.TestKit project * * change the component referring to an IDomEventListerner interface instead of a concrete class, so that the component can be tested via a mock TestDomEventListerner. * introduce DisposeShared and Dispose method in DomEventListerner to ease user remove callback from DomEventListerner * register IDomEventListerner into DI container instead of create manually * fix FormatKey * fix FormatKey * fix tests * fix test * fix test Co-authored-by: James Yeung <shunjiey@hotmail.com>
96 lines
2.9 KiB
C#
96 lines
2.9 KiB
C#
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using AntDesign.JsInterop;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace AntDesign
|
|
{
|
|
public partial class BackTop : AntDomComponentBase
|
|
{
|
|
private bool _visible = false;
|
|
|
|
[Inject]
|
|
private IDomEventListener DomEventListener { get; set; }
|
|
|
|
[Parameter]
|
|
public string Icon { get; set; } = "vertical-align-top";
|
|
|
|
[Parameter]
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
[Parameter]
|
|
public double VisibilityHeight { get; set; } = 400;
|
|
|
|
/// <summary>
|
|
/// 回到顶部的目标控件
|
|
/// </summary>
|
|
[Parameter]
|
|
public string TargetSelector { get; set; }
|
|
|
|
protected ClassMapper BackTopContentClassMapper { get; set; } = new ClassMapper();
|
|
|
|
protected ClassMapper BackTopIconClassMapper { get; set; } = new ClassMapper();
|
|
|
|
[Parameter]
|
|
public EventCallback OnClick { get; set; }
|
|
|
|
protected async Task OnClickHandle()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(TargetSelector))
|
|
await JsInvokeAsync<DomRect>(JSInteropConstants.BackTop);
|
|
else
|
|
await JsInvokeAsync<DomRect>(JSInteropConstants.BackTop, TargetSelector);
|
|
|
|
if (OnClick.HasDelegate)
|
|
await OnClick.InvokeAsync(null);
|
|
}
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
SetClass();
|
|
base.OnInitialized();
|
|
}
|
|
|
|
protected async override Task OnFirstAfterRenderAsync()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(TargetSelector))
|
|
{
|
|
DomEventListener.AddExclusive<JsonElement>("window", "scroll", OnScroll);
|
|
}
|
|
else
|
|
{
|
|
DomEventListener.AddExclusive<JsonElement>(TargetSelector, "scroll", OnScroll);
|
|
}
|
|
await base.OnFirstAfterRenderAsync();
|
|
}
|
|
|
|
protected void SetClass()
|
|
{
|
|
string prefixCls = "ant-back-top";
|
|
ClassMapper.Add(prefixCls);
|
|
|
|
BackTopContentClassMapper
|
|
.If($"{prefixCls}-content", () => ChildContent == null)
|
|
.If("ant-fade ant-fade-leave ant-fade-leave-active", () => !_visible)
|
|
.If("ant-fade ant-fade-enter ant-fade-enter-active", () => _visible)
|
|
;
|
|
|
|
BackTopIconClassMapper.Add($"{prefixCls}-icon");
|
|
}
|
|
|
|
private async void OnScroll(JsonElement obj)
|
|
{
|
|
JsonElement scrollInfo = await JsInvokeAsync<JsonElement>(JSInteropConstants.GetScroll);
|
|
double offset = scrollInfo.GetProperty("y").GetDouble();
|
|
_visible = offset > VisibilityHeight;
|
|
StateHasChanged();
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
DomEventListener.DisposeExclusive();
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
}
|