diff --git a/src/Components/Web/src/Forms/InputRadio.cs b/src/Components/Web/src/Forms/InputRadio.cs index 4e35c44be8a0..afccdd2f3958 100644 --- a/src/Components/Web/src/Forms/InputRadio.cs +++ b/src/Components/Web/src/Forms/InputRadio.cs @@ -33,6 +33,14 @@ public class InputRadio<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTyp /// [Parameter] public string? Name { get; set; } + /// + /// Gets or sets the associated . + /// + /// May be if accessed before the component is rendered. + /// + /// + [DisallowNull] public ElementReference? Element { get; protected set; } + [CascadingParameter] private InputRadioContext? CascadedContext { get; set; } /// @@ -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(); } } diff --git a/src/Components/Web/src/PublicAPI.Unshipped.txt b/src/Components/Web/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..34c8afc7fb6e 100644 --- a/src/Components/Web/src/PublicAPI.Unshipped.txt +++ b/src/Components/Web/src/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ #nullable enable +Microsoft.AspNetCore.Components.Forms.InputRadio.Element.get -> Microsoft.AspNetCore.Components.ElementReference? +Microsoft.AspNetCore.Components.Forms.InputRadio.Element.set -> void diff --git a/src/Components/Web/test/Forms/InputRadioTest.cs b/src/Components/Web/test/Forms/InputRadioTest.cs index 3958b647966b..e25d729c0632 100644 --- a/src/Components/Web/test/Forms/InputRadioTest.cs +++ b/src/Components/Web/test/Forms/InputRadioTest.cs @@ -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 + { + 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))) diff --git a/src/Components/test/testassets/BasicTestApp/FormsTest/InputFocusComponent.razor b/src/Components/test/testassets/BasicTestApp/FormsTest/InputFocusComponent.razor index 26f41f0cd45c..8c131ee533de 100644 --- a/src/Components/test/testassets/BasicTestApp/FormsTest/InputFocusComponent.razor +++ b/src/Components/test/testassets/BasicTestApp/FormsTest/InputFocusComponent.razor @@ -45,7 +45,15 @@

- +

+ T/F: 77 + 33 = 100
+ + true
+ false
+
+ +

+ @code { @@ -55,6 +63,7 @@ InputDate inputDateReference; InputSelect inputSelectReference; InputCheckbox inputCheckboxReference; + InputRadio inputRadioReference; InputFile inputFile; Person person = new Person(); @@ -75,6 +84,8 @@ public bool AcceptsTerms { get; set; } + public bool IsSelectMathStatementTrue { get; set; } + public string Description { get; set; } public TicketClass TicketClass { get; set; }