36 lines
1.5 KiB
C#
36 lines
1.5 KiB
C#
namespace Htmx.ApiDemo.Templates.Components;
|
|
|
|
/// <summary>
|
|
/// shadcn-style Checkbox with an optional label.
|
|
/// </summary>
|
|
public sealed class Checkbox : CheckboxBase
|
|
{
|
|
private readonly byte[] _idData;
|
|
private readonly byte[] _nameData;
|
|
private readonly byte[] _valueData;
|
|
private readonly byte[] _checkedData;
|
|
private readonly byte[] _labelData;
|
|
|
|
public Checkbox(
|
|
string id,
|
|
string label = "",
|
|
string name = "",
|
|
string value = "true",
|
|
bool @checked = false)
|
|
{
|
|
_idData = id.ToUtf8Bytes();
|
|
_nameData = (string.IsNullOrEmpty(name) ? id : name).ToUtf8Bytes();
|
|
_valueData = value.ToUtf8Bytes();
|
|
_checkedData = (@checked ? "checked" : "").ToUtf8Bytes();
|
|
_labelData = string.IsNullOrEmpty(label)
|
|
? []
|
|
: $"""<label for="{id}" class="text-sm font-medium leading-none cursor-pointer peer-disabled:cursor-not-allowed peer-disabled:opacity-70">{label}</label>""".ToUtf8Bytes();
|
|
}
|
|
|
|
protected override void RenderId(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_idData);
|
|
protected override void RenderName(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_nameData);
|
|
protected override void RenderValue(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_valueData);
|
|
protected override void RenderChecked(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_checkedData);
|
|
protected override void RenderLabel(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_labelData);
|
|
}
|