Skip to content

Enable input number component to support type='range' attribute #55583

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
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ca712c1
Commit modifies the InputNumber component to allow the 'type' attribu…
MattyLeslie May 7, 2024
f9ceb16
Adding a test to the inputNumberTests to ensure the user defined type…
MattyLeslie May 7, 2024
525b346
Merge branch 'dotnet:main' into Enable-InputNumber-Component-to-Suppo…
MattyLeslie May 7, 2024
bfa7733
Using the TestRederer to extract attributes and assert the user-defin…
MattyLeslie May 7, 2024
ce3d8c0
Merge branch 'Enable-InputNumber-Component-to-Support-type='range'-At…
MattyLeslie May 7, 2024
8df498d
Retrieving frames with new methodology
MattyLeslie May 8, 2024
c7a76af
Isolating the correct element to extract attributes from
MattyLeslie May 9, 2024
ac09491
Only asserting the type attribute = range
MattyLeslie May 9, 2024
dd6b779
Isolating input element, atrributes and type attribute correctly
MattyLeslie May 14, 2024
7b2ee01
Improving methodology of handling render tree frames and isolating th…
MattyLeslie May 15, 2024
ebf82de
Ammendments to tests
MattyLeslie May 20, 2024
af90057
Merge branch 'dotnet:main' into Enable-InputNumber-Component-to-Suppo…
MattyLeslie May 28, 2024
d1f916e
Merge branch 'dotnet:main' into Enable-InputNumber-Component-to-Suppo…
MattyLeslie May 30, 2024
b1ab8e0
Update src/Components/Web/test/Forms/InputNumberTest.cs
MattyLeslie Jul 26, 2024
bf70742
Update src/Components/Web/test/Forms/InputNumberTest.cs
MattyLeslie Jul 26, 2024
abfb3e6
Update src/Components/Web/test/Forms/InputNumberTest.cs
MattyLeslie Jul 26, 2024
11aa774
re-adding missing tests
MattyLeslie Jul 26, 2024
6e7ca94
Using helper methods in tests.
MattyLeslie Jul 26, 2024
c717209
Update src/Components/Web/test/Forms/InputNumberTest.cs
MackinnonBuck Jul 26, 2024
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
4 changes: 2 additions & 2 deletions src/Components/Web/src/Forms/InputNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "input");
builder.AddAttribute(1, "step", _stepAttributeValue);
builder.AddMultipleAttributes(2, AdditionalAttributes);
builder.AddAttribute(3, "type", "number");
builder.AddAttribute(2, "type", "number");
builder.AddMultipleAttributes(3, AdditionalAttributes);
builder.AddAttributeIfNotNullOrEmpty(4, "name", NameAttributeValue);
builder.AddAttributeIfNotNullOrEmpty(5, "class", CssClass);
builder.AddAttribute(6, "value", CurrentValueAsString);
Expand Down
52 changes: 52 additions & 0 deletions src/Components/Web/test/Forms/InputNumberTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Components.Forms.Mapping;
using Microsoft.AspNetCore.Components.Infrastructure;
using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.AspNetCore.Components.Test.Helpers;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Components.Forms;

public class InputNumberTest
{
private readonly TestRenderer _testRenderer;

public InputNumberTest()
{
var services = new ServiceCollection();
services.AddLogging();
_testRenderer = new TestRenderer(services.BuildServiceProvider());
}

[Fact]
public async Task ValidationErrorUsesDisplayAttributeName()
{
Expand Down Expand Up @@ -49,6 +64,43 @@ public async Task InputElementIsAssignedSuccessfully()
Assert.NotNull(inputSelectComponent.Element);
}

[Fact]
public async Task UserDefinedTypeAttributeOverridesDefault()
{
// Arrange
var model = new TestModel();
var hostComponent = new TestInputHostComponent<int, TestInputNumberComponent>
{
EditContext = new EditContext(model),
ValueExpression = () => model.SomeNumber,
AdditionalAttributes = new Dictionary<string, object>
{
{ "type", "range" } // User-defined 'type' attribute to override default
}
};

// Act
var componentId = await RenderAndGetTestInputNumberComponentIdAsync(hostComponent);

// Retrieve the render tree frames and extract attributes using helper methods
var frames = _testRenderer.GetCurrentRenderTreeFrames(componentId);

var typeAttributeFrame = frames.Array.Single(frame =>
frame.FrameType == RenderTreeFrameType.Attribute &&
frame.AttributeName == "type");

// Assert
Assert.Equal("range", typeAttributeFrame.AttributeValue);
}

private async Task<int> RenderAndGetTestInputNumberComponentIdAsync(TestInputHostComponent<int, TestInputNumberComponent> hostComponent)
{
var hostComponentId = _testRenderer.AssignRootComponentId(hostComponent);
await _testRenderer.RenderRootComponentAsync(hostComponentId);
var batch = _testRenderer.Batches.Single();
return batch.GetComponentFrames<TestInputNumberComponent>().Single().ComponentId;
}

private class TestModel
{
public int SomeNumber { get; set; }
Expand Down
Loading