From 3a896f559e4bdc8f339f07b33094137b128f8d87 Mon Sep 17 00:00:00 2001
From: Juan Barahona
Date: Thu, 13 Aug 2020 21:32:59 -0400
Subject: [PATCH 1/9] Expose "InputElement" on qualified classes
---
src/Components/Web/src/Forms/InputCheckbox.cs | 7 +++++++
src/Components/Web/src/Forms/InputDate.cs | 7 +++++++
src/Components/Web/src/Forms/InputNumber.cs | 7 +++++++
src/Components/Web/src/Forms/InputRadio.cs | 7 +++++++
src/Components/Web/src/Forms/InputSelect.cs | 9 ++++++++-
src/Components/Web/src/Forms/InputText.cs | 7 +++++++
src/Components/Web/src/Forms/InputTextArea.cs | 7 +++++++
7 files changed, 50 insertions(+), 1 deletion(-)
diff --git a/src/Components/Web/src/Forms/InputCheckbox.cs b/src/Components/Web/src/Forms/InputCheckbox.cs
index f1eb030a355f..c1077d997976 100644
--- a/src/Components/Web/src/Forms/InputCheckbox.cs
+++ b/src/Components/Web/src/Forms/InputCheckbox.cs
@@ -21,6 +21,12 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputCheckbox : InputBase
{
+ ///
+ /// Gets or sets the associated
+ /// This value is used to gain access to helper methods on the input elment.
+ ///
+ public ElementReference? InputElement { get; protected set; }
+
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
@@ -30,6 +36,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(3, "class", CssClass);
builder.AddAttribute(4, "checked", BindConverter.FormatValue(CurrentValue));
builder.AddAttribute(5, "onchange", EventCallback.Factory.CreateBinder(this, __value => CurrentValue = __value, CurrentValue));
+ builder.AddElementReferenceCapture(6, __inputReference => InputElement = __inputReference);
builder.CloseElement();
}
diff --git a/src/Components/Web/src/Forms/InputDate.cs b/src/Components/Web/src/Forms/InputDate.cs
index 4372646c7b15..c102fe91f7a7 100644
--- a/src/Components/Web/src/Forms/InputDate.cs
+++ b/src/Components/Web/src/Forms/InputDate.cs
@@ -21,6 +21,12 @@ public class InputDate : InputBase
///
[Parameter] public string ParsingErrorMessage { get; set; } = "The {0} field must be a date.";
+ ///
+ /// Gets or sets the associated
+ /// This value is used to gain access to helper methods on the input elment.
+ ///
+ public ElementReference? InputElement { get; protected set; }
+
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
@@ -30,6 +36,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(3, "class", CssClass);
builder.AddAttribute(4, "value", BindConverter.FormatValue(CurrentValueAsString));
builder.AddAttribute(5, "onchange", EventCallback.Factory.CreateBinder(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
+ builder.AddElementReferenceCapture(6, __inputReference => InputElement = __inputReference);
builder.CloseElement();
}
diff --git a/src/Components/Web/src/Forms/InputNumber.cs b/src/Components/Web/src/Forms/InputNumber.cs
index 51ac2c524151..ba341a449f5d 100644
--- a/src/Components/Web/src/Forms/InputNumber.cs
+++ b/src/Components/Web/src/Forms/InputNumber.cs
@@ -41,6 +41,12 @@ static InputNumber()
///
[Parameter] public string ParsingErrorMessage { get; set; } = "The {0} field must be a number.";
+ ///
+ /// Gets or sets the associated
+ /// This value is used to gain access to helper methods on the input elment.
+ ///
+ public ElementReference? InputElement { get; protected set; }
+
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
@@ -51,6 +57,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(4, "class", CssClass);
builder.AddAttribute(5, "value", BindConverter.FormatValue(CurrentValueAsString));
builder.AddAttribute(6, "onchange", EventCallback.Factory.CreateBinder(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
+ builder.AddElementReferenceCapture(7, __inputReference => InputElement = __inputReference);
builder.CloseElement();
}
diff --git a/src/Components/Web/src/Forms/InputRadio.cs b/src/Components/Web/src/Forms/InputRadio.cs
index 4a4ad46dc3a5..9860800287a7 100644
--- a/src/Components/Web/src/Forms/InputRadio.cs
+++ b/src/Components/Web/src/Forms/InputRadio.cs
@@ -39,6 +39,12 @@ public class InputRadio : ComponentBase
[CascadingParameter] private InputRadioContext? CascadedContext { get; set; }
+ ///
+ /// Gets or sets the associated
+ /// This value is used to gain access to helper methods on the input elment.
+ ///
+ public ElementReference? InputElement { get; protected set; }
+
private string GetCssClass(string fieldClass)
{
if (AdditionalAttributes != null &&
@@ -76,6 +82,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 => InputElement = __inputReference);
builder.CloseElement();
}
}
diff --git a/src/Components/Web/src/Forms/InputSelect.cs b/src/Components/Web/src/Forms/InputSelect.cs
index b7d5dd702552..044335251d56 100644
--- a/src/Components/Web/src/Forms/InputSelect.cs
+++ b/src/Components/Web/src/Forms/InputSelect.cs
@@ -16,6 +16,12 @@ public class InputSelect : InputBase
///
[Parameter] public RenderFragment? ChildContent { get; set; }
+ ///
+ /// Gets or sets the associated
+ /// This value is used to gain access to helper methods on the input elment.
+ ///
+ public ElementReference? InputElement { get; protected set; }
+
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
@@ -24,7 +30,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(2, "class", CssClass);
builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValueAsString));
builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
- builder.AddContent(5, ChildContent);
+ builder.AddElementReferenceCapture(5, __inputReference => InputElement = __inputReference);
+ builder.AddContent(6, ChildContent);
builder.CloseElement();
}
diff --git a/src/Components/Web/src/Forms/InputText.cs b/src/Components/Web/src/Forms/InputText.cs
index 17561eb942f8..1c6c70872230 100644
--- a/src/Components/Web/src/Forms/InputText.cs
+++ b/src/Components/Web/src/Forms/InputText.cs
@@ -20,6 +20,12 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputText : InputBase
{
+ ///
+ /// Gets or sets the associated
+ /// This value is used to gain access to helper methods on the input elment.
+ ///
+ public ElementReference? InputElement { get; protected set; }
+
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
@@ -28,6 +34,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(2, "class", CssClass);
builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValue));
builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
+ builder.AddElementReferenceCapture(5, __inputReference => InputElement = __inputReference);
builder.CloseElement();
}
diff --git a/src/Components/Web/src/Forms/InputTextArea.cs b/src/Components/Web/src/Forms/InputTextArea.cs
index 80958270322b..ed8871544d68 100644
--- a/src/Components/Web/src/Forms/InputTextArea.cs
+++ b/src/Components/Web/src/Forms/InputTextArea.cs
@@ -20,6 +20,12 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputTextArea : InputBase
{
+ ///
+ /// Gets or sets the associated
+ /// This value is used to gain access to helper methods on the input elment.
+ ///
+ public ElementReference? InputElement { get; protected set; }
+
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
@@ -28,6 +34,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(2, "class", CssClass);
builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValue));
builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
+ builder.AddElementReferenceCapture(5, __inputReference => InputElement = __inputReference);
builder.CloseElement();
}
From 5474c839c79623083344e0bb5063a9d8b7edc37a Mon Sep 17 00:00:00 2001
From: Juan Barahona
Date: Thu, 13 Aug 2020 21:33:35 -0400
Subject: [PATCH 2/9] Add test to check "Input Element" was successfully set
---
.../Web/test/Forms/InputDateTest.cs | 18 ++++++++++
.../Web/test/Forms/InputNumberTest.cs | 18 ++++++++++
.../Web/test/Forms/InputRadioTest.cs | 15 ++++++++
.../Web/test/Forms/InputSelectTest.cs | 18 ++++++++++
.../Web/test/Forms/InputTextAreaTest.cs | 35 +++++++++++++++++++
.../Web/test/Forms/InputTextTest.cs | 35 +++++++++++++++++++
6 files changed, 139 insertions(+)
create mode 100644 src/Components/Web/test/Forms/InputTextAreaTest.cs
create mode 100644 src/Components/Web/test/Forms/InputTextTest.cs
diff --git a/src/Components/Web/test/Forms/InputDateTest.cs b/src/Components/Web/test/Forms/InputDateTest.cs
index 9c6f87b8321b..42290c395a30 100644
--- a/src/Components/Web/test/Forms/InputDateTest.cs
+++ b/src/Components/Web/test/Forms/InputDateTest.cs
@@ -36,6 +36,24 @@ public async Task ValidationErrorUsesDisplayAttributeName()
Assert.Contains("The Date property field must be a date.", validationMessages);
}
+ [Fact]
+ public async Task InputElementIsAssignedSuccessfully()
+ {
+ // Arrange
+ var model = new TestModel();
+ var rootComponent = new TestInputHostComponent
+ {
+ EditContext = new EditContext(model),
+ ValueExpression = () => model.DateProperty,
+ };
+
+ // Act
+ var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+
+ // Assert
+ Assert.NotNull(inputSelectComponent.InputElement);
+ }
+
private class TestModel
{
public DateTime DateProperty { get; set; }
diff --git a/src/Components/Web/test/Forms/InputNumberTest.cs b/src/Components/Web/test/Forms/InputNumberTest.cs
index 6916f0e06e03..283634fc7cea 100644
--- a/src/Components/Web/test/Forms/InputNumberTest.cs
+++ b/src/Components/Web/test/Forms/InputNumberTest.cs
@@ -35,6 +35,24 @@ public async Task ValidationErrorUsesDisplayAttributeName()
Assert.Contains("The Some number field must be a number.", validationMessages);
}
+ [Fact]
+ public async Task InputElementIsAssignedSuccessfully()
+ {
+ // Arrange
+ var model = new TestModel();
+ var rootComponent = new TestInputHostComponent
+ {
+ EditContext = new EditContext(model),
+ ValueExpression = () => model.SomeNumber,
+ };
+
+ // Act
+ var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+
+ // Assert
+ Assert.NotNull(inputSelectComponent.InputElement);
+ }
+
private class TestModel
{
public int SomeNumber { get; set; }
diff --git a/src/Components/Web/test/Forms/InputRadioTest.cs b/src/Components/Web/test/Forms/InputRadioTest.cs
index 1447a16316f3..a94adf3d5349 100644
--- a/src/Components/Web/test/Forms/InputRadioTest.cs
+++ b/src/Components/Web/test/Forms/InputRadioTest.cs
@@ -60,6 +60,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.InputElement));
+ }
+
private static RenderFragment RadioButtonsWithoutGroup(string name) => (builder) =>
{
foreach (var selectedValue in (TestEnum[])Enum.GetValues(typeof(TestEnum)))
diff --git a/src/Components/Web/test/Forms/InputSelectTest.cs b/src/Components/Web/test/Forms/InputSelectTest.cs
index 8945867fd0da..729d8ca7020c 100644
--- a/src/Components/Web/test/Forms/InputSelectTest.cs
+++ b/src/Components/Web/test/Forms/InputSelectTest.cs
@@ -194,6 +194,24 @@ public async Task ValidationErrorUsesDisplayAttributeName()
Assert.Contains("The Some number field is not valid.", validationMessages);
}
+ [Fact]
+ public async Task InputElementIsAssignedSuccessfully()
+ {
+ // Arrange
+ var model = new TestModel();
+ var rootComponent = new TestInputHostComponent>
+ {
+ EditContext = new EditContext(model),
+ ValueExpression = () => model.NotNullableInt,
+ };
+
+ // Act
+ var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+
+ // Assert
+ Assert.NotNull(inputSelectComponent.InputElement);
+ }
+
enum TestEnum
{
One,
diff --git a/src/Components/Web/test/Forms/InputTextAreaTest.cs b/src/Components/Web/test/Forms/InputTextAreaTest.cs
new file mode 100644
index 000000000000..2c35d6f78dd3
--- /dev/null
+++ b/src/Components/Web/test/Forms/InputTextAreaTest.cs
@@ -0,0 +1,35 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace Microsoft.AspNetCore.Components.Forms
+{
+ public class InputTextAreaTest
+ {
+ [Fact]
+ public async Task InputElementIsAssignedSuccessfully()
+ {
+ // Arrange
+ var model = new TestModel();
+ var rootComponent = new TestInputHostComponent
+ {
+ EditContext = new EditContext(model),
+ ValueExpression = () => model.StringProperty,
+ };
+
+ // Act
+ var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+
+ // Assert
+ Assert.NotNull(inputSelectComponent.InputElement);
+ }
+
+ private class TestModel
+ {
+ public string StringProperty { get; set; }
+ }
+ }
+}
diff --git a/src/Components/Web/test/Forms/InputTextTest.cs b/src/Components/Web/test/Forms/InputTextTest.cs
new file mode 100644
index 000000000000..93f4a6486f4b
--- /dev/null
+++ b/src/Components/Web/test/Forms/InputTextTest.cs
@@ -0,0 +1,35 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace Microsoft.AspNetCore.Components.Forms
+{
+ public class InputTextTest
+ {
+ [Fact]
+ public async Task InputElementIsAssignedSuccessfully()
+ {
+ // Arrange
+ var model = new TestModel();
+ var rootComponent = new TestInputHostComponent
+ {
+ EditContext = new EditContext(model),
+ ValueExpression = () => model.StringProperty,
+ };
+
+ // Act
+ var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+
+ // Assert
+ Assert.NotNull(inputSelectComponent.InputElement);
+ }
+
+ private class TestModel
+ {
+ public string StringProperty { get; set; }
+ }
+ }
+}
From 58202f6dcca929bc5990f37a8ba019f39e3b602d Mon Sep 17 00:00:00 2001
From: Juan Barahona
Date: Thu, 13 Aug 2020 21:43:05 -0400
Subject: [PATCH 3/9] Cleaning unneeded imports
---
src/Components/Web/test/Forms/InputTextAreaTest.cs | 1 -
src/Components/Web/test/Forms/InputTextTest.cs | 1 -
2 files changed, 2 deletions(-)
diff --git a/src/Components/Web/test/Forms/InputTextAreaTest.cs b/src/Components/Web/test/Forms/InputTextAreaTest.cs
index 2c35d6f78dd3..89f59ddc4c21 100644
--- a/src/Components/Web/test/Forms/InputTextAreaTest.cs
+++ b/src/Components/Web/test/Forms/InputTextAreaTest.cs
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
diff --git a/src/Components/Web/test/Forms/InputTextTest.cs b/src/Components/Web/test/Forms/InputTextTest.cs
index 93f4a6486f4b..8a53b107a781 100644
--- a/src/Components/Web/test/Forms/InputTextTest.cs
+++ b/src/Components/Web/test/Forms/InputTextTest.cs
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
From 62d4515151843e570b0496f6428d61f162881522 Mon Sep 17 00:00:00 2001
From: Juan Barahona
Date: Thu, 13 Aug 2020 22:11:39 -0400
Subject: [PATCH 4/9] Fixing typo
---
src/Components/Web/src/Forms/InputCheckbox.cs | 2 +-
src/Components/Web/src/Forms/InputDate.cs | 2 +-
src/Components/Web/src/Forms/InputNumber.cs | 2 +-
src/Components/Web/src/Forms/InputRadio.cs | 2 +-
src/Components/Web/src/Forms/InputSelect.cs | 2 +-
src/Components/Web/src/Forms/InputText.cs | 2 +-
src/Components/Web/src/Forms/InputTextArea.cs | 2 +-
7 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/src/Components/Web/src/Forms/InputCheckbox.cs b/src/Components/Web/src/Forms/InputCheckbox.cs
index c1077d997976..8907decebe8c 100644
--- a/src/Components/Web/src/Forms/InputCheckbox.cs
+++ b/src/Components/Web/src/Forms/InputCheckbox.cs
@@ -23,7 +23,7 @@ public class InputCheckbox : InputBase
{
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input elment.
+ /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputDate.cs b/src/Components/Web/src/Forms/InputDate.cs
index c102fe91f7a7..a087001e014a 100644
--- a/src/Components/Web/src/Forms/InputDate.cs
+++ b/src/Components/Web/src/Forms/InputDate.cs
@@ -23,7 +23,7 @@ public class InputDate : InputBase
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input elment.
+ /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputNumber.cs b/src/Components/Web/src/Forms/InputNumber.cs
index ba341a449f5d..588aac1743d2 100644
--- a/src/Components/Web/src/Forms/InputNumber.cs
+++ b/src/Components/Web/src/Forms/InputNumber.cs
@@ -43,7 +43,7 @@ static InputNumber()
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input elment.
+ /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputRadio.cs b/src/Components/Web/src/Forms/InputRadio.cs
index 9860800287a7..e530c208d52b 100644
--- a/src/Components/Web/src/Forms/InputRadio.cs
+++ b/src/Components/Web/src/Forms/InputRadio.cs
@@ -41,7 +41,7 @@ public class InputRadio : ComponentBase
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input elment.
+ /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputSelect.cs b/src/Components/Web/src/Forms/InputSelect.cs
index 044335251d56..7da0bf910a1c 100644
--- a/src/Components/Web/src/Forms/InputSelect.cs
+++ b/src/Components/Web/src/Forms/InputSelect.cs
@@ -18,7 +18,7 @@ public class InputSelect : InputBase
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input elment.
+ /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputText.cs b/src/Components/Web/src/Forms/InputText.cs
index 1c6c70872230..0411ad9c012b 100644
--- a/src/Components/Web/src/Forms/InputText.cs
+++ b/src/Components/Web/src/Forms/InputText.cs
@@ -22,7 +22,7 @@ public class InputText : InputBase
{
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input elment.
+ /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputTextArea.cs b/src/Components/Web/src/Forms/InputTextArea.cs
index ed8871544d68..90410277e7c5 100644
--- a/src/Components/Web/src/Forms/InputTextArea.cs
+++ b/src/Components/Web/src/Forms/InputTextArea.cs
@@ -22,7 +22,7 @@ public class InputTextArea : InputBase
{
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input elment.
+ /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
From e4cae59c3249bd50f038df6a07039c0e7001d3c6 Mon Sep 17 00:00:00 2001
From: Juan Barahona
Date: Thu, 13 Aug 2020 22:18:45 -0400
Subject: [PATCH 5/9] Removing unneeded documentation
---
src/Components/Web/src/Forms/InputCheckbox.cs | 1 -
src/Components/Web/src/Forms/InputDate.cs | 1 -
src/Components/Web/src/Forms/InputNumber.cs | 1 -
src/Components/Web/src/Forms/InputRadio.cs | 1 -
src/Components/Web/src/Forms/InputSelect.cs | 1 -
src/Components/Web/src/Forms/InputText.cs | 1 -
src/Components/Web/src/Forms/InputTextArea.cs | 1 -
7 files changed, 7 deletions(-)
diff --git a/src/Components/Web/src/Forms/InputCheckbox.cs b/src/Components/Web/src/Forms/InputCheckbox.cs
index 8907decebe8c..7080fe5f22c2 100644
--- a/src/Components/Web/src/Forms/InputCheckbox.cs
+++ b/src/Components/Web/src/Forms/InputCheckbox.cs
@@ -23,7 +23,6 @@ public class InputCheckbox : InputBase
{
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputDate.cs b/src/Components/Web/src/Forms/InputDate.cs
index a087001e014a..3d6245759253 100644
--- a/src/Components/Web/src/Forms/InputDate.cs
+++ b/src/Components/Web/src/Forms/InputDate.cs
@@ -23,7 +23,6 @@ public class InputDate : InputBase
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputNumber.cs b/src/Components/Web/src/Forms/InputNumber.cs
index 588aac1743d2..e47a8d5a22cc 100644
--- a/src/Components/Web/src/Forms/InputNumber.cs
+++ b/src/Components/Web/src/Forms/InputNumber.cs
@@ -43,7 +43,6 @@ static InputNumber()
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputRadio.cs b/src/Components/Web/src/Forms/InputRadio.cs
index e530c208d52b..7a3d649dc7f0 100644
--- a/src/Components/Web/src/Forms/InputRadio.cs
+++ b/src/Components/Web/src/Forms/InputRadio.cs
@@ -41,7 +41,6 @@ public class InputRadio : ComponentBase
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputSelect.cs b/src/Components/Web/src/Forms/InputSelect.cs
index 7da0bf910a1c..521c262a81f7 100644
--- a/src/Components/Web/src/Forms/InputSelect.cs
+++ b/src/Components/Web/src/Forms/InputSelect.cs
@@ -18,7 +18,6 @@ public class InputSelect : InputBase
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputText.cs b/src/Components/Web/src/Forms/InputText.cs
index 0411ad9c012b..95a7350bcdbd 100644
--- a/src/Components/Web/src/Forms/InputText.cs
+++ b/src/Components/Web/src/Forms/InputText.cs
@@ -22,7 +22,6 @@ public class InputText : InputBase
{
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
diff --git a/src/Components/Web/src/Forms/InputTextArea.cs b/src/Components/Web/src/Forms/InputTextArea.cs
index 90410277e7c5..cacfab6e613e 100644
--- a/src/Components/Web/src/Forms/InputTextArea.cs
+++ b/src/Components/Web/src/Forms/InputTextArea.cs
@@ -22,7 +22,6 @@ public class InputTextArea : InputBase
{
///
/// Gets or sets the associated
- /// This value is used to gain access to helper methods on the input element.
///
public ElementReference? InputElement { get; protected set; }
From 99b1cc71fb4cc404138f99dd7262bcd0935fae69 Mon Sep 17 00:00:00 2001
From: Juan Barahona
Date: Thu, 13 Aug 2020 22:54:13 -0400
Subject: [PATCH 6/9] Making InputElement and SelectElement non-null
---
src/Components/Web/src/Forms/InputCheckbox.cs | 8 +++++++-
src/Components/Web/src/Forms/InputDate.cs | 7 ++++++-
src/Components/Web/src/Forms/InputNumber.cs | 7 ++++++-
src/Components/Web/src/Forms/InputRadio.cs | 8 +++++++-
src/Components/Web/src/Forms/InputSelect.cs | 11 +++++++++--
src/Components/Web/src/Forms/InputText.cs | 9 ++++++++-
src/Components/Web/src/Forms/InputTextArea.cs | 9 ++++++++-
src/Components/Web/test/Forms/InputDateTest.cs | 3 ++-
src/Components/Web/test/Forms/InputNumberTest.cs | 3 ++-
src/Components/Web/test/Forms/InputRadioTest.cs | 2 +-
src/Components/Web/test/Forms/InputSelectTest.cs | 3 ++-
src/Components/Web/test/Forms/InputTextAreaTest.cs | 3 ++-
src/Components/Web/test/Forms/InputTextTest.cs | 3 ++-
13 files changed, 62 insertions(+), 14 deletions(-)
diff --git a/src/Components/Web/src/Forms/InputCheckbox.cs b/src/Components/Web/src/Forms/InputCheckbox.cs
index 7080fe5f22c2..eb30ba8a3969 100644
--- a/src/Components/Web/src/Forms/InputCheckbox.cs
+++ b/src/Components/Web/src/Forms/InputCheckbox.cs
@@ -21,10 +21,16 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputCheckbox : InputBase
{
+ private ElementReference? _inputElement;
+
///
/// Gets or sets the associated
///
- public ElementReference? InputElement { get; protected set; }
+ public ElementReference InputElement
+ {
+ get => _inputElement ?? throw new InvalidOperationException($"Component must be rendered before {nameof(InputElement)} can be accessed.");
+ protected set => _inputElement = value;
+ }
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
diff --git a/src/Components/Web/src/Forms/InputDate.cs b/src/Components/Web/src/Forms/InputDate.cs
index 3d6245759253..7f24b9a5c767 100644
--- a/src/Components/Web/src/Forms/InputDate.cs
+++ b/src/Components/Web/src/Forms/InputDate.cs
@@ -15,6 +15,7 @@ namespace Microsoft.AspNetCore.Components.Forms
public class InputDate : InputBase
{
private const string DateFormat = "yyyy-MM-dd"; // Compatible with HTML date inputs
+ private ElementReference? _inputElement;
///
/// Gets or sets the error message used when displaying an a parsing error.
@@ -24,7 +25,11 @@ public class InputDate : InputBase
///
/// Gets or sets the associated
///
- public ElementReference? InputElement { get; protected set; }
+ public ElementReference InputElement
+ {
+ get => _inputElement ?? throw new InvalidOperationException($"Component must be rendered before {nameof(InputElement)} can be accessed.");
+ protected set => _inputElement = value;
+ }
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
diff --git a/src/Components/Web/src/Forms/InputNumber.cs b/src/Components/Web/src/Forms/InputNumber.cs
index e47a8d5a22cc..95ec963612c9 100644
--- a/src/Components/Web/src/Forms/InputNumber.cs
+++ b/src/Components/Web/src/Forms/InputNumber.cs
@@ -15,6 +15,7 @@ namespace Microsoft.AspNetCore.Components.Forms
public class InputNumber : InputBase
{
private readonly static string _stepAttributeValue; // Null by default, so only allows whole numbers as per HTML spec
+ private ElementReference? _inputElement;
static InputNumber()
{
@@ -44,7 +45,11 @@ static InputNumber()
///
/// Gets or sets the associated
///
- public ElementReference? InputElement { get; protected set; }
+ public ElementReference InputElement
+ {
+ get => _inputElement ?? throw new InvalidOperationException($"Component must be rendered before {nameof(InputElement)} can be accessed.");
+ protected set => _inputElement = value;
+ }
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
diff --git a/src/Components/Web/src/Forms/InputRadio.cs b/src/Components/Web/src/Forms/InputRadio.cs
index 7a3d649dc7f0..ee0adc4828c6 100644
--- a/src/Components/Web/src/Forms/InputRadio.cs
+++ b/src/Components/Web/src/Forms/InputRadio.cs
@@ -14,6 +14,8 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputRadio : ComponentBase
{
+ private ElementReference? _inputElement;
+
///
/// Gets context for this .
///
@@ -42,7 +44,11 @@ public class InputRadio : ComponentBase
///
/// Gets or sets the associated
///
- public ElementReference? InputElement { get; protected set; }
+ public ElementReference InputElement
+ {
+ get => _inputElement ?? throw new InvalidOperationException($"Component must be rendered before {nameof(InputElement)} can be accessed.");
+ protected set => _inputElement = value;
+ }
private string GetCssClass(string fieldClass)
{
diff --git a/src/Components/Web/src/Forms/InputSelect.cs b/src/Components/Web/src/Forms/InputSelect.cs
index 521c262a81f7..167d9832312a 100644
--- a/src/Components/Web/src/Forms/InputSelect.cs
+++ b/src/Components/Web/src/Forms/InputSelect.cs
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components.Rendering;
@@ -11,6 +12,8 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputSelect : InputBase
{
+ private ElementReference? _selectElement;
+
///
/// Gets or sets the child content to be rendering inside the select element.
///
@@ -19,7 +22,11 @@ public class InputSelect : InputBase
///
/// Gets or sets the associated
///
- public ElementReference? InputElement { get; protected set; }
+ public ElementReference SelectElement
+ {
+ get => _selectElement ?? throw new InvalidOperationException($"Component must be rendered before {nameof(SelectElement)} can be accessed.");
+ protected set => _selectElement = value;
+ }
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
@@ -29,7 +36,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(2, "class", CssClass);
builder.AddAttribute(3, "value", BindConverter.FormatValue(CurrentValueAsString));
builder.AddAttribute(4, "onchange", EventCallback.Factory.CreateBinder(this, __value => CurrentValueAsString = __value, CurrentValueAsString));
- builder.AddElementReferenceCapture(5, __inputReference => InputElement = __inputReference);
+ builder.AddElementReferenceCapture(5, __selectReference => SelectElement = __selectReference);
builder.AddContent(6, ChildContent);
builder.CloseElement();
}
diff --git a/src/Components/Web/src/Forms/InputText.cs b/src/Components/Web/src/Forms/InputText.cs
index 95a7350bcdbd..290b32c48acf 100644
--- a/src/Components/Web/src/Forms/InputText.cs
+++ b/src/Components/Web/src/Forms/InputText.cs
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components.Rendering;
@@ -20,10 +21,16 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputText : InputBase
{
+ private ElementReference? _inputElement;
+
///
/// Gets or sets the associated
///
- public ElementReference? InputElement { get; protected set; }
+ public ElementReference InputElement
+ {
+ get => _inputElement ?? throw new InvalidOperationException($"Component must be rendered before {nameof(InputElement)} can be accessed.");
+ protected set => _inputElement = value;
+ }
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
diff --git a/src/Components/Web/src/Forms/InputTextArea.cs b/src/Components/Web/src/Forms/InputTextArea.cs
index cacfab6e613e..169be6f25e72 100644
--- a/src/Components/Web/src/Forms/InputTextArea.cs
+++ b/src/Components/Web/src/Forms/InputTextArea.cs
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components.Rendering;
@@ -20,10 +21,16 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputTextArea : InputBase
{
+ private ElementReference? _inputElement;
+
///
/// Gets or sets the associated
///
- public ElementReference? InputElement { get; protected set; }
+ public ElementReference InputElement
+ {
+ get => _inputElement ?? throw new InvalidOperationException($"Component must be rendered before {nameof(InputElement)} can be accessed.");
+ protected set => _inputElement = value;
+ }
///
protected override void BuildRenderTree(RenderTreeBuilder builder)
diff --git a/src/Components/Web/test/Forms/InputDateTest.cs b/src/Components/Web/test/Forms/InputDateTest.cs
index 42290c395a30..f37e692f1deb 100644
--- a/src/Components/Web/test/Forms/InputDateTest.cs
+++ b/src/Components/Web/test/Forms/InputDateTest.cs
@@ -49,9 +49,10 @@ public async Task InputElementIsAssignedSuccessfully()
// Act
var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+ var exception = Record.Exception(() => inputSelectComponent.InputElement);
// Assert
- Assert.NotNull(inputSelectComponent.InputElement);
+ Assert.Null(exception);
}
private class TestModel
diff --git a/src/Components/Web/test/Forms/InputNumberTest.cs b/src/Components/Web/test/Forms/InputNumberTest.cs
index 283634fc7cea..d9a9fead869c 100644
--- a/src/Components/Web/test/Forms/InputNumberTest.cs
+++ b/src/Components/Web/test/Forms/InputNumberTest.cs
@@ -48,9 +48,10 @@ public async Task InputElementIsAssignedSuccessfully()
// Act
var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+ var exception = Record.Exception(() => inputSelectComponent.InputElement);
// Assert
- Assert.NotNull(inputSelectComponent.InputElement);
+ Assert.Null(exception);
}
private class TestModel
diff --git a/src/Components/Web/test/Forms/InputRadioTest.cs b/src/Components/Web/test/Forms/InputRadioTest.cs
index a94adf3d5349..f45bbe5b36c2 100644
--- a/src/Components/Web/test/Forms/InputRadioTest.cs
+++ b/src/Components/Web/test/Forms/InputRadioTest.cs
@@ -72,7 +72,7 @@ public async Task InputElementIsAssignedSuccessfully()
var inputRadioComponents = await RenderAndGetTestInputComponentAsync(rootComponent);
- Assert.All(inputRadioComponents, inputRadio => Assert.NotNull(inputRadio.InputElement));
+ Assert.All(inputRadioComponents, inputRadio => Assert.Null(Record.Exception(() => inputRadio.InputElement)));
}
private static RenderFragment RadioButtonsWithoutGroup(string name) => (builder) =>
diff --git a/src/Components/Web/test/Forms/InputSelectTest.cs b/src/Components/Web/test/Forms/InputSelectTest.cs
index 729d8ca7020c..167b8e434a15 100644
--- a/src/Components/Web/test/Forms/InputSelectTest.cs
+++ b/src/Components/Web/test/Forms/InputSelectTest.cs
@@ -207,9 +207,10 @@ public async Task InputElementIsAssignedSuccessfully()
// Act
var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+ var exception = Record.Exception(() => inputSelectComponent.SelectElement);
// Assert
- Assert.NotNull(inputSelectComponent.InputElement);
+ Assert.Null(exception);
}
enum TestEnum
diff --git a/src/Components/Web/test/Forms/InputTextAreaTest.cs b/src/Components/Web/test/Forms/InputTextAreaTest.cs
index 89f59ddc4c21..63b77bfcf799 100644
--- a/src/Components/Web/test/Forms/InputTextAreaTest.cs
+++ b/src/Components/Web/test/Forms/InputTextAreaTest.cs
@@ -21,9 +21,10 @@ public async Task InputElementIsAssignedSuccessfully()
// Act
var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+ var exception = Record.Exception(() => inputSelectComponent.InputElement);
// Assert
- Assert.NotNull(inputSelectComponent.InputElement);
+ Assert.Null(exception);
}
private class TestModel
diff --git a/src/Components/Web/test/Forms/InputTextTest.cs b/src/Components/Web/test/Forms/InputTextTest.cs
index 8a53b107a781..583dec039816 100644
--- a/src/Components/Web/test/Forms/InputTextTest.cs
+++ b/src/Components/Web/test/Forms/InputTextTest.cs
@@ -21,9 +21,10 @@ public async Task InputElementIsAssignedSuccessfully()
// Act
var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
+ var exception = Record.Exception(() => inputSelectComponent.InputElement);
// Assert
- Assert.NotNull(inputSelectComponent.InputElement);
+ Assert.Null(exception);
}
private class TestModel
From 6f1b8b390bbba02f92d7e0681f52e43d2e34d12e Mon Sep 17 00:00:00 2001
From: Juan Barahona
Date: Fri, 14 Aug 2020 19:11:57 -0400
Subject: [PATCH 7/9] Adding test to check focus is successfully applied
---
.../test/E2ETest/Tests/InputActionsTest.cs | 74 +++++++++++++++
.../FormsTest/InputActionsComponent.razor | 94 +++++++++++++++++++
.../test/testassets/BasicTestApp/Index.razor | 1 +
3 files changed, 169 insertions(+)
create mode 100644 src/Components/test/E2ETest/Tests/InputActionsTest.cs
create mode 100644 src/Components/test/testassets/BasicTestApp/FormsTest/InputActionsComponent.razor
diff --git a/src/Components/test/E2ETest/Tests/InputActionsTest.cs b/src/Components/test/E2ETest/Tests/InputActionsTest.cs
new file mode 100644
index 000000000000..808491ea52df
--- /dev/null
+++ b/src/Components/test/E2ETest/Tests/InputActionsTest.cs
@@ -0,0 +1,74 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text.Json;
+using System.Threading.Tasks;
+using BasicTestApp;
+using BasicTestApp.FormsTest;
+using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
+using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
+using Microsoft.AspNetCore.E2ETesting;
+using Microsoft.AspNetCore.Testing;
+using OpenQA.Selenium;
+using OpenQA.Selenium.Support.UI;
+using Xunit;
+using Xunit.Abstractions;
+
+namespace Microsoft.AspNetCore.Components.E2ETest.Tests
+{
+ public class InputActionsTest : ServerTestBase>
+ {
+ public InputActionsTest(
+ BrowserFixture browserFixture,
+ ToggleExecutionModeServerFixture serverFixture,
+ ITestOutputHelper output)
+ : base(browserFixture, serverFixture, output)
+ {
+ }
+
+ protected override void InitializeAsyncCore()
+ {
+ // On WebAssembly, page reloads are expensive so skip if possible
+ Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Client);
+ }
+
+ protected virtual IWebElement MountInputActionsComponent()
+ => Browser.MountTestComponent();
+
+ [Theory]
+ [InlineData("name")]
+ [InlineData("age")]
+ [InlineData("description")]
+ [InlineData("renewal-date")]
+ [InlineData("radio-group")]
+ [InlineData("accepts-terms")]
+ [InlineData("ticket-class")]
+ public void InputElementsGetFocusedSuccessfully(string className)
+ {
+ var appElement = MountInputActionsComponent();
+ var inputSection = appElement.FindElement(By.ClassName(className));
+ var buttonsToFocus = inputSection.FindElements(By.TagName("button"));
+ var inputsToFocus = inputSection.FindElements(By.TagName("input"));
+
+ if (inputsToFocus.Count == 0)
+ {
+ inputsToFocus = inputSection.FindElements(By.TagName("textarea"));
+ }
+
+ if (inputsToFocus.Count == 0)
+ {
+ inputsToFocus = inputSection.FindElements(By.TagName("select"));
+ }
+
+ for (int i = 0; i < buttonsToFocus.Count; i++)
+ {
+ buttonsToFocus[i].Click();
+ Browser.Equal(inputsToFocus[i], () => Browser.SwitchTo().ActiveElement());
+ }
+ }
+ }
+}
diff --git a/src/Components/test/testassets/BasicTestApp/FormsTest/InputActionsComponent.razor b/src/Components/test/testassets/BasicTestApp/FormsTest/InputActionsComponent.razor
new file mode 100644
index 000000000000..3a1b14013eb9
--- /dev/null
+++ b/src/Components/test/testassets/BasicTestApp/FormsTest/InputActionsComponent.razor
@@ -0,0 +1,94 @@
+@using Microsoft.AspNetCore.Components.Forms
+
+
+
+ Name:
+
+
+
+
+ Age (years):
+
+
+
+
+ Description:
+
+
+
+
+ Renewal date:
+
+
+
+
+ Ticket class:
+
+
+
+
+
+
+ @person.TicketClass
+
+
+
+
+
+ Pick one country:
+
+ japan
+
+
+ Latvia
+
+
+
+
+ Accepts terms:
+
+
+
+
+@code {
+ InputText inputTextReference;
+ InputNumber inputNumberReference;
+ InputTextArea inputTextAreaReference;
+ InputDate inputDateReference;
+ InputSelect inputSelectReference;
+ InputRadio inputRadio1Reference;
+ InputRadio inputRadio2Reference;
+ InputCheckbox inputCheckboxReference;
+
+ Person person = new Person();
+ EditContext editContext;
+
+ protected override void OnInitialized()
+ {
+ editContext = new EditContext(person);
+ }
+
+
+ // Usually this would be in a different file
+ class Person
+ {
+ public string Name { get; set; }
+
+ public int AgeInYears { get; set; }
+
+ public DateTime RenewalDate { get; set; } = DateTime.Now;
+
+ public bool AcceptsTerms { get; set; }
+
+ public string Description { get; set; }
+
+ public TicketClass TicketClass { get; set; }
+
+ public Country? Country { get; set; } = null;
+ }
+
+ enum TicketClass { Economy, Premium, First }
+
+ enum Country { Japan, Latvia }
+}
+
diff --git a/src/Components/test/testassets/BasicTestApp/Index.razor b/src/Components/test/testassets/BasicTestApp/Index.razor
index 674760191873..26abe10a6e2a 100644
--- a/src/Components/test/testassets/BasicTestApp/Index.razor
+++ b/src/Components/test/testassets/BasicTestApp/Index.razor
@@ -31,6 +31,7 @@
+
From 3b65057a9d7ea2ff5ecc36c8150152e138a0cf7d Mon Sep 17 00:00:00 2001
From: Juan Barahona
Date: Fri, 14 Aug 2020 19:13:28 -0400
Subject: [PATCH 8/9] Fixing the behavior where RadioGroup was being
re-rendered always
---
.../Web/src/Forms/InputRadioContext.cs | 33 ++++++++++++++++++-
1 file changed, 32 insertions(+), 1 deletion(-)
diff --git a/src/Components/Web/src/Forms/InputRadioContext.cs b/src/Components/Web/src/Forms/InputRadioContext.cs
index 45f302871bec..d2a51c416e76 100644
--- a/src/Components/Web/src/Forms/InputRadioContext.cs
+++ b/src/Components/Web/src/Forms/InputRadioContext.cs
@@ -1,12 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+using System;
+using System.Collections.Generic;
+
namespace Microsoft.AspNetCore.Components.Forms
{
///
/// Describes context for an component.
///
- internal class InputRadioContext
+ internal class InputRadioContext : IEquatable
{
private readonly InputRadioContext? _parentContext;
@@ -60,5 +63,33 @@ public InputRadioContext(
/// The , or null if none was found.
public InputRadioContext? FindContextInAncestors(string groupName)
=> string.Equals(GroupName, groupName) ? this : _parentContext?.FindContextInAncestors(groupName);
+
+ public override bool Equals(object? obj)
+ {
+ return Equals(obj as InputRadioContext);
+ }
+
+ public bool Equals(InputRadioContext? other)
+ {
+ return other != null &&
+ EqualityComparer.Default.Equals(_parentContext, other._parentContext) &&
+ GroupName == other.GroupName &&
+ FieldClass == other.FieldClass;
+ }
+
+ public override int GetHashCode()
+ {
+ return HashCode.Combine(_parentContext, GroupName, CurrentValue, FieldClass, ChangeEventCallback);
+ }
+
+ public static bool operator ==(InputRadioContext? left, InputRadioContext? right)
+ {
+ return EqualityComparer.Default.Equals(left, right);
+ }
+
+ public static bool operator !=(InputRadioContext? left, InputRadioContext? right)
+ {
+ return !(left == right);
+ }
}
}
From 6392ae39ce9efb714c65ead2536ddbcfd8e5744a Mon Sep 17 00:00:00 2001
From: Juan Barahona
Date: Wed, 19 Aug 2020 20:19:17 -0400
Subject: [PATCH 9/9] Removing InputRadio from the list
---
src/Components/Web/src/Forms/InputRadio.cs | 12 -------
.../Web/src/Forms/InputRadioContext.cs | 33 +------------------
.../Web/test/Forms/InputRadioTest.cs | 15 ---------
.../test/E2ETest/Tests/InputActionsTest.cs | 9 -----
.../FormsTest/InputActionsComponent.razor | 16 ---------
5 files changed, 1 insertion(+), 84 deletions(-)
diff --git a/src/Components/Web/src/Forms/InputRadio.cs b/src/Components/Web/src/Forms/InputRadio.cs
index ee0adc4828c6..4a4ad46dc3a5 100644
--- a/src/Components/Web/src/Forms/InputRadio.cs
+++ b/src/Components/Web/src/Forms/InputRadio.cs
@@ -14,8 +14,6 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputRadio : ComponentBase
{
- private ElementReference? _inputElement;
-
///
/// Gets context for this .
///
@@ -41,15 +39,6 @@ public class InputRadio : ComponentBase
[CascadingParameter] private InputRadioContext? CascadedContext { get; set; }
- ///
- /// Gets or sets the associated
- ///
- public ElementReference InputElement
- {
- get => _inputElement ?? throw new InvalidOperationException($"Component must be rendered before {nameof(InputElement)} can be accessed.");
- protected set => _inputElement = value;
- }
-
private string GetCssClass(string fieldClass)
{
if (AdditionalAttributes != null &&
@@ -87,7 +76,6 @@ 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 => InputElement = __inputReference);
builder.CloseElement();
}
}
diff --git a/src/Components/Web/src/Forms/InputRadioContext.cs b/src/Components/Web/src/Forms/InputRadioContext.cs
index d2a51c416e76..45f302871bec 100644
--- a/src/Components/Web/src/Forms/InputRadioContext.cs
+++ b/src/Components/Web/src/Forms/InputRadioContext.cs
@@ -1,15 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-using System;
-using System.Collections.Generic;
-
namespace Microsoft.AspNetCore.Components.Forms
{
///
/// Describes context for an component.
///
- internal class InputRadioContext : IEquatable
+ internal class InputRadioContext
{
private readonly InputRadioContext? _parentContext;
@@ -63,33 +60,5 @@ public InputRadioContext(
/// The , or null if none was found.
public InputRadioContext? FindContextInAncestors(string groupName)
=> string.Equals(GroupName, groupName) ? this : _parentContext?.FindContextInAncestors(groupName);
-
- public override bool Equals(object? obj)
- {
- return Equals(obj as InputRadioContext);
- }
-
- public bool Equals(InputRadioContext? other)
- {
- return other != null &&
- EqualityComparer.Default.Equals(_parentContext, other._parentContext) &&
- GroupName == other.GroupName &&
- FieldClass == other.FieldClass;
- }
-
- public override int GetHashCode()
- {
- return HashCode.Combine(_parentContext, GroupName, CurrentValue, FieldClass, ChangeEventCallback);
- }
-
- public static bool operator ==(InputRadioContext? left, InputRadioContext? right)
- {
- return EqualityComparer.Default.Equals(left, right);
- }
-
- public static bool operator !=(InputRadioContext? left, InputRadioContext? right)
- {
- return !(left == right);
- }
}
}
diff --git a/src/Components/Web/test/Forms/InputRadioTest.cs b/src/Components/Web/test/Forms/InputRadioTest.cs
index f45bbe5b36c2..1447a16316f3 100644
--- a/src/Components/Web/test/Forms/InputRadioTest.cs
+++ b/src/Components/Web/test/Forms/InputRadioTest.cs
@@ -60,21 +60,6 @@ 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.Null(Record.Exception(() => inputRadio.InputElement)));
- }
-
private static RenderFragment RadioButtonsWithoutGroup(string name) => (builder) =>
{
foreach (var selectedValue in (TestEnum[])Enum.GetValues(typeof(TestEnum)))
diff --git a/src/Components/test/E2ETest/Tests/InputActionsTest.cs b/src/Components/test/E2ETest/Tests/InputActionsTest.cs
index 808491ea52df..d425b9dc9cc4 100644
--- a/src/Components/test/E2ETest/Tests/InputActionsTest.cs
+++ b/src/Components/test/E2ETest/Tests/InputActionsTest.cs
@@ -1,20 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Runtime.InteropServices;
-using System.Text.Json;
-using System.Threading.Tasks;
using BasicTestApp;
using BasicTestApp.FormsTest;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.E2ETesting;
-using Microsoft.AspNetCore.Testing;
using OpenQA.Selenium;
-using OpenQA.Selenium.Support.UI;
using Xunit;
using Xunit.Abstractions;
@@ -44,7 +36,6 @@ protected virtual IWebElement MountInputActionsComponent()
[InlineData("age")]
[InlineData("description")]
[InlineData("renewal-date")]
- [InlineData("radio-group")]
[InlineData("accepts-terms")]
[InlineData("ticket-class")]
public void InputElementsGetFocusedSuccessfully(string className)
diff --git a/src/Components/test/testassets/BasicTestApp/FormsTest/InputActionsComponent.razor b/src/Components/test/testassets/BasicTestApp/FormsTest/InputActionsComponent.razor
index 3a1b14013eb9..626091bd46ea 100644
--- a/src/Components/test/testassets/BasicTestApp/FormsTest/InputActionsComponent.razor
+++ b/src/Components/test/testassets/BasicTestApp/FormsTest/InputActionsComponent.razor
@@ -34,16 +34,6 @@
-
- Pick one country:
-
- japan
-
-
- Latvia
-
-
-
Accepts terms:
@@ -56,8 +46,6 @@
InputTextArea inputTextAreaReference;
InputDate inputDateReference;
InputSelect inputSelectReference;
- InputRadio inputRadio1Reference;
- InputRadio inputRadio2Reference;
InputCheckbox inputCheckboxReference;
Person person = new Person();
@@ -83,12 +71,8 @@
public string Description { get; set; }
public TicketClass TicketClass { get; set; }
-
- public Country? Country { get; set; } = null;
}
enum TicketClass { Economy, Premium, First }
-
- enum Country { Japan, Latvia }
}