using System; using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using OneOf; namespace AntDesign { /// /// Modal Dialog /// public partial class Modal { #region Parameter /// /// Specify a function that will be called when modal is closed /// [Parameter] public Func AfterClose { get; set; } /// /// Body style for modal body element. Such as height, padding etc /// [Parameter] public string BodyStyle { get; set; } /// /// Text or RenderFragment of the Cancel button, it will override the ModalLocale /// [Parameter] public OneOf? CancelText { get; set; } = null; /// /// centered /// [Parameter] public bool Centered { get; set; } /// /// Whether a close (x) button is visible on top right of the modal dialog or not /// [Parameter] public bool Closable { get; set; } = true; /// /// Whether the modal dialog box be dragged /// [Parameter] public bool Draggable { get; set; } /// /// Drag and drop only within the Viewport /// [Parameter] public bool DragInViewport { get; set; } = true; /// /// closer icon RenderFragment, the default is a "X" /// [Parameter] public RenderFragment CloseIcon { get; set; } = DialogOptions.DefaultCloseIcon; /// /// Whether to apply loading visual effect for OK button or not /// [Parameter] public bool ConfirmLoading { get; set; } /// /// Whether to unmount child components on onClose, default is false /// [Parameter] public bool DestroyOnClose { get; set; } /// /// Footer content, set as Footer=null when you don't need default buttons /// [Parameter] public OneOf? Footer { get; set; } = DialogOptions.DefaultFooter; /// /// get or set the modal parent DOM, default is null: which is specifying document.body /// [Parameter] public ElementReference? GetContainer { get; set; } = null; /// /// Whether support press esc to close /// [Parameter] public bool Keyboard { get; set; } = true; /// /// Whether show mask or not /// [Parameter] public bool Mask { get; set; } = true; /// /// Whether to close the modal dialog when the mask (area outside the modal) is clicked /// [Parameter] public bool MaskClosable { get; set; } = true; /// /// Style for modal's mask element /// [Parameter] public string MaskStyle { get; set; } /// /// Text of RenderFragment of the OK button, it will override the ModalLocale /// [Parameter] public OneOf? OkText { get; set; } = null; /// /// Button type of the OK button /// [Parameter] public string OkType { get; set; } = ButtonType.Primary; #region title /// /// The modal dialog's title. If TitleTemplate!= null, Title will not take effect /// [Parameter] public string Title { get; set; } = null; /// /// The modal dialog's title /// [Parameter] public RenderFragment TitleTemplate { get; set; } = null; #endregion /// /// Whether the modal dialog is visible or not /// [Parameter] public bool Visible { get; set; } /// /// Width of the modal dialog, the default value is 520 /// [Parameter] public OneOf Width { get; set; } = 520; private string GetWidth() { if (Width.IsT0) { return Width.AsT0; } else { return $"{Width.AsT1}px"; } } /// /// The class name of the container of the modal dialog /// [Parameter] public string WrapClassName { get; set; } /// /// The z-index of the Modal /// [Parameter] public int ZIndex { get; set; } = 1000; /// /// Specify a function that will be called when a user clicks mask, close button on top right or Cancel button /// [Parameter] public EventCallback OnCancel { get; set; } /// /// Specify a function that will be called when a user clicks the OK button /// [Parameter] public EventCallback OnOk { get; set; } /// /// The OK button props /// [Parameter] public ButtonProps OkButtonProps { get; set; } /// /// The Cancel button props /// [Parameter] public ButtonProps CancelButtonProps { get; set; } /// /// /// [Parameter] public RenderFragment ChildContent { get; set; } /// /// Is RTL /// [CascadingParameter] public bool Rtl { get; set; } = false; /// /// Modal Locale /// [Parameter] public ModalLocale Locale { get; set; } = LocaleProvider.CurrentLocale.Modal; #endregion Parameter #pragma warning disable 649 private DialogWrapper _dialogWrapper; #pragma warning restore 649 private DialogOptions BuildDialogOptions() { DialogOptions options = new DialogOptions() { OnClosed = AfterClose, BodyStyle = BodyStyle, CancelText = CancelText ?? Locale.CancelText, Centered = Centered, Closable = Closable, Draggable = Draggable, DragInViewport = DragInViewport, CloseIcon = CloseIcon, ConfirmLoading = ConfirmLoading, Footer = Footer, GetContainer = GetContainer, Keyboard = Keyboard, Mask = Mask, MaskClosable = MaskClosable, MaskStyle = MaskStyle, OkText = OkText ?? Locale.OkText, OkType = OkType, Title = Title, TitleTemplate = TitleTemplate, Width = Width, WrapClassName = WrapClassName, ZIndex = ZIndex, OnCancel = async (e) => { if (OnCancel.HasDelegate) { await OnCancel.InvokeAsync(e); } }, OnOk = async (e) => { if (OnOk.HasDelegate) { await OnOk.InvokeAsync(e); } }, OkButtonProps = OkButtonProps, CancelButtonProps = CancelButtonProps, Rtl = Rtl }; return options; } #region Sustainable Dialog private bool _hasFocus = false; private async Task OnAfterDialogShow() { if (!_hasFocus) { await JsInvokeAsync(JSInteropConstants.FocusDialog, $"#{_dialogWrapper.Dialog.SentinelStart}"); _hasFocus = true; } } private async Task OnBeforeDialogWrapperDestroy() { _hasFocus = false; await InvokeAsync(StateHasChanged); } #endregion } }