|
| 1 | +@namespace MudBlazor.StaticInput |
| 2 | + |
| 3 | +@typeparam T |
| 4 | +@inherits MudRadio<T> |
| 5 | + |
| 6 | +<MudInputControl Class="@Classname" Style="@Style" Error="@HasErrors" ErrorText="@GetErrorText()" Required="@Required"> |
| 7 | + <InputContent> |
| 8 | + <label class="@LabelClassname" style="@Style" id="@($"static-radio-container-{_elementId}")" @onclick:stopPropagation="@StopClickPropagation"> |
| 9 | + <span tabindex="0" class="@IconClassname"> |
| 10 | + <input id="@($"static-radio-{_elementId}")" tabindex="-1" @attributes="UserAttributes" type="radio" role="radio" |
| 11 | + class="mud-radio-input static-radio-input" checked="@_isChecked" disabled="@GetDisabledState()" name="@(_isChecked ? ParentGroup?.GroupName : "")" value="@Value" |
| 12 | + aria-checked="@(_isChecked.ToString().ToLower())" aria-disabled="@(GetDisabledState().ToString().ToLower())" @onclick:preventDefault="@GetReadOnlyState()" /> |
| 13 | + <MudIcon Icon="@(ParentGroup.CheckedIcon ?? CheckedIcon)" Color="HasErrors ? Color.Error : ParentGroup?.Color ?? this.Color" Size="@Size" Disabled="@Disabled" |
| 14 | + id="@($"radio-checked-icon-{_elementId}")" style="@($"display: {_checkedStyle}")" /> |
| 15 | + <MudIcon Icon="@(ParentGroup.UncheckedIcon ?? UncheckedIcon)" Color="HasErrors ? Color.Error : ParentGroup?.UncheckedColor ?? this.UncheckedColor ?? Color.Inherit" |
| 16 | + Size="@Size" Disabled="@Disabled" id="@($"radio-unchecked-icon-{_elementId}")" style="@($"display: {_uncheckedStyle}")" /> |
| 17 | + </span> |
| 18 | + @if (!string.IsNullOrEmpty(Label)) |
| 19 | + { |
| 20 | + <MudText Color="HasErrors ? Color.Error : Color.Inherit">@Label</MudText> |
| 21 | + } |
| 22 | + @if (ChildContent is not null) |
| 23 | + { |
| 24 | + <MudText Color="HasErrors ? Color.Error : Color.Inherit"> |
| 25 | + @ChildContent |
| 26 | + </MudText> |
| 27 | + } |
| 28 | + </label> |
| 29 | + </InputContent> |
| 30 | +</MudInputControl> |
| 31 | + |
| 32 | +@code { |
| 33 | + [CascadingParameter] |
| 34 | + private MudStaticRadioGroup<T>? ParentGroup { get; set; } |
| 35 | + |
| 36 | + [Parameter] |
| 37 | + public Expression<Func<T?>>? ValueExpression { get; set; } |
| 38 | + |
| 39 | + private bool _isChecked; |
| 40 | + private readonly string _elementId = Guid.NewGuid().ToString()[..8]; |
| 41 | + private string _checkedStyle => _isChecked ? "block" : "none"; |
| 42 | + private string _uncheckedStyle => _isChecked ? "none" : "block"; |
| 43 | + |
| 44 | + protected override void OnInitialized() |
| 45 | + { |
| 46 | + if (ParentGroup is null) |
| 47 | + throw new InvalidOperationException($"{nameof(MudStaticRadio<T>)} must be used inside a {nameof(MudStaticRadioGroup<T>)}"); |
| 48 | + |
| 49 | + if (ParentGroup!.SelectedValue is not null) |
| 50 | + _isChecked = ParentGroup!.SelectedValue!.Equals(Value); |
| 51 | + |
| 52 | + base.OnInitialized(); |
| 53 | + } |
| 54 | + |
| 55 | + protected override void OnParametersSet() |
| 56 | + { |
| 57 | + UserAttributes["data-static-component"] = true; |
| 58 | + |
| 59 | + base.OnParametersSet(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +<script> |
| 64 | + document.addEventListener("DOMContentLoaded", function () { |
| 65 | + document.querySelectorAll('.static-radio-input').forEach(function (radio) { |
| 66 | + radio.addEventListener('change', function () { |
| 67 | + const parentContainer = radio.closest("[role='radiogroup']"); |
| 68 | + if (!parentContainer) return; |
| 69 | +
|
| 70 | + parentContainer.querySelectorAll('.static-radio-input').forEach(function (r) { |
| 71 | + if (r !== radio) { |
| 72 | + r.checked = false; |
| 73 | + r.removeAttribute("name"); |
| 74 | + r.setAttribute("checked", false); |
| 75 | + r.setAttribute("aria-checked", false); |
| 76 | + } |
| 77 | +
|
| 78 | + const radioId = r.id.split('-')[2]; |
| 79 | + const checkedIcon = document.getElementById(`radio-checked-icon-${radioId}`); |
| 80 | + const uncheckedIcon = document.getElementById(`radio-unchecked-icon-${radioId}`); |
| 81 | +
|
| 82 | + if (r.checked) { |
| 83 | + checkedIcon.style.display = 'block'; |
| 84 | + uncheckedIcon.style.display = 'none'; |
| 85 | + r.setAttribute("checked", true); |
| 86 | + r.setAttribute("aria-checked", true); |
| 87 | + r.setAttribute("name", "@ParentGroup?.GroupName"); |
| 88 | + } else { |
| 89 | + checkedIcon.style.display = 'none'; |
| 90 | + uncheckedIcon.style.display = 'block'; |
| 91 | + } |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | +</script> |
0 commit comments