|
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="@_elementId" @onclick:stopPropagation="@StopClickPropagation"> |
| 8 | + <label class="@LabelClassname" style="@Style" id="@($"static-radio-container-{_elementId}")" @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 | | - class="mud-radio-input" checked="@IsChecked" name="@ParentGroup?.Name" disabled="@GetDisabledState()" value="@Value" |
12 | | - aria-checked="@(IsChecked.ToString().ToLower())" aria-disabled="@(GetDisabledState().ToString().ToLower())" @onclick:preventDefault="@GetReadOnlyState()" /> |
13 | | - <MudIcon id="@($"radio-svg-{_elementId}")" Disabled="@Disabled" Icon="@(IsChecked ? CheckedIcon : UncheckedIcon)" Color="HasErrors ? Color.Error : Color.Inherit" Size="@Size" /> |
| 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="@CheckedIcon" Color="HasErrors ? Color.Error : this.Color" Size="@Size" Disabled="@Disabled" |
| 14 | + id="@($"radio-checked-icon-{_elementId}")" style="@($"display: {_checkedStyle}")" /> |
| 15 | + <MudIcon Icon="@UncheckedIcon" Color="HasErrors ? Color.Error : this.UncheckedColor ?? Color.Inherit" Size="@Size" Disabled="@Disabled" |
| 16 | + id="@($"radio-unchecked-icon-{_elementId}")" style="@($"display: {_uncheckedStyle}")" /> |
14 | 17 | </span> |
15 | 18 | @if (!string.IsNullOrEmpty(Label)) |
16 | 19 | { |
|
30 | 33 | [CascadingParameter] |
31 | 34 | private MudStaticRadioGroup<T>? ParentGroup { get; set; } |
32 | 35 |
|
33 | | - private bool IsChecked => ParentGroup is not null && EqualityComparer<T>.Default.Equals(ParentGroup.Value, Value); |
| 36 | + [Parameter] |
| 37 | + public Expression<Func<T?>>? ValueExpression { get; set; } |
34 | 38 |
|
| 39 | + private bool _isChecked; |
35 | 40 | private readonly string _elementId = Guid.NewGuid().ToString()[..8]; |
| 41 | + private string _checkedStyle => _isChecked ? "block" : "none"; |
| 42 | + private string _uncheckedStyle => _isChecked ? "none" : "block"; |
36 | 43 |
|
37 | 44 | protected override void OnInitialized() |
38 | 45 | { |
39 | 46 | if (ParentGroup is null) |
40 | | - { |
41 | 47 | throw new InvalidOperationException($"{nameof(MudStaticRadio<T>)} must be used inside a {nameof(MudStaticRadioGroup<T>)}"); |
42 | | - } |
| 48 | + |
| 49 | + if (ParentGroup!.SelectedValue is not null) |
| 50 | + _isChecked = ParentGroup!.SelectedValue!.Equals(Value); |
43 | 51 |
|
44 | 52 | base.OnInitialized(); |
45 | 53 | } |
46 | 54 |
|
47 | 55 | protected override void OnParametersSet() |
48 | 56 | { |
49 | | - if (UserAttributes is null) |
50 | | - { |
51 | | - UserAttributes = new Dictionary<string, object?>(); |
52 | | - } |
53 | | - |
54 | 57 | UserAttributes["data-static-component"] = true; |
55 | 58 |
|
56 | 59 | base.OnParametersSet(); |
57 | 60 | } |
58 | 61 | } |
59 | 62 |
|
60 | 63 | <script> |
61 | | - (function() { |
62 | | - const radio = document.getElementById('static-radio-@_elementId'); |
63 | | - const svg = document.getElementById('radio-svg-@_elementId'); |
64 | | -
|
65 | | - const checkedIcon = '@(CheckedIcon)'; |
66 | | - const uncheckedIcon = '@(UncheckedIcon)'; |
| 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; |
67 | 69 |
|
68 | | - if (radio && svg) { |
69 | | - const handleChange = () => { |
70 | | - svg.innerHTML = radio.checked ? checkedIcon : uncheckedIcon; |
71 | | - }; |
| 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 | + } |
72 | 77 |
|
73 | | - radio.addEventListener('change', handleChange); |
| 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}`); |
74 | 81 |
|
75 | | - document.addEventListener('change', (e) => { |
76 | | - if (e.target.type === 'radio' && e.target.name === radio.name && e.target !== radio) { |
77 | | - handleChange(); |
78 | | - } |
| 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 | + }); |
79 | 93 | }); |
80 | | - } |
81 | | - })(); |
| 94 | + }); |
| 95 | + }); |
82 | 96 | </script> |
0 commit comments