fix(module: overlay): Interop delElementFrom() exception on page refresh (#1008)

* fix(module:overlay): page reload detection

* fix(module:overlaytrigger): event handler on window resize remove on dispose
This commit is contained in:
Andrzej Bakun 2021-01-20 12:58:20 +01:00 committed by GitHub
parent 375d72f90e
commit 81817ea3db
2 changed files with 15 additions and 3 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Text.Json;
using System.Threading.Tasks;
using AntDesign.JsInterop;
using Microsoft.AspNetCore.Components;
@ -55,6 +56,9 @@ namespace AntDesign.Internal
[Parameter]
public bool HiddenMode { get; set; } = false;
[Inject]
private DomEventService DomEventService { get; set; }
private bool _hasAddOverlayToBody = false;
private bool _isPreventHide = false;
private bool _isChildOverlayShow = false;
@ -100,6 +104,7 @@ namespace AntDesign.Internal
if (firstRender)
{
await JsInvokeAsync(JSInteropConstants.AddClsToFirstChild, Ref, $"{Trigger.PrefixCls}-trigger");
DomEventService.AddEventListener("window", "beforeunload", Reloading, false);
}
if (_lastDisabledState != Trigger.Disabled)
@ -130,7 +135,7 @@ namespace AntDesign.Internal
protected override void Dispose(bool disposing)
{
if (_hasAddOverlayToBody)
if (_hasAddOverlayToBody && !_isReloading)
{
_ = InvokeAsync(async () =>
{
@ -138,7 +143,7 @@ namespace AntDesign.Internal
await JsInvokeAsync(JSInteropConstants.DelElementFrom, Ref, Trigger.PopupContainerSelector);
});
}
DomEventService.RemoveEventListerner<JsonElement>("window", "beforeunload", Reloading);
base.Dispose(disposing);
}
@ -265,6 +270,12 @@ namespace AntDesign.Internal
return _isOverlayHiding;
}
/// <summary>
/// Indicates that a page is being refreshed
/// </summary>
private bool _isReloading;
private void Reloading(JsonElement jsonElement) => _isReloading = true;
private async Task AddOverlayToBody()
{
if (!_hasAddOverlayToBody)

View File

@ -118,6 +118,7 @@ namespace AntDesign.Internal
protected override void Dispose(bool disposing)
{
DomEventService.RemoveEventListerner<JsonElement>("document", "mouseup", OnMouseUp);
DomEventService.RemoveEventListerner<JsonElement>("window", "resize", OnMouseUp);
base.Dispose(disposing);
}