ant-design-blazor/components/modal/modalDialog/ModalContainer.razor.cs
zxyao 6aff9d370c fix(module: dialog): Modal and Drawer render reducing and update document and demo (#1701)
* fix(module: Modal&Drawer): render reducing and demo update

* docs(module: modal): add FAQ section

* docs: add English comments

* fix: wrong format in Modal_service_for_confirm.razor

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-07-11 20:48:16 +08:00

64 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
namespace AntDesign
{
public partial class ModalContainer
{
[Inject]
private ModalService ModalService { get; set; }
private readonly List<ModalRef> _modalRefs = new List<ModalRef>();
protected override void OnInitialized()
{
ModalService.OnModalOpenEvent += ModalService_OnModalOpenEvent;
ModalService.OnModalCloseEvent += ModalService_OnModalCloseEvent;
ModalService.OnModalUpdateEvent += ModalService_OnModalUpdateEvent;
}
private async Task ModalService_OnModalOpenEvent(ModalRef modalRef)
{
if (!_modalRefs.Contains(modalRef))
{
_modalRefs.Add(modalRef);
}
await InvokeAsync(StateHasChanged);
}
private async Task ModalService_OnModalCloseEvent(ModalRef modalRef)
{
if (modalRef.Config.Visible)
{
modalRef.Config.Visible = false;
await InvokeAsync(StateHasChanged);
await Task.Delay(250);
if (modalRef.Config.DestroyOnClose && _modalRefs.Contains(modalRef))
{
_modalRefs.Remove(modalRef);
await InvokeAsync(StateHasChanged);
}
}
}
private async Task ModalService_OnModalUpdateEvent(ModalRef arg)
{
await InvokeStateHasChangedAsync();
}
protected override void Dispose(bool disposing)
{
ModalService.OnModalOpenEvent -= ModalService_OnModalOpenEvent;
ModalService.OnModalCloseEvent -= ModalService_OnModalCloseEvent;
ModalService.OnModalUpdateEvent -= ModalService_OnModalUpdateEvent;
base.Dispose(disposing);
}
}
}