Skip to content

Commit eb52148

Browse files
committed
Add the NewElementCommand.Hx property
1 parent 173f417 commit eb52148

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

Sources/New-Element.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ public abstract class NewElementCommand(string tagName, bool isVoid = false): PS
8484
[Parameter]
8585
public SwitchParameter Hidden { get; set; }
8686

87+
/// <summary>
88+
/// The <c>htmx</c> attributes to render.
89+
/// </summary>
90+
[Parameter]
91+
public Hashtable Hx { get; set; } = [];
92+
8793
/// <summary>
8894
/// The element identifier.
8995
/// </summary>
@@ -222,6 +228,7 @@ protected virtual void RenderAttributes(IDictionary<string, object?> attributes)
222228
if (Dir is not null) attributes["dir"] = Dir;
223229
if (Draggable is not null) attributes["draggable"] = Draggable;
224230
if (Hidden) attributes["hidden"] = true;
231+
foreach (DictionaryEntry entry in Hx) attributes[$"hx-{kebabCase(entry.Key.ToString() ?? "")}"] = entry.Value;
225232
if (!string.IsNullOrWhiteSpace(Id)) attributes["id"] = Id;
226233
if (Inert) attributes["inert"] = true;
227234
if (InputMode is not null) attributes["inputmode"] = InputMode;

Tests/New-Element.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ Describe "New-Element" {
6868
$expected | Should-ContainCollection (button -DataSet @{ BsToggle = "tooltip"; PushUrl = $true })
6969
}
7070

71+
It "should htmx attributes" {
72+
$expected = '<button hx-confirm="Wat?" hx-post="/new"></button>', '<button hx-post="/new" hx-confirm="Wat?"></button>'
73+
$expected | Should-ContainCollection (button -Hx @{ Confirm = "Wat?"; Post = "/new" })
74+
Should-BeString '<button hx-on:app:click="alert(''Hello!'')"></button>' (button -Hx @{ "On:app:click" = "alert('Hello!')" }) -CaseSensitive
75+
}
76+
7177
It "should handle event handler attributes" {
7278
$expected = '<button onclick="submit(event)" oncontextmenu="showMenu()"></button>', '<button oncontextmenu="showMenu()" onclick="submit(event)"></button>'
7379
$expected | Should-ContainCollection (button -On @{ Click = "submit(event)"; ContextMenu = "showMenu()" })

0 commit comments

Comments
 (0)