Files
decePubClient/Components/Daisy/DataInput/DToggle.razor
T

101 lines
3.1 KiB
Plaintext
Raw Normal View History

@inherits DInputBase<bool>
@* daisyUI Toggle: https://daisyui.com/components/toggle/ . With CheckedIcon/UncheckedIcon the toggle class moves to a
wrapper around the checkbox and the two icons (each must be a single element: daisyUI shows the wrapper's 2nd child
while unchecked and its 3rd while checked). daisyUI colors such a wrapper only through aria-checked, which a label
may not carry, so its color is a has-checked: variable instead. With LabelText all of it sits in a label.label. *@
@if (!HasIcons && LabelText is null)
{
@Checkbox(Cx("toggle", IsInvalid ? "toggle-error" : ColorClass, SizeClass, CssClass))
}
else if (!HasIcons)
{
<label class="label">
@Checkbox(Cx("toggle", IsInvalid ? "toggle-error" : ColorClass, SizeClass, CssClass))
@LabelText
</label>
}
else if (LabelText is null)
{
<label class="@WrapperClass">@IconParts</label>
}
else
{
<label class="label">
<span class="@WrapperClass">@IconParts</span>
@LabelText
</label>
}
@code {
[Parameter] public DColor Color { get; set; }
[Parameter] public DSize Size { get; set; }
[Parameter] public string LabelText { get; set; }
[Parameter] public RenderFragment CheckedIcon { get; set; }
[Parameter] public RenderFragment UncheckedIcon { get; set; }
bool HasIcons => CheckedIcon is not null || UncheckedIcon is not null;
string WrapperClass => Cx("toggle", IsInvalid ? "has-checked:[--input-color:var(--color-error)]" : WrapperColorClass, SizeClass, CssClass);
RenderFragment Checkbox(string cssClass) =>
@<input @attributes="InputAttributes" type="checkbox" class="@cssClass" name="@NameAttributeValue" checked="@CurrentValue"
@onchange="OnChange" aria-invalid="@(IsInvalid ? "true" : null)" />;
RenderFragment IconParts => @<text>
@Checkbox(null)
@if (UncheckedIcon is null)
{
<span></span>
}
else
{
@UncheckedIcon
}
@if (CheckedIcon is null)
{
<span></span>
}
else
{
@CheckedIcon
}
</text>;
string ColorClass => Color switch
{
DColor.Neutral => "toggle-neutral",
DColor.Primary => "toggle-primary",
DColor.Secondary => "toggle-secondary",
DColor.Accent => "toggle-accent",
DColor.Info => "toggle-info",
DColor.Success => "toggle-success",
DColor.Warning => "toggle-warning",
DColor.Error => "toggle-error",
_ => null
};
string WrapperColorClass => Color switch
{
DColor.Neutral => "has-checked:[--input-color:var(--color-neutral)]",
DColor.Primary => "has-checked:[--input-color:var(--color-primary)]",
DColor.Secondary => "has-checked:[--input-color:var(--color-secondary)]",
DColor.Accent => "has-checked:[--input-color:var(--color-accent)]",
DColor.Info => "has-checked:[--input-color:var(--color-info)]",
DColor.Success => "has-checked:[--input-color:var(--color-success)]",
DColor.Warning => "has-checked:[--input-color:var(--color-warning)]",
DColor.Error => "has-checked:[--input-color:var(--color-error)]",
_ => null
};
string SizeClass => Size switch
{
DSize.Xs => "toggle-xs",
DSize.Sm => "toggle-sm",
DSize.Lg => "toggle-lg",
DSize.Xl => "toggle-xl",
_ => null
};
void OnChange(ChangeEventArgs e) => CurrentValue = e.Value is true;
}