-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add an InputHidden component for Blazor #62626
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
dariatiurina
merged 3 commits into
dotnet:main
from
dariatiurina:55720-add-input-hidden
Jul 10, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
|
||
namespace Microsoft.AspNetCore.Components.Forms; | ||
|
||
/// <summary> | ||
/// An hidden input component for storing <see cref="string"/> values. | ||
/// </summary> | ||
public class InputHidden : InputBase<string?> | ||
{ | ||
/// <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; } | ||
|
||
/// <inheritdoc /> | ||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
builder.OpenElement(0, "input"); | ||
builder.AddAttribute(1, "type", "hidden"); | ||
builder.AddMultipleAttributes(2, AdditionalAttributes); | ||
builder.AddAttributeIfNotNullOrEmpty(3, "name", NameAttributeValue); | ||
builder.AddAttributeIfNotNullOrEmpty(4, "class", CssClass); | ||
builder.AddAttribute(5, "value", CurrentValueAsString); | ||
builder.AddAttribute(6, "onchange", EventCallback.Factory.CreateBinder<string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); | ||
builder.SetUpdatesAttributeName("value"); | ||
builder.AddElementReferenceCapture(7, __inputReference => Element = __inputReference); | ||
builder.CloseElement(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override bool TryParseValueFromString(string? value, out string? result, [NotNullWhen(false)] out string? validationErrorMessage) | ||
{ | ||
result = value; | ||
validationErrorMessage = null; | ||
return true; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Components.Forms.InputHidden | ||
Microsoft.AspNetCore.Components.Forms.InputHidden.Element.get -> Microsoft.AspNetCore.Components.ElementReference? | ||
Microsoft.AspNetCore.Components.Forms.InputHidden.Element.set -> void | ||
Microsoft.AspNetCore.Components.Forms.InputHidden.InputHidden() -> void | ||
Microsoft.AspNetCore.Components.Web.Internal.IInternalWebJSInProcessRuntime.InvokeJS(in Microsoft.JSInterop.Infrastructure.JSInvocationInfo invocationInfo) -> string! | ||
override Microsoft.AspNetCore.Components.Forms.InputHidden.BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder! builder) -> void | ||
override Microsoft.AspNetCore.Components.Forms.InputHidden.TryParseValueFromString(string? value, out string? result, out string? validationErrorMessage) -> bool | ||
virtual Microsoft.AspNetCore.Components.Routing.NavLink.ShouldMatch(string! uriAbsolute) -> bool |
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,30 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Components.Forms; | ||
|
||
public class InputHiddenTest | ||
{ | ||
[Fact] | ||
public async Task InputElementIsAssignedSuccessfully() | ||
{ | ||
// Arrange | ||
var model = new TestModel(); | ||
var rootComponent = new TestInputHostComponent<string, InputHidden> | ||
{ | ||
EditContext = new EditContext(model), | ||
ValueExpression = () => model.StringProperty, | ||
}; | ||
|
||
// Act | ||
var inputHiddenComponent = await InputRenderer.RenderAndGetComponent(rootComponent); | ||
|
||
// Assert | ||
Assert.NotNull(inputHiddenComponent.Element); | ||
} | ||
|
||
private class TestModel | ||
{ | ||
public string StringProperty { get; set; } | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
...testassets/Components.TestServer/RazorComponents/Pages/Forms/DefaultFormInputHidden.razor
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,21 @@ | ||
@page "/forms/default-form-input-hidden" | ||
@page "/reexecution/forms/default-form-input-hidden" | ||
@using Microsoft.AspNetCore.Components.Forms | ||
|
||
<h2>Default form Input Hidden</h2> | ||
|
||
<EditForm Enhance Model="Parameter" method="POST" OnValidSubmit="() => _submitted = true" FormName="someform"> | ||
<InputHidden id="hidden" @bind-Value="Parameter" /> | ||
<input id="send" type="submit" value="Send" /> | ||
</EditForm> | ||
|
||
@if (_submitted) | ||
{ | ||
<p id="pass">Hello @Parameter!</p> | ||
} | ||
|
||
@code { | ||
bool _submitted = false; | ||
|
||
[SupplyParameterFromForm] public string Parameter { get; set; } = "stranger"; | ||
} |
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.