Skip to content
Open
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
22 changes: 13 additions & 9 deletions src/Components/Web/src/Forms/InputBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,7 @@ protected string NameAttributeValue

if (_shouldGenerateFieldNames)
{
if (_formattedValueExpression is null && ValueExpression is not null)
{
_formattedValueExpression = FieldPrefix != null ? FieldPrefix.GetFieldName(ValueExpression) :
ExpressionFormatter.FormatLambda(ValueExpression);
}

return _formattedValueExpression ?? string.Empty;
return GetFieldName();
}

return string.Empty;
Expand All @@ -225,7 +219,7 @@ protected string NameAttributeValue
/// </summary>
/// <remarks>
/// If an explicit "id" is provided via <see cref="AdditionalAttributes"/>, that value takes precedence.
/// Otherwise, the id is derived from <see cref="NameAttributeValue"/> with invalid characters sanitized.
/// Otherwise, the id is a sanitized version of <see cref="NameAttributeValue"/> in SSR mode; generated independently in interactive mode.
/// </remarks>
protected string IdAttributeValue
{
Expand All @@ -236,10 +230,20 @@ protected string IdAttributeValue
return Convert.ToString(idAttributeValue, CultureInfo.InvariantCulture) ?? string.Empty;
}

return FieldIdGenerator.SanitizeHtmlId(NameAttributeValue);
var fieldName = NameAttributeValue;
if (string.IsNullOrEmpty(fieldName))
{
fieldName = GetFieldName();
}

return FieldIdGenerator.SanitizeHtmlId(fieldName);
}
}

private string GetFieldName()
=> _formattedValueExpression ??= FieldPrefix?.GetFieldName(ValueExpression!)
?? ExpressionFormatter.FormatLambda(ValueExpression!);

/// <inheritdoc />
public override Task SetParametersAsync(ParameterView parameters)
{
Expand Down
20 changes: 20 additions & 0 deletions src/Components/Web/test/Forms/InputTextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ public async Task ExplicitIdOverridesGenerated()
Assert.Equal("custom-id", idAttribute.AttributeValue);
}

[Fact]
public async Task RendersIdAttribute_WhenShouldUseFieldIdentifiersIsFalse_InteractiveMode()
{
// simulate interactive mode where ShouldUseFieldIdentifiers is false
var model = new TestModel();
var editContext = new EditContext(model) { ShouldUseFieldIdentifiers = false };
var rootComponent = new TestInputHostComponent<string, InputText>
{
EditContext = editContext,
ValueExpression = () => model.StringProperty,
};

var componentId = await RenderAndGetInputTextComponentIdAsync(rootComponent);
var frames = _testRenderer.GetCurrentRenderTreeFrames(componentId);

// id should still be generated for Label/Input association to work in interactive mode
var idAttribute = frames.Array.Single(f => f.FrameType == RenderTreeFrameType.Attribute && f.AttributeName == "id");
Assert.Equal("model_StringProperty", idAttribute.AttributeValue);
}

private async Task<int> RenderAndGetInputTextComponentIdAsync(TestInputHostComponent<string, InputText> hostComponent)
{
var hostComponentId = _testRenderer.AssignRootComponentId(hostComponent);
Expand Down
Loading