ant-design-blazor/components/core/Base/AntComponentBase.cs

154 lines
4.4 KiB
C#
Raw Normal View History

2020-04-24 18:32:50 +08:00
using System;
2019-12-08 00:51:07 +08:00
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using AntDesign.Core.Extensions;
2019-12-08 00:51:07 +08:00
namespace AntDesign
2019-12-08 00:51:07 +08:00
{
2020-04-24 18:32:50 +08:00
public abstract class AntComponentBase : ComponentBase, IDisposable
2019-12-08 00:51:07 +08:00
{
[Parameter]
feat(module: overlay): OverlayTrigger not bound to a div (#937) * feat(module:overlay): OverlayTrigger not bound to a div * feat(module:overlay): OverlayTrigger not bound to a div * feat(module:overlay): Logic transfer to single Overlay * feat(module:overlay): remove obsolete duplication * feat(module:Tooltip): Add for unbounded oncontextmenu event handler * feat(module:tooltip): unbound js event listeners remove * docs(module:tooltip): unbound explanation * fix(module:button): attach Ref to top level html element @ref * feat(module:dropdown&tooltip&popconfirm&popover): Overlay not bound to a div * docs(module:dropdown&tooltip&popconfirm&popover): unbound explanation * feat(module:OverlayTrigger): common logic relocation * feat(module:overlaytrigger): Overlay not bound to a div * feat(module:DatePicker): Overlay not bound to a div * feat(module:select): Overlay not boud to div * fix(module:select): onclickarrow event relocation * fix(module:select): rename Show to OnArrowClick * feat(module:avatar): Overlay not bound to a div * docs(module:avatar): demo switch to unbound version * feat(module:autocomplete): partial OverlayTrigger not bound to a div * feat(module:slider): tooltip * docs(module:slider): tooltip * fix(module:overlay): add SetVisible method * feat: set Ref where missing, performance components register Ref when missing IsFixed flag for CascadeValue changed hard-code sequence numbers when using RenderTreeBuilder Rate component use Tooltip Unbound version Tabs test fix * fix: revert changes (accidental) * feat(module:upload): tooltip with unbound usage * feat(module:table): column use of unbound tooltip * feat(module:autocomplete):overlay unbound from div * fix(module:upload): missing div restore Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-01-21 17:20:10 +08:00
public ForwardRef RefBack { get; set; } = new ForwardRef();
2019-12-08 00:51:07 +08:00
private readonly Queue<Func<Task>> _afterRenderCallQuene = new Queue<Func<Task>>();
2019-12-08 00:51:07 +08:00
protected void CallAfterRender(Func<Task> action)
{
_afterRenderCallQuene.Enqueue(action);
2019-12-08 00:51:07 +08:00
}
2019-12-09 00:25:22 +08:00
protected override async Task OnAfterRenderAsync(bool firstRender)
2019-12-08 00:51:07 +08:00
{
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
await OnFirstAfterRenderAsync();
}
if (_afterRenderCallQuene.Count > 0)
2019-12-08 00:51:07 +08:00
{
var actions = _afterRenderCallQuene.ToArray();
_afterRenderCallQuene.Clear();
2019-12-08 00:51:07 +08:00
foreach (var action in actions)
{
2020-04-24 18:32:50 +08:00
if (IsDisposed)
2019-12-08 00:51:07 +08:00
{
return;
}
await action();
}
}
}
protected virtual Task OnFirstAfterRenderAsync()
{
return Task.CompletedTask;
}
protected AntComponentBase()
{
}
protected void InvokeStateHasChanged()
{
InvokeAsync(() =>
{
2020-04-24 18:32:50 +08:00
if (!IsDisposed)
2019-12-08 00:51:07 +08:00
{
StateHasChanged();
}
});
}
protected async Task InvokeStateHasChangedAsync()
{
await InvokeAsync(() =>
{
if (!IsDisposed)
{
StateHasChanged();
}
});
}
2019-12-08 00:51:07 +08:00
[Inject]
protected IJSRuntime Js { get; set; }
protected async Task<T> JsInvokeAsync<T>(string code, params object[] args)
{
try
{
return await Js.InvokeAsync<T>(code, args);
}
catch (Exception e)
{
throw;
}
}
protected async Task JsInvokeAsync(string code, params object[] args)
{
try
{
await Js.InvokeVoidAsync(code, args);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
/// <summary>
/// Standard Focus. From Net5 uses Blazor extension method on ElementReference.
/// Before, uses JS implemented exactly the same as Net5 JS.
/// </summary>
/// <param name="target">Element that will receive focus.</param>
/// <param name="preventScroll">Whether to scroll to focused element</param>
protected async Task FocusAsync(ElementReference target, bool preventScroll = false)
=> await Js.FocusAsync(target, preventScroll);
/// <summary>
/// Focus with behaviors. Behavior will work only for elements that are
/// HTMLInputElement or HTMLTextAreaElement. Otherwise will only focus.
/// </summary>
/// <param name="target">Element that will receive focus.</param>
/// <param name="behavior">Behavior of focused element</param>
/// <param name="preventScroll">Whether to scroll to focused element</param>
protected async Task FocusAsync(ElementReference target, FocusBehavior behavior, bool preventScroll = false)
=> await Js.FocusAsync(target, behavior, preventScroll);
/// <summary>
/// Standard Blur. Uses JS interop.
/// </summary>
/// <param name="target">Element that will receive focus.</param>
protected async Task BlurAsync(ElementReference target)
=> await Js.BlurAsyc(target);
2020-04-24 18:32:50 +08:00
protected bool IsDisposed { get; private set; }
2019-12-08 00:51:07 +08:00
2020-04-24 18:32:50 +08:00
protected virtual void Dispose(bool disposing)
2019-12-08 00:51:07 +08:00
{
2020-04-24 18:32:50 +08:00
if (IsDisposed) return;
IsDisposed = true;
2019-12-08 00:51:07 +08:00
}
2020-04-24 18:32:50 +08:00
public void Dispose()
2019-12-08 00:51:07 +08:00
{
2020-04-24 18:32:50 +08:00
Dispose(true);
GC.SuppressFinalize(this);
2019-12-08 00:51:07 +08:00
}
2020-04-24 18:32:50 +08:00
~AntComponentBase()
{
// Finalizer calls Dispose(false)
Dispose(false);
}
2019-12-08 00:51:07 +08:00
}
}