Skip to content

Commit cf4dead

Browse files
authored
MudStaticRadioGroup: Update selected value (#47)
* RadioGroup: update selected value * MudStaticTextField: Adornment Icon Toggle (#46) * Add ability to toggle the adornment icon
1 parent b41f22d commit cf4dead

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

src/Components/MudStaticRadio.razor

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
<span tabindex="0" class="@IconClassname">
1010
<input id="@($"static-radio-{_elementId}")" tabindex="-1" @attributes="UserAttributes" type="radio" role="radio"
1111
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"
12+
aria-checked="@(_isChecked.ToString().ToLower())" aria-disabled="@(GetDisabledState().ToString().ToLower())" @onclick:preventDefault="@GetReadOnlyState()"
13+
data-value="@Value" data-group-name="@ParentGroup?.GroupName" />
14+
<MudIcon Icon="@(ParentGroup?.CheckedIcon ?? CheckedIcon)" Color="HasErrors? Color.Error: ParentGroup?.Color ?? this.Color" Size="@Size" Disabled="@Disabled"
1415
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+
<MudIcon Icon="@(ParentGroup?.UncheckedIcon ?? UncheckedIcon)" Color="HasErrors ? Color.Error: ParentGroup?.UncheckedColor ?? this.UncheckedColor ?? Color.Inherit"
1617
Size="@Size" Disabled="@Disabled" id="@($"radio-unchecked-icon-{_elementId}")" style="@($"display: {_uncheckedStyle}")" />
1718
</span>
1819
@if (!string.IsNullOrEmpty(Label))
@@ -67,6 +68,10 @@
6768
const parentContainer = radio.closest("[role='radiogroup']");
6869
if (!parentContainer) return;
6970
71+
const hiddenInput = parentContainer.querySelector("input[type='hidden']");
72+
const selectedValue = radio.getAttribute('data-value');
73+
const groupName = radio.getAttribute('data-group-name');
74+
7075
parentContainer.querySelectorAll('.static-radio-input').forEach(function (r) {
7176
if (r !== radio) {
7277
r.checked = false;
@@ -84,7 +89,12 @@
8489
uncheckedIcon.style.display = 'none';
8590
r.setAttribute("checked", true);
8691
r.setAttribute("aria-checked", true);
87-
r.setAttribute("name", "@ParentGroup?.GroupName");
92+
r.setAttribute("name", groupName);
93+
// Update the hidden input value
94+
if (hiddenInput) {
95+
hiddenInput.value = selectedValue;
96+
hiddenInput.setAttribute("value", selectedValue);
97+
}
8898
} else {
8999
checkedIcon.style.display = 'none';
90100
uncheckedIcon.style.display = 'block';

src/Components/NavMenu/MudStaticNavDrawerToggle.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Microsoft.AspNetCore.Components.Web;
2-
using Microsoft.AspNetCore.Components;
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.AspNetCore.Components.Web;
33

44
namespace MudBlazor.StaticInput;
55

tests/StaticInput.UnitTests/Components/RadioTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,25 @@ public async Task Radio_Should_Initialize_True()
186186
await Expect(radioC).ToBeCheckedAsync(new() { Checked = false });
187187
}
188188

189+
[Fact]
190+
public async Task Radio_Should_Update_Hidden_Input_On_Change()
191+
{
192+
var url = typeof(RadioGroupTest).ToQueryString();
193+
await Page.GotoAsync(url);
194+
195+
var radioA = Page.Locator("input.static-radio-input[value='A']");
196+
var radioB = Page.Locator("input.static-radio-input[value='B']");
197+
var hiddenInput = Page.Locator("input[type='hidden']");
198+
199+
await Expect(radioA).ToBeCheckedAsync();
200+
await Expect(radioB).Not.ToBeCheckedAsync();
201+
await Expect(hiddenInput).ToHaveAttributeAsync("value", "A");
202+
203+
await radioB.CheckAsync();
204+
205+
await Expect(radioA).Not.ToBeCheckedAsync();
206+
await Expect(radioB).ToBeCheckedAsync();
207+
await Expect(hiddenInput).ToHaveAttributeAsync("value", "B");
208+
}
189209
}
190210
}

0 commit comments

Comments
 (0)