Refactor UIComponent
Removed unnecessary Category tags. Removed Tag property as it usually leads to component abuse. Refactored class format.
This commit is contained in:
parent
08a8a5338f
commit
f62c1f267e
@ -1,62 +1,36 @@
|
|||||||
using Connected.Annotations;
|
using Microsoft.AspNetCore.Components;
|
||||||
using Connected.Middleware;
|
|
||||||
using Microsoft.AspNetCore.Components;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace Connected.Components;
|
namespace Connected.Components;
|
||||||
|
|
||||||
public abstract class UIComponent : ComponentBase
|
public abstract class UIComponent : ComponentBase
|
||||||
{
|
{
|
||||||
[Inject]
|
/// <summary>
|
||||||
private ILoggerFactory LoggerFactory { get; set; }
|
/// User class names, separated by space.
|
||||||
[Inject]
|
/// </summary>
|
||||||
private IComponentMiddlewareService? MiddlewareService { get; set; }
|
[Parameter]
|
||||||
|
public string Class { get; set; }
|
||||||
|
|
||||||
private ILogger _logger;
|
/// <summary>
|
||||||
protected ILogger Logger => _logger ??= LoggerFactory.CreateLogger(GetType());
|
/// User styles, applied on top of the component's own classes and styles.
|
||||||
|
/// </summary>
|
||||||
|
[Parameter]
|
||||||
|
public string Style { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// User class names, separated by space.
|
/// Use Tag to attach any user data object to the component for your convenience.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Parameter]
|
[Parameter]
|
||||||
[Category(CategoryTypes.ComponentBase.Common)]
|
public object Tag { get; set; }
|
||||||
public string Class { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// User styles, applied on top of the component's own classes and styles.
|
/// UserAttributes carries all attributes you add to the component that don't match any of its parameters.
|
||||||
/// </summary>
|
/// They will be splatted onto the underlying HTML tag.
|
||||||
[Parameter]
|
/// </summary>
|
||||||
[Category(CategoryTypes.ComponentBase.Common)]
|
[Parameter(CaptureUnmatchedValues = true)]
|
||||||
public string Style { get; set; }
|
public Dictionary<string, object> UserAttributes { get; set; } = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Use Tag to attach any user data object to the component for your convenience.
|
/// If the UserAttributes contain an ID make it accessible for WCAG labelling of input fields
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Parameter]
|
public string FieldId => UserAttributes.TryGetValue("id", out var id) ? id.ToString() : $"input-{Guid.NewGuid()}";
|
||||||
[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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user