ant-design-blazor/components/modal/Modal.razor.cs
zxyao 6db07ad916 feat: add component modal (#167)
* feat: add component modal

* fix: adjust namespace and fix TAB keyboard bug

* fix: reset package.json to cuueernt head

* fix: remove antblazor.sln

* refactor: using c# to control the display and hiding of modal
2020-06-02 14:25:44 +08:00

206 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;
using OneOf;
namespace AntDesign
{
public partial class Modal
{
private const string PrefixCls = "ant-modal";
#region Parameter
[Parameter] public EventCallback AfterClose { get; set; }
[Parameter]
public string BodyStyle { get; set; }
[Parameter]
public OneOf<string, RenderFragment> CancelText { get; set; } = "Cancel";
[Parameter]
public bool Centered { get; set; }
[Parameter] public bool Closable { get; set; } = true;
private static readonly RenderFragment _defaultCloseIcon = (builder) =>
{
builder.OpenComponent<AntIcon>(0);
builder.AddAttribute(1, "Type", "Close");
builder.AddAttribute(2, "Theme", "Outline");
builder.CloseComponent();
};
[Parameter] public RenderFragment CloseIcon { get; set; } = _defaultCloseIcon;
[Parameter] public bool ConfirmLoading { get; set; }
[Parameter] public bool DestroyOnClose { get; set; }
private static readonly RenderFragment _defaultFooter = (builder) =>
{
builder.OpenComponent<ModalFooter>(0);
builder.CloseComponent();
};
[Parameter] public OneOf<string, RenderFragment>? Footer { get; set; } = _defaultFooter;
[Parameter] public bool ForceRender { get; set; }
[Parameter] public ElementReference? GetContainer { get; set; } = null;
[Parameter] public bool Keyboard { get; set; } = true;
[Parameter] public bool Mask { get; set; } = true;
[Parameter] public bool MaskClosable { get; set; } = true;
[Parameter]
public string MaskStyle { get; set; }
[Parameter]
public OneOf<string, RenderFragment> OkText { get; set; } = "OK";
[Parameter]
public string OkType { get; set; } = ButtonType.Primary;
[Parameter]
public OneOf<string, RenderFragment> Title { get; set; }
[Parameter]
public bool Visible { get; set; }
[Parameter] public OneOf<string, double> Width { get; set; } = 520;
private string GetWidth()
{
if (Width.IsT0)
{
return Width.AsT0;
}
else
{
return $"{Width.AsT1}px";
}
}
[Parameter]
public string WrapClassName { get; set; }
[Parameter]
public int ZIndex { get; set; } = 1000;
[Parameter]
public EventCallback<MouseEventArgs> OnCancel { get; set; }
[Parameter]
public EventCallback<MouseEventArgs> OnOk { get; set; }
[Parameter]
public ButtonProps OkButtonProps { get; set; }
[Parameter]
public ButtonProps CancelButtonProps { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
[CascadingParameter] public bool Rtl { get; set; } = false;
private string GetWrapClassNameExtended()
{
var classNameArray = new List<string>();
if (Centered)
{
classNameArray.Add($"{PrefixCls}-centered");
}
if (Rtl)
{
classNameArray.Add($"{PrefixCls}-wrap-rtl");
}
return string.Join(' ', classNameArray);
}
#endregion
private Dialog _dialog;
private DialogOptions BuildDialogOptions()
{
DialogOptions options = new DialogOptions()
{
OnClosed = AfterClose,
BodyStyle = BodyStyle,
CancelText = CancelText,
Centered = Centered,
Closable = Closable,
CloseIcon = CloseIcon,
ConfirmLoading = ConfirmLoading,
DestroyOnClose = DestroyOnClose,
Footer = Footer,
ForceRender = ForceRender,
GetContainer = GetContainer,
Keyboard= Keyboard,
Mask = Mask,
MaskClosable = MaskClosable,
MaskStyle = MaskStyle,
OkText = OkText,
OkType = OkType,
Title = Title,
Width = Width,
WrapClassName = WrapClassName,
ZIndex = ZIndex,
OnCancel = OnCancel,
OnOk = OnOk,
OkButtonProps = OkButtonProps,
CancelButtonProps = CancelButtonProps,
Rtl = Rtl
};
return options;
}
private bool _hasAdd = false;
protected override async Task OnParametersSetAsync()
{
if (Visible && !_hasAdd)
{
_hasAdd = true;
}
await base.OnParametersSetAsync();
}
protected override async Task OnAfterRenderAsync(bool isFirst)
{
if (Visible)
{
await JsInvokeAsync(JSInteropConstants.focusDialog, $"#{_dialog.SentinelStart}");
}
else
{
if (DestroyOnClose)
{
if (_dialog != null)
{
await _dialog.Hide();
}
_hasAdd = false;
await InvokeAsync(StateHasChanged);
}
}
await base.OnAfterRenderAsync(isFirst);
}
}
}