-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabCheckbox.razor
74 lines (62 loc) · 2.16 KB
/
LabCheckbox.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<div class="form-check @formCheckSwitch">
<input class="form-check-input @Classes" type="@inputType" @bind="IsChecked" @oninput="OnCheckboxChanged" checked="@IsChecked" name="@Name" disabled="@IsDisabled">
@if (!string.IsNullOrEmpty(Label))
{
<label class="form-check-label">
@Label
</label>
}
@if (!string.IsNullOrEmpty(Help))
{
<div class="form-text">
@Help
</div>
}
</div>
@code {
[Parameter] public EventCallback<bool> IsCheckedChanged { get; set; }
[Parameter] public string Label { get; set; } = string.Empty;
[Parameter] public string Help { get; set; } = string.Empty;
[Parameter] public string Name { get; set; } = string.Empty;
[Parameter] public string Type { get; set; } = CheckboxType.Checkbox;
[Parameter] public bool IsDisabled { get; set; } = false;
[Parameter] public string Classes { get; set; } = string.Empty;
[Parameter] public bool IsChecked { get; set; } = false;
private string formCheckSwitch = string.Empty;
private string inputType = string.Empty;
private async Task OnCheckboxChanged(ChangeEventArgs e)
{
IsChecked = (bool)e.Value;
await IsCheckedChanged.InvokeAsync(IsChecked);
}
protected override Task OnInitializedAsync()
{
SetCheckboxType();
return base.OnInitializedAsync();
}
private void SetCheckboxType()
{
if (Type == CheckboxType.Switch)
{
formCheckSwitch = "form-switch";
inputType = CheckboxType.Checkbox;
}
else if (Type == CheckboxType.Checkbox)
{
inputType = CheckboxType.Checkbox;
}
}
private static class CheckboxType
{
public static string Checkbox => "checkbox";
public static string Switch => "switch";
}
}
@* Example *@
@* <LabCheckbox Type="checkbox" Label="Terms and condition" @bind-IsChecked="featureEnabled" />
<LabCheckbox Type="switch" Label="Terms and condition" @bind-IsChecked="featureEnabled" />
<p>Feature is @(featureEnabled ? "enabled" : "disabled").</p>
<p>@featureEnabled</p>
@code {
private bool featureEnabled = false;
} *@