Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Components/Web/src/Forms/InputRadio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public class InputRadio<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTyp
/// </summary>
[Parameter] public string? Name { get; set; }

/// <summary>
/// Gets or sets the associated <see cref="ElementReference"/>.
/// <para>
/// May be <see langword="null"/> if accessed before the component is rendered.
/// </para>
/// </summary>
[DisallowNull] public ElementReference? Element { get; protected set; }

[CascadingParameter] private InputRadioContext? CascadedContext { get; set; }

/// <inheritdoc />
Expand Down Expand Up @@ -60,6 +68,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(5, "value", BindConverter.FormatValue(Value?.ToString()));
builder.AddAttribute(6, "checked", Context.CurrentValue?.Equals(Value));
builder.AddAttribute(7, "onchange", Context.ChangeEventCallback);
builder.AddElementReferenceCapture(8, __inputReference => Element = __inputReference);
builder.CloseElement();
}
}
2 changes: 2 additions & 0 deletions src/Components/Web/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Microsoft.AspNetCore.Components.Forms.InputRadio<TValue>.Element.get -> Microsoft.AspNetCore.Components.ElementReference?
Microsoft.AspNetCore.Components.Forms.InputRadio<TValue>.Element.set -> void
15 changes: 15 additions & 0 deletions src/Components/Web/test/Forms/InputRadioTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ public async Task RadioInputContextExistsWhenValidNameSupplied()
Assert.All(inputRadioComponents, inputRadio => Assert.Equal(groupName, inputRadio.GroupName));
}

[Fact]
public async Task InputElementIsAssignedSuccessfully()
{
var model = new TestModel();
var rootComponent = new TestInputRadioHostComponent<TestEnum>
{
EditContext = new EditContext(model),
InnerContent = RadioButtonsWithGroup(null, () => model.TestEnum)
};

var inputRadioComponents = await RenderAndGetTestInputComponentAsync(rootComponent);

Assert.All(inputRadioComponents, inputRadio => Assert.NotNull(inputRadio.Element));
}

private static RenderFragment RadioButtonsWithoutGroup(string name) => (builder) =>
{
foreach (var selectedValue in (TestEnum[])Enum.GetValues(typeof(TestEnum)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@
<button @onclick="async () => await inputCheckboxReference.Element.Value.FocusAsync()">Focus</button>
</p>


<p class="select-bool-values input-group">
T/F: 77 + 33 = 100<br>
<InputRadioGroup @bind-Value="person.IsSelectMathStatementTrue">
<InputRadio Value="true" @ref="inputRadioReference" class="input-control" />true<br>
<InputRadio Value="false" />false<br>
</InputRadioGroup>
<button @onclick="async () => await inputRadioReference.Element.Value.FocusAsync()">Focus</button>
</p>

</EditForm>

@code {
Expand All @@ -55,6 +63,7 @@
InputDate<DateTime> inputDateReference;
InputSelect<TicketClass> inputSelectReference;
InputCheckbox inputCheckboxReference;
InputRadio<bool> inputRadioReference;
InputFile inputFile;

Person person = new Person();
Expand All @@ -75,6 +84,8 @@

public bool AcceptsTerms { get; set; }

public bool IsSelectMathStatementTrue { get; set; }

public string Description { get; set; }

public TicketClass TicketClass { get; set; }
Expand Down