You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Components/Components/UIComponent.cs

63 lines
1.9 KiB

using Connected.Annotations;
using Connected.Middleware;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
namespace Connected.Components;
public abstract class UIComponent : ComponentBase
{
[Inject]
private ILoggerFactory LoggerFactory { get; set; }
[Inject]
private IComponentMiddlewareService? MiddlewareService { get; set; }
private ILogger _logger;
protected ILogger Logger => _logger ??= LoggerFactory.CreateLogger(GetType());
/// <summary>
/// User class names, separated by space.
/// </summary>
[Parameter]
[Category(CategoryTypes.ComponentBase.Common)]
public string Class { get; set; }
/// <summary>
/// User styles, applied on top of the component's own classes and styles.
/// </summary>
[Parameter]
[Category(CategoryTypes.ComponentBase.Common)]
public string Style { get; set; }
/// <summary>
/// Use Tag to attach any user data object to the component for your convenience.
/// </summary>
[Parameter]
[Category(CategoryTypes.ComponentBase.Common)]
public object Tag { get; set; }
/// <summary>
/// UserAttributes carries all attributes you add to the component that don't match any of its parameters.
/// They will be splatted onto the underlying HTML tag.
/// </summary>
[Parameter(CaptureUnmatchedValues = true)]
[Category(CategoryTypes.ComponentBase.Common)]
public Dictionary<string, object> UserAttributes { get; set; } = new Dictionary<string, object>();
/// <summary>
/// If the UserAttributes contain an ID make it accessible for WCAG labelling of input fields
/// </summary>
public string FieldId => (UserAttributes?.ContainsKey("id") == true ? UserAttributes["id"].ToString() : $"mudinput-{Guid.NewGuid()}");
protected Type ResolveComponent<TComponent>()
{
if (MiddlewareService is null)
return typeof(TComponent);
if (MiddlewareService.Select<TComponent>() is Type type)
return type;
return typeof(TComponent);
}
}