Skip to content

Commit

Permalink
Bind to current-value instead of value.
Browse files Browse the repository at this point in the history
  • Loading branch information
atmgrifter00 committed Nov 13, 2024
1 parent 7a0ba9a commit dbf9e93
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@inherits NimbleInputBase<double?>
<nimble-number-field
appearance="@Appearance.ToAttributeValue()"
value="@Value"
current-value="@CurrentValue"
@onchange="@(EventCallback.Factory.CreateBinder<string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString))"
readonly="@ReadOnly"
disabled="@Disabled"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@page "/InteractiveServer/NumberFieldBindings"
@namespace NimbleBlazor.Tests.Acceptance.Pages
@using System.Diagnostics.CodeAnalysis;
@inherits LayoutComponentBase
@rendermode @(new InteractiveServerRenderMode(prerender: false))

<NimbleNumberField id="number1" Value="_value1" ValueChanged="Value1Changed"></NimbleNumberField>
<NimbleNumberField id="number2" Value="_value2" ValueChanged="Value2Changed"></NimbleNumberField>

@code {
private double _value1;
private double _value2;

private void Value1Changed(double? value)
{
double newValue = value / 2 ?? 0;
if (_value2 != newValue)
{
_value2 = newValue;
}
}

private void Value2Changed(double? value)
{
double newValue = value * 2 ?? 0;
if (_value1 != newValue)
{
_value1 = newValue;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using BlazorWorkspace.Testing.Acceptance;
using Microsoft.Playwright;
using Xunit;

namespace NimbleBlazor.Tests.Acceptance.InteractiveServer;

public class NumberFieldBindingTests : NimbleInteractiveAcceptanceTestsBase
{
public NumberFieldBindingTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture)
: base(playwrightFixture, blazorServerClassFixture)
{
}

[Fact]
public async Task NumberField_TwoWayBindingsDontBreakAsync()
{
await using (var pageWrapper = await NewPageForRouteAsync("InteractiveServer/NumberFieldBindings"))
{
var page = pageWrapper.Page;
var numberField1 = page.Locator("#number1");
var numberField1IncButton = numberField1.Locator("nimble-button", new LocatorLocatorOptions() { HasText = "Increment" });
var numberField2 = page.Locator("#number2");
var numberField2IncButton = numberField2.Locator("nimble-button", new LocatorLocatorOptions() { HasText = "Increment" });

await numberField1IncButton.ClickAsync();
await Assertions.Expect(numberField2).ToHaveAttributeAsync("current-value", "0.5");
await numberField2IncButton.ClickAsync();
await Assertions.Expect(numberField1).ToHaveAttributeAsync("current-value", "3"); // incrementing numberField2 to 1.5 should result in 3 for numberField1
}
}
}

0 comments on commit dbf9e93

Please sign in to comment.