|
1 | | -@namespace MudBlazor.StaticInput |
| 1 | +@namespace MudBlazor.StaticInput |
2 | 2 |
|
3 | 3 | @typeparam T |
4 | 4 | @inherits MudRadio<T> |
5 | 5 |
|
6 | 6 | <MudInputControl Class="@Classname" Style="@Style" Error="@HasErrors" ErrorText="@GetErrorText()" Required="@Required"> |
7 | 7 | <InputContent> |
8 | | - <label class="@LabelClassname" style="@Style" id="@($"static-radio-container-{_elementId}")" @onclick:stopPropagation="@StopClickPropagation"> |
| 8 | + <label class="@LabelClassname" style="@Style" @onclick:stopPropagation="@StopClickPropagation"> |
9 | 9 | <span tabindex="0" class="@IconClassname"> |
10 | 10 | <input id="@($"static-radio-{_elementId}")" tabindex="-1" @attributes="UserAttributes" type="radio" role="radio" |
11 | 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 | | - data-value="@Value" data-group-name="@ParentGroup?.GroupName" /> |
| 12 | + aria-checked="@(_isChecked.ToString().ToLower())" aria-disabled="@(GetDisabledState().ToString().ToLower())" @onclick:preventDefault="@GetReadOnlyState()"/> |
14 | 13 | <MudIcon Icon="@(ParentGroup?.CheckedIcon ?? CheckedIcon)" Color="HasErrors? Color.Error: ParentGroup?.Color ?? this.Color" Size="@Size" Disabled="@Disabled" |
15 | 14 | id="@($"radio-checked-icon-{_elementId}")" style="@($"display: {_checkedStyle}")" /> |
16 | 15 | <MudIcon Icon="@(ParentGroup?.UncheckedIcon ?? UncheckedIcon)" Color="HasErrors ? Color.Error: ParentGroup?.UncheckedColor ?? this.UncheckedColor ?? Color.Inherit" |
|
55 | 54 |
|
56 | 55 | protected override void OnParametersSet() |
57 | 56 | { |
58 | | - UserAttributes["data-static-component"] = true; |
| 57 | + if (ParentGroup is not null && ParentGroup.IsStatic()) |
| 58 | + { |
| 59 | + UserAttributes["data-mud-static-type"] = "radio"; |
| 60 | + UserAttributes["data-value"] = Value?.ToString(); |
| 61 | + UserAttributes["data-group-name"] = ParentGroup?.GroupName; |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + UserAttributes.Remove("data-mud-static-type"); |
| 66 | + UserAttributes.Remove("data-value"); |
| 67 | + UserAttributes.Remove("data-group-name"); |
| 68 | + UserAttributes.Remove("data-mud-static-initialized"); |
| 69 | + } |
59 | 70 |
|
60 | 71 | base.OnParametersSet(); |
61 | 72 | } |
62 | 73 | } |
63 | | - |
64 | | -<script> |
65 | | - document.addEventListener("DOMContentLoaded", function () { |
66 | | - document.querySelectorAll('.static-radio-input').forEach(function (radio) { |
67 | | - radio.addEventListener('change', function () { |
68 | | - const parentContainer = radio.closest("[role='radiogroup']"); |
69 | | - if (!parentContainer) return; |
70 | | -
|
71 | | - const hiddenInput = parentContainer.querySelector("input[type='hidden']"); |
72 | | - const selectedValue = radio.getAttribute('data-value'); |
73 | | - const groupName = radio.getAttribute('data-group-name'); |
74 | | -
|
75 | | - parentContainer.querySelectorAll('.static-radio-input').forEach(function (r) { |
76 | | - if (r !== radio) { |
77 | | - r.checked = false; |
78 | | - r.removeAttribute("name"); |
79 | | - r.setAttribute("checked", false); |
80 | | - r.setAttribute("aria-checked", false); |
81 | | - } |
82 | | -
|
83 | | - const radioId = r.id.split('-')[2]; |
84 | | - const checkedIcon = document.getElementById(`radio-checked-icon-${radioId}`); |
85 | | - const uncheckedIcon = document.getElementById(`radio-unchecked-icon-${radioId}`); |
86 | | -
|
87 | | - if (r.checked) { |
88 | | - checkedIcon.style.display = 'block'; |
89 | | - uncheckedIcon.style.display = 'none'; |
90 | | - r.setAttribute("checked", true); |
91 | | - r.setAttribute("aria-checked", true); |
92 | | - r.setAttribute("name", groupName); |
93 | | - // Update the hidden input value |
94 | | - if (hiddenInput) { |
95 | | - hiddenInput.value = selectedValue; |
96 | | - hiddenInput.setAttribute("value", selectedValue); |
97 | | - } |
98 | | - } else { |
99 | | - checkedIcon.style.display = 'none'; |
100 | | - uncheckedIcon.style.display = 'block'; |
101 | | - } |
102 | | - }); |
103 | | - }); |
104 | | - }); |
105 | | - }); |
106 | | -</script> |
0 commit comments