33 lines
1.3 KiB
C#
33 lines
1.3 KiB
C#
namespace Htmx.ApiDemo.Templates.Components;
|
|
|
|
/// <summary>
|
|
/// shadcn-style Switch (toggle). Rendered as a styled checkbox.
|
|
/// JS in components.js handles the visual on/off state.
|
|
/// </summary>
|
|
public sealed class Switch : SwitchBase
|
|
{
|
|
private readonly byte[] _idData;
|
|
private readonly byte[] _nameData;
|
|
private readonly byte[] _checkedData;
|
|
private readonly byte[] _labelData;
|
|
|
|
public Switch(
|
|
string id,
|
|
string label = "",
|
|
string name = "",
|
|
bool isChecked = false)
|
|
{
|
|
_idData = id.ToUtf8Bytes();
|
|
_nameData = (string.IsNullOrEmpty(name) ? id : name).ToUtf8Bytes();
|
|
_checkedData = (isChecked ? "checked" : "").ToUtf8Bytes();
|
|
_labelData = string.IsNullOrEmpty(label)
|
|
? []
|
|
: $"""<span class="text-sm font-medium leading-none">{label}</span>""".ToUtf8Bytes();
|
|
}
|
|
|
|
protected override void RenderId(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_idData);
|
|
protected override void RenderName(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_nameData);
|
|
protected override void RenderChecked(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_checkedData);
|
|
protected override void RenderLabel(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_labelData);
|
|
}
|