|
|
@ -1,5 +1,4 @@
|
|
|
|
using Connected.Annotations;
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Components.Rendering;
|
|
|
|
using Microsoft.AspNetCore.Components.Rendering;
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
|
|
|
|
|
|
|
@ -11,72 +10,97 @@ namespace Connected.Components;
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public class Element : UIComponent
|
|
|
|
public class Element : UIComponent
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Child content
|
|
|
|
/// Child content
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
[Parameter]
|
|
|
|
[Category(CategoryTypes.Element.Misc)]
|
|
|
|
public RenderFragment? ChildContent { get; set; }
|
|
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
#region EventCallbacks
|
|
|
|
/// <summary>
|
|
|
|
[Parameter]
|
|
|
|
/// The HTML element that will be rendered in the root by the component
|
|
|
|
public EventCallback<ElementReference> RefChanged { get; set; }
|
|
|
|
/// </summary>
|
|
|
|
#endregion
|
|
|
|
[Parameter]
|
|
|
|
|
|
|
|
[Category(CategoryTypes.Element.Misc)]
|
|
|
|
#region Content placeholders
|
|
|
|
public string HtmlTag { get; set; } = "span";
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// The Ref to bind to.
|
|
|
|
/// The ElementReference to bind to.
|
|
|
|
/// Usage: @bind-Ref="myRef"
|
|
|
|
/// Use like @bind-Ref="myRef"
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
[Parameter]
|
|
|
|
public ElementReference? Ref { get; set; }
|
|
|
|
[Category(CategoryTypes.Element.Misc)]
|
|
|
|
#endregion
|
|
|
|
public ElementReference? Ref { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
#region Styling properties
|
|
|
|
[Parameter] public EventCallback<ElementReference> RefChanged { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// A space separated list of class names, added on top of the default class list.
|
|
|
|
/// <summary>
|
|
|
|
/// </summary>
|
|
|
|
/// Calling StateHasChanged to refresh the component's state
|
|
|
|
[Parameter]
|
|
|
|
/// </summary>
|
|
|
|
public string? ClassList { get; set; }
|
|
|
|
public void Refresh() => StateHasChanged();
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
protected override void BuildRenderTree(RenderTreeBuilder builder)
|
|
|
|
#region Behavior properties
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
base.BuildRenderTree(builder);
|
|
|
|
/// The HTML element that will be rendered in the root by the component
|
|
|
|
//Open
|
|
|
|
/// </summary>
|
|
|
|
builder.OpenElement(0, HtmlTag);
|
|
|
|
[Parameter]
|
|
|
|
|
|
|
|
public string HtmlTag { get; set; } = "span";
|
|
|
|
//splatted attributes
|
|
|
|
|
|
|
|
foreach (var attribute in CustomAttributes)
|
|
|
|
[Parameter]
|
|
|
|
{
|
|
|
|
public bool PreventOnClickPropagation { get; set; }
|
|
|
|
// checking if the value is null, we can get rid of null event handlers
|
|
|
|
#endregion
|
|
|
|
// for example `@onmouseenter=@(IsOpen ? HandleEnter : null)`
|
|
|
|
|
|
|
|
// this is a powerful feature that in normal HTML elements doesn't work, because
|
|
|
|
/// <summary>
|
|
|
|
// Blazor adds always the attribute value and creates an EventCallback
|
|
|
|
/// Schedules a component's state refresh
|
|
|
|
if (attribute.Value != null)
|
|
|
|
/// </summary>
|
|
|
|
builder.AddAttribute(1, attribute.Key, attribute.Value);
|
|
|
|
public void Refresh() => StateHasChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Class
|
|
|
|
protected override void BuildRenderTree(RenderTreeBuilder builder)
|
|
|
|
builder.AddAttribute(2, "class", AdditionalClassList);
|
|
|
|
{
|
|
|
|
|
|
|
|
base.BuildRenderTree(builder);
|
|
|
|
// StopPropagation
|
|
|
|
|
|
|
|
// the order matters. This has to be before content is added
|
|
|
|
builder.OpenElement(0, HtmlTag);
|
|
|
|
if (HtmlTag == "button")
|
|
|
|
|
|
|
|
builder.AddEventStopPropagationAttribute(5, "onclick", true);
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Handle splatted attributes
|
|
|
|
//Reference capture
|
|
|
|
*/
|
|
|
|
if (Ref != null)
|
|
|
|
foreach (var attribute in CustomAttributes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
builder.AddElementReferenceCapture(6, async capturedRef =>
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
* Checking if the value is null, we can get rid of null event handlers,
|
|
|
|
Ref = capturedRef;
|
|
|
|
* for example `@onmouseenter=@(IsOpen ? HandleEnter : null)`.
|
|
|
|
await RefChanged.InvokeAsync(Ref.Value);
|
|
|
|
* This is a powerful feature that does not work in regular html elements
|
|
|
|
});
|
|
|
|
* because Blazor always adds the attribute value and creates an EventCallback.
|
|
|
|
}
|
|
|
|
* */
|
|
|
|
|
|
|
|
if (attribute.Value is not null)
|
|
|
|
//Content
|
|
|
|
builder.AddAttribute(1, attribute.Key, attribute.Value);
|
|
|
|
builder.AddContent(10, ChildContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Close
|
|
|
|
builder.AddAttribute(2, "class", ClassList);
|
|
|
|
builder.CloseElement();
|
|
|
|
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Stop onclock propagation for buttons. The order matters.
|
|
|
|
|
|
|
|
* This has to be before content is added
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (PreventOnClickPropagation)
|
|
|
|
|
|
|
|
builder.AddEventStopPropagationAttribute(5, "onclick", true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Add reference capture propagation.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (Ref is not null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
builder.AddElementReferenceCapture(6, async capturedRef =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Ref = capturedRef;
|
|
|
|
|
|
|
|
await RefChanged.InvokeAsync(Ref.Value);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Render child content
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
builder.AddContent(10, ChildContent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.CloseElement();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|