using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; namespace AntDesign { /// /// A component that can exist in the DOM tree for a long time, unless you destroy it on your own initiative /// public partial class DialogWrapper { #region Parameters /// /// /// [Parameter] public DialogOptions Config { get; set; } /// /// /// [Parameter] public RenderFragment ChildContent { get; set; } /// /// /// [Parameter] public bool DestroyOnClose { get; set; } /// /// /// [Parameter] public bool Visible { get; set; } /// /// Before destroy the component from the DOM tree. And you can cancel the destroy by set CancelEventArgs.Cancel = true /// [Parameter] public EventCallback OnBeforeDestroy { get; set; } /// /// trigger when visible is true on OnAfterRenderAsync method /// [Parameter] public EventCallback OnAfterShow { get; set; } /// /// trigger when visible is false on OnAfterRenderAsync method /// [Parameter] public EventCallback OnAfterHide { get; set; } #endregion /// /// /// public Dialog Dialog => _dialog; #pragma warning disable 649 private Dialog _dialog; #pragma warning restore 649 private bool _hasAdd; private bool _showDone; private bool _hideDone; private bool _hasDestroy; /// /// /// /// protected override async Task OnParametersSetAsync() { if (Visible && !_hasAdd) { _hasAdd = true; } await base.OnParametersSetAsync(); } /// /// /// /// /// protected override async Task OnAfterRenderAsync(bool firstRender) { if (_hasAdd) { if (Visible && !_showDone) { _showDone = true; _hideDone = false; _hasDestroy = false; if (OnAfterShow.HasDelegate) { await OnAfterShow.InvokeAsync(null); } } else if (!Visible) { if (!_hideDone) { _hideDone = true; _showDone = false; if (_dialog != null) { await _dialog.Hide(); await Task.Delay(250); await _dialog.TryResetModalStyle(); } await OnAfterHide.InvokeAsync(null); } if (DestroyOnClose && !_hasDestroy) { _hasDestroy = true; await DestroyAsync(); } } } await base.OnAfterRenderAsync(firstRender); } /// /// Destroy the component from the DOM tree /// public async Task DestroyAsync() { var cancel = new CancelEventArgs(); if (OnBeforeDestroy.HasDelegate) { await OnBeforeDestroy.InvokeAsync(cancel); } _hasAdd = cancel.Cancel; await InvokeStateHasChangedAsync(); } } }