|
| 1 | +@namespace MudBlazor.StaticInput |
| 2 | + |
| 3 | +@typeparam T |
| 4 | +@inherits MudRadio<T> |
| 5 | + |
| 6 | +<div class="mud-input-control mud-input-control-boolean-input mud-input-with-content"> |
| 7 | + <div class="mud-input-control-input-container"> |
| 8 | + <label class="mud-radio mud-input-content-placement-end" __internal_stoppropagation_onclick=""> |
| 9 | + <span class="mud-button-root mud-icon-button mud-ripple mud-ripple-radio mud-default-text hover:mud-default-hover" |
| 10 | + tabindex="0" |
| 11 | + @onclick:stopPropagation="true"> |
| 12 | + <input type="radio" |
| 13 | + class="mud-radio-input" |
| 14 | + tabindex="-1" |
| 15 | + role="radio" |
| 16 | + name="@ParentGroup?.GroupName" |
| 17 | + value="@Value" |
| 18 | + id="@($"static-radio-{_elementId}")" |
| 19 | + checked="@IsChecked" /> |
| 20 | + |
| 21 | + <svg class="mud-icon-root mud-svg-icon mud-icon-size-medium" |
| 22 | + focusable="false" |
| 23 | + viewBox="0 0 24 24" |
| 24 | + aria-hidden="true" |
| 25 | + role="img" |
| 26 | + id="@($"radio-svg-{_elementId}")"> |
| 27 | + <path d="M0 0h24v24H0z" fill="none"></path> |
| 28 | + <path d="@(IsChecked ? Checked : UnChecked)"></path> |
| 29 | + </svg> |
| 30 | + </span> |
| 31 | + |
| 32 | + <p class="mud-typography mud-typography-body1"> |
| 33 | + @ChildContent |
| 34 | + </p> |
| 35 | + </label> |
| 36 | + </div> |
| 37 | +</div> |
| 38 | + |
| 39 | +<script> |
| 40 | + (function() { |
| 41 | + const radio = document.getElementById('static-radio-@_elementId'); |
| 42 | + const svg = document.getElementById('radio-svg-@_elementId'); |
| 43 | +
|
| 44 | + if (radio && svg) { |
| 45 | + const handleChange = () => { |
| 46 | + const path = svg.querySelector('path:last-child'); |
| 47 | +
|
| 48 | + if (path) { |
| 49 | + path.setAttribute('d', radio.checked ? '@Checked' : '@UnChecked'); |
| 50 | + } |
| 51 | + }; |
| 52 | +
|
| 53 | + radio.addEventListener('change', handleChange); |
| 54 | +
|
| 55 | + document.addEventListener('change', (e) => { |
| 56 | + if (e.target.type === 'radio' && e.target.name === radio.name && e.target !== radio) { |
| 57 | + handleChange(); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + })(); |
| 62 | +</script> |
| 63 | + |
| 64 | +@code { |
| 65 | + [CascadingParameter] |
| 66 | + private MudStaticRadioGroup<T>? ParentGroup { get; set; } |
| 67 | + |
| 68 | + private bool IsChecked => ParentGroup is not null && EqualityComparer<T>.Default.Equals(ParentGroup.Value, Value); |
| 69 | + |
| 70 | + private const string UnChecked = "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"; |
| 71 | + private const string Checked = "M12 7c-2.76 0-5 2.24-5 5s2.24 5 5 5 5-2.24 5-5-2.24-5-5-5zm0-5C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"; |
| 72 | + |
| 73 | + private readonly string _elementId = Guid.NewGuid().ToString()[..8]; |
| 74 | + |
| 75 | + protected override void OnInitialized() |
| 76 | + { |
| 77 | + if (ParentGroup is null) |
| 78 | + { |
| 79 | + throw new InvalidOperationException($"{nameof(MudStaticRadio<T>)} must be used inside a {nameof(MudStaticRadioGroup<T>)}"); |
| 80 | + } |
| 81 | + |
| 82 | + base.OnInitialized(); |
| 83 | + } |
| 84 | + |
| 85 | + protected override void OnParametersSet() |
| 86 | + { |
| 87 | + if (UserAttributes is null) |
| 88 | + { |
| 89 | + UserAttributes = new Dictionary<string, object?>(); |
| 90 | + } |
| 91 | + |
| 92 | + UserAttributes["data-static-component"] = true; |
| 93 | + |
| 94 | + base.OnParametersSet(); |
| 95 | + } |
| 96 | + |
| 97 | + private async Task SelectValue() |
| 98 | + { |
| 99 | + if (ParentGroup is not null) |
| 100 | + { |
| 101 | + await ParentGroup.SetValue(Value); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments