Skip to content

Commit 7307650

Browse files
committed
add bases of static mudradio & mudradio group
1 parent c551989 commit 7307650

3 files changed

Lines changed: 179 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
@namespace MudBlazor.StaticInput
2+
3+
@typeparam T
4+
@inherits MudRadioGroup<T>
5+
6+
<MudInputControl Class="@Classname"
7+
Style="@Style"
8+
Error="@HasErrors"
9+
ErrorText="@GetErrorText()"
10+
Required="@Required">
11+
<InputContent>
12+
<CascadingValue Value="this">
13+
<div class="mud-input-control mud-input-control-boolean-input">
14+
<div class="mud-input-control-input-container">
15+
<div class="mud-radio-group" role="radiogroup">
16+
@ChildContent
17+
</div>
18+
</div>
19+
</div>
20+
</CascadingValue>
21+
</InputContent>
22+
</MudInputControl>
23+
24+
@code {
25+
[Parameter]
26+
public Expression<Func<T?>>? ValueExpression { get; set; }
27+
28+
public string GroupName { get; private set; } = Guid.NewGuid().ToString();
29+
30+
protected override void OnInitialized()
31+
{
32+
if (For != null)
33+
{
34+
string expression = For.ToString();
35+
int index = expression.LastIndexOf(").", StringComparison.Ordinal);
36+
37+
if (index > 0)
38+
{
39+
GroupName = expression[(index + 2)..];
40+
}
41+
}
42+
else
43+
{
44+
GroupName = Guid.NewGuid().ToString();
45+
}
46+
47+
base.OnInitialized();
48+
}
49+
50+
internal async Task SetValue(T? newValue)
51+
{
52+
if (Value?.Equals(newValue) != true)
53+
{
54+
Value = newValue;
55+
56+
await ValueChanged.InvokeAsync(newValue);
57+
58+
StateHasChanged();
59+
}
60+
}
61+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.AspNetCore.Components;
2+
3+
namespace MudBlazor.StaticInput
4+
{
5+
public partial class MudStaticRadioGroup<T> : MudRadioGroup<T>
6+
{
7+
/**********************************************
8+
* Hide these inherited properties to prevent *
9+
* consumers from modifying them directly. *
10+
**********************************************/
11+
protected new T? Value { get; set; }
12+
protected new EventCallback<T?> ValueChanged { get; set; }
13+
}
14+
}

0 commit comments

Comments
 (0)