-
Notifications
You must be signed in to change notification settings - Fork 10.4k
InputRadio component with form support #23415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d82e703
Started on InputRadio forms component.
MackinnonBuck f9058cf
Added E2E test for InputRadio.
MackinnonBuck 20adb6a
Added docstring for InputRadio.
MackinnonBuck 0a46728
Changed value to be serialized using BindConverter.
MackinnonBuck dccec3d
Added InputChoice for choice-based inputs.
MackinnonBuck 05f4547
Added InputRadioGroup.
MackinnonBuck 3cbdd1a
Small fix.
MackinnonBuck d6e0bc8
Removed InputChoice, cleaned up.
MackinnonBuck f278c64
Added internal access modifier to InputExtensions.
MackinnonBuck 32d0b72
Small improvements.
MackinnonBuck 2dc1d3a
Updated an outdated exception message.
MackinnonBuck 4e93647
Updated test to reflect updated exception message.
MackinnonBuck 8b15f9c
Improved API to enforce InputRadioGroup.
MackinnonBuck 3a17be6
Added support for InputSelect int and Guid bindings.
MackinnonBuck ab90f69
Changed validation CSS classes to influence InputRadio components.
MackinnonBuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 System.Globalization; | ||
|
||
namespace Microsoft.AspNetCore.Components.Forms | ||
{ | ||
internal static class InputExtensions | ||
{ | ||
public static bool TryParseSelectableValueFromString<TValue>(this InputBase<TValue> input, string? value, [MaybeNull] out TValue result, [NotNullWhen(false)] out string? validationErrorMessage) | ||
{ | ||
if (typeof(TValue) == typeof(string)) | ||
{ | ||
result = (TValue)(object?)value; | ||
validationErrorMessage = null; | ||
return true; | ||
} | ||
else if (typeof(TValue).IsEnum || (Nullable.GetUnderlyingType(typeof(TValue))?.IsEnum ?? false)) | ||
{ | ||
var success = BindConverter.TryConvertTo<TValue>(value, CultureInfo.CurrentCulture, out var parsedValue); | ||
if (success) | ||
{ | ||
result = parsedValue; | ||
validationErrorMessage = null; | ||
return true; | ||
} | ||
else | ||
{ | ||
result = default; | ||
validationErrorMessage = $"The {input.FieldIdentifier.FieldName} field is not valid."; | ||
return false; | ||
} | ||
} | ||
|
||
throw new InvalidOperationException($"{input.GetType()} does not support the type '{typeof(TValue)}'."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 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; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
|
||
namespace Microsoft.AspNetCore.Components.Forms | ||
{ | ||
/// <summary> | ||
/// An input component used for selecting a value from a group of choices. | ||
/// </summary> | ||
public class InputRadio<TValue> : InputBase<TValue> | ||
{ | ||
/// <summary> | ||
/// Gets the name of this <see cref="InputRadio{TValue}"/> group. | ||
/// </summary> | ||
protected string? GroupName { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the value that will be bound when this radio input is selected. | ||
/// </summary> | ||
[AllowNull] | ||
[MaybeNull] | ||
[Parameter] | ||
public TValue SelectedValue { get; set; } = default; | ||
|
||
/// <summary> | ||
/// Gets or sets group name inherited from an ancestor <see cref="InputRadioGroup"/>. | ||
/// </summary> | ||
[CascadingParameter] InputRadioGroup? CascadedRadioGroup { get; set; } | ||
|
||
/// <inheritdoc /> | ||
protected override void OnParametersSet() | ||
{ | ||
GroupName = AdditionalAttributes != null && AdditionalAttributes.TryGetValue("name", out var nameAttribute) ? | ||
MackinnonBuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nameAttribute as string : | ||
CascadedRadioGroup?.GroupName; | ||
|
||
if (string.IsNullOrEmpty(GroupName)) | ||
{ | ||
throw new InvalidOperationException($"{GetType()} requires either an explicit string attribute 'name' or " + | ||
$"an ancestor {nameof(InputRadioGroup)}."); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
Debug.Assert(GroupName != null); | ||
|
||
builder.OpenElement(0, "input"); | ||
builder.AddMultipleAttributes(1, AdditionalAttributes); | ||
builder.AddAttribute(2, "type", "radio"); | ||
builder.AddAttribute(3, "class", CssClass); | ||
builder.AddAttribute(4, "name", GroupName); | ||
builder.AddAttribute(5, "value", BindConverter.FormatValue(FormatValueAsString(SelectedValue))); | ||
builder.AddAttribute(6, "checked", SelectedValue?.Equals(CurrentValue)); | ||
builder.AddAttribute(7, "onchange", EventCallback.Factory.CreateBinder<string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); | ||
builder.CloseElement(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override bool TryParseValueFromString(string? value, [MaybeNull] out TValue result, [NotNullWhen(false)] out string? validationErrorMessage) | ||
=> this.TryParseSelectableValueFromString(value, out result, out validationErrorMessage); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
|
||
namespace Microsoft.AspNetCore.Components.Forms | ||
{ | ||
/// <summary> | ||
/// Groups child <see cref="InputRadio{TValue}"/> components. | ||
/// </summary> | ||
public class InputRadioGroup : ComponentBase | ||
{ | ||
private readonly string _defaultGroupName = Guid.NewGuid().ToString("N"); | ||
|
||
internal string? GroupName { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the child content to be rendering inside the <see cref="InputRadioGroup"/>. | ||
/// </summary> | ||
[Parameter] public RenderFragment? ChildContent { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the group. | ||
/// </summary> | ||
[Parameter] public string? Name { get; set; } | ||
|
||
/// <inheritdoc /> | ||
protected override void OnParametersSet() | ||
{ | ||
GroupName = !string.IsNullOrEmpty(Name) ? Name : _defaultGroupName; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
Debug.Assert(GroupName != null); | ||
|
||
builder.OpenComponent<CascadingValue<InputRadioGroup>>(0); | ||
builder.AddAttribute(1, "IsFixed", true); | ||
MackinnonBuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
builder.AddAttribute(2, "Value", this); | ||
builder.AddAttribute(3, "ChildContent", ChildContent); | ||
builder.CloseComponent(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.