-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathHxDynamicElement.cs
65 lines (54 loc) · 2.26 KB
/
HxDynamicElement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace Havit.Blazor.Components.Web;
/// <summary>
/// Renders an element with the specified name, attributes, and child content.<br />
/// Full documentation and demos: <see href="https://havit.blazor.eu/components/HxDynamicElement">https://havit.blazor.eu/components/HxDynamicElement</see>
/// </summary>
public class HxDynamicElement : ComponentBase
{
/// <summary>
/// Gets or sets the name of the element to render.
/// </summary>
[Parameter] public string ElementName { get; set; } = "span";
/// <summary>
/// Raised after the element is clicked.
/// </summary>
[Parameter] public EventCallback<MouseEventArgs> OnClick { get; set; }
/// <summary>
/// Triggers the <see cref="OnClick"/> event. Allows interception of the event in derived components.
/// </summary>
protected virtual Task InvokeOnClickAsync(MouseEventArgs args) => OnClick.InvokeAsync(args);
/// <summary>
/// Stops onClick event propagation. Default is <c>false</c>.
/// </summary>
[Parameter] public bool OnClickStopPropagation { get; set; }
/// <summary>
/// Prevents the default action for the onclick event. Default is <c>false</c>.
/// </summary>
[Parameter] public bool OnClickPreventDefault { get; set; }
/// <summary>
/// Element reference.
/// </summary>
[Parameter] public ElementReference ElementRef { get; set; }
/// <summary>
/// Action (synchronous, not an EventCallback) called when the element's reference is captured.
/// </summary>
[Parameter] public Action<ElementReference> ElementRefChanged { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
[Parameter(CaptureUnmatchedValues = true)]
public IDictionary<string, object> AdditionalAttributes { get; set; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, ElementName);
builder.AddAttribute(1, "onclick", InvokeOnClickAsync);
builder.AddEventPreventDefaultAttribute(2, "onclick", OnClickPreventDefault);
builder.AddEventStopPropagationAttribute(3, "onclick", OnClickStopPropagation);
builder.AddMultipleAttributes(4, AdditionalAttributes);
builder.AddElementReferenceCapture(5, capturedRef =>
{
ElementRef = capturedRef;
ElementRefChanged?.Invoke(ElementRef);
});
builder.AddContent(6, ChildContent);
builder.CloseElement();
}
}