using Microsoft.AspNetCore.Components;
|
using Microsoft.JSInterop;
|
using System.Reflection;
|
|
namespace HotelPms.Client.Blazor.Util
|
{
|
public static class ElementReferenceEx
|
{
|
private static readonly PropertyInfo jsRuntimeProperty = typeof(WebElementReferenceContext).GetProperty("JSRuntime", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
internal static IJSRuntime GetJSRuntime(this ElementReference elementReference)
|
{
|
if (elementReference.Context is not WebElementReferenceContext context)
|
{
|
return null;
|
}
|
|
return (IJSRuntime)jsRuntimeProperty.GetValue(context);
|
}
|
|
/// <summary>
|
/// input[text] or textarea
|
/// </summary>
|
/// <param name="elementReference"></param>
|
/// <returns></returns>
|
public static ValueTask<string> GetInputValue(this ElementReference elementReference)
|
{
|
return elementReference.GetJSRuntime()?.InvokeAsync<string>("NetCallJs.getInputValue", elementReference) ?? ValueTask.FromResult(string.Empty);
|
}
|
|
/// <summary>
|
/// input[text] or textarea
|
/// </summary>
|
/// <param name="elementReference"></param>
|
/// <param name="value"></param>
|
/// <returns></returns>
|
public static ValueTask SetInputValue(this ElementReference elementReference, string value) => elementReference.GetJSRuntime()?.InvokeVoidAsync("NetCallJs.setInputValue", elementReference, value) ?? ValueTask.CompletedTask;
|
|
public static ValueTask<BoundingClientRect> GetClientRect(this ElementReference elementReference)
|
{
|
return elementReference.GetJSRuntime()?.InvokeAsync<BoundingClientRect>("NetCallJs.getClientRect", elementReference) ?? ValueTask.FromResult(new BoundingClientRect());
|
}
|
|
public static ValueTask<ScrollPosition> GetScroll(this ElementReference elementReference)
|
{
|
return elementReference.GetJSRuntime()?.InvokeAsync<ScrollPosition>("NetCallJs.getScroll", elementReference) ?? ValueTask.FromResult(new ScrollPosition());
|
}
|
}
|
}
|