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);
}
///
/// input[text] or textarea
///
///
///
public static ValueTask GetInputValue(this ElementReference elementReference)
{
return elementReference.GetJSRuntime()?.InvokeAsync("NetCallJs.getInputValue", elementReference) ?? ValueTask.FromResult(string.Empty);
}
///
/// input[text] or textarea
///
///
///
///
public static ValueTask SetInputValue(this ElementReference elementReference, string value) => elementReference.GetJSRuntime()?.InvokeVoidAsync("NetCallJs.setInputValue", elementReference, value) ?? ValueTask.CompletedTask;
public static ValueTask GetClientRect(this ElementReference elementReference)
{
return elementReference.GetJSRuntime()?.InvokeAsync("NetCallJs.getClientRect", elementReference) ?? ValueTask.FromResult(new BoundingClientRect());
}
public static ValueTask GetScroll(this ElementReference elementReference)
{
return elementReference.GetJSRuntime()?.InvokeAsync("NetCallJs.getScroll", elementReference) ?? ValueTask.FromResult(new ScrollPosition());
}
}
}