47 lines
1.8 KiB
C#
47 lines
1.8 KiB
C#
namespace Htmx.ApiDemo.Templates.Components;
|
|
|
|
/// <summary>
|
|
/// shadcn-style RadioGroup.
|
|
/// Direction: flex-col | flex-row
|
|
/// </summary>
|
|
public sealed class RadioGroup : RadioGroupBase
|
|
{
|
|
private readonly byte[] _groupLabelData;
|
|
private readonly byte[] _directionData;
|
|
private readonly byte[] _itemsData;
|
|
|
|
public RadioGroup(
|
|
string name,
|
|
IEnumerable<(string Value, string Label, bool Selected)> options,
|
|
string label = "",
|
|
string direction = "flex-col")
|
|
{
|
|
_groupLabelData = string.IsNullOrEmpty(label)
|
|
? []
|
|
: $"""<span class="text-sm font-medium leading-none">{label}</span>""".ToUtf8Bytes();
|
|
|
|
_directionData = direction.ToUtf8Bytes();
|
|
|
|
var sb = new System.Text.StringBuilder();
|
|
foreach (var (value, optLabel, selected) in options)
|
|
{
|
|
var optId = $"{name}-{value}";
|
|
var sel = selected ? " checked" : "";
|
|
sb.Append($"""
|
|
<label class="flex items-center gap-2 cursor-pointer text-sm">
|
|
<input type="radio" id="{optId}" name="{name}" value="{value}"{sel}
|
|
class="h-4 w-4 border border-input accent-primary
|
|
focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring
|
|
disabled:cursor-not-allowed disabled:opacity-50" />
|
|
{optLabel}
|
|
</label>
|
|
""");
|
|
}
|
|
_itemsData = sb.ToString().ToUtf8Bytes();
|
|
}
|
|
|
|
protected override void RenderGroupLabel(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_groupLabelData);
|
|
protected override void RenderDirection(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_directionData);
|
|
protected override void RenderItems(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_itemsData);
|
|
}
|