ant-design-blazor/components/core/Base/AntComponentBase.cs
Andrzej Bakun 2d693740c6 fix(module: input): fix & feat & docs & tests: Input general fixes (#1530)
* docs(module:input): api correction & comments

* fix(module:input): clear icon in sync with antD

* fix(module:input): add parameter bordered

* fix(module:textarea): add parameter ShowCounter

* fix(module:input): Focus with behaviors

* docs: add missing docs, rearrange order

* fix(module:search): update to match new standard look

doc(module:search): add API & demos

* fix(module:inputgroup): override style to remove whitespace\
docs: same as react

* fix(module:inputpassword): support custom icon
docs: same as react

* fix(module:input): add blur method
docs: add blur method

* fix(module:input): add readonly parameter

* tests

* fix

* fix(module:input): sequence correction

* Update components/core/Base/AntComponentBase.cs

Co-authored-by: James Yeung <shunjiey@hotmail.com>

* review fixes

* fix: clean-up

* tests: fix missing mock

* fix: focus

* fix(module:input): bind clear button to _inputValue

* tests: clear button show/hide

* tests: package update

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-05-27 18:13:26 +08:00

155 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using AntDesign.Core.Extensions;
namespace AntDesign
{
public abstract class AntComponentBase : ComponentBase, IDisposable
{
[Parameter]
public ForwardRef RefBack { get; set; } = new ForwardRef();
private readonly Queue<Func<Task>> _afterRenderCallQuene = new Queue<Func<Task>>();
protected void CallAfterRender(Func<Task> action)
{
_afterRenderCallQuene.Enqueue(action);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
await OnFirstAfterRenderAsync();
}
if (_afterRenderCallQuene.Count > 0)
{
var actions = _afterRenderCallQuene.ToArray();
_afterRenderCallQuene.Clear();
foreach (var action in actions)
{
if (IsDisposed)
{
return;
}
await action();
}
}
}
protected virtual Task OnFirstAfterRenderAsync()
{
return Task.CompletedTask;
}
protected AntComponentBase()
{
}
protected void InvokeStateHasChanged()
{
InvokeAsync(() =>
{
if (!IsDisposed)
{
StateHasChanged();
}
});
}
protected async Task InvokeStateHasChangedAsync()
{
await InvokeAsync(() =>
{
if (!IsDisposed)
{
StateHasChanged();
}
});
}
[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)
{
Console.WriteLine(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);
protected bool IsDisposed { get; private set; }
protected virtual void Dispose(bool disposing)
{
if (IsDisposed) return;
IsDisposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~AntComponentBase()
{
// Finalizer calls Dispose(false)
Dispose(false);
}
}
}