Add support for binding decimal? double? float? long? int? bool? (imported from Blazor PR 1658) (#4600)
This commit is contained in:
parent
1e404a5acf
commit
8fbb9fb0e6
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// 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;
|
||||
|
|
@ -85,6 +85,14 @@ namespace Microsoft.AspNetCore.Components
|
|||
return _ => setter((bool)((UIChangeEventArgs)_).Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
public static Action<UIEventArgs> SetValueHandler(Action<bool?> setter, bool? existingValue)
|
||||
{
|
||||
return _ => setter((bool?)((UIChangeEventArgs)_).Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
|
|
@ -93,6 +101,16 @@ namespace Microsoft.AspNetCore.Components
|
|||
return _ => setter(int.Parse((string)((UIChangeEventArgs)_).Value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
public static Action<UIEventArgs> SetValueHandler(Action<int?> setter, int? existingValue)
|
||||
{
|
||||
return _ => setter(int.TryParse((string)((UIChangeEventArgs)_).Value, out var tmpvalue)
|
||||
? tmpvalue
|
||||
: (int?)null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
|
|
@ -101,6 +119,16 @@ namespace Microsoft.AspNetCore.Components
|
|||
return _ => setter(long.Parse((string)((UIChangeEventArgs)_).Value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
public static Action<UIEventArgs> SetValueHandler(Action<long?> setter, long? existingValue)
|
||||
{
|
||||
return _ => setter(long.TryParse((string)((UIChangeEventArgs)_).Value, out var tmpvalue)
|
||||
? tmpvalue
|
||||
: (long?)null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
|
|
@ -109,6 +137,16 @@ namespace Microsoft.AspNetCore.Components
|
|||
return _ => setter(float.Parse((string)((UIChangeEventArgs)_).Value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
public static Action<UIEventArgs> SetValueHandler(Action<float?> setter, float? existingValue)
|
||||
{
|
||||
return _ => setter(float.TryParse((string)((UIChangeEventArgs)_).Value, out var tmpvalue)
|
||||
? tmpvalue
|
||||
: (float?)null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
|
|
@ -117,6 +155,16 @@ namespace Microsoft.AspNetCore.Components
|
|||
return _ => setter(double.Parse((string)((UIChangeEventArgs)_).Value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
public static Action<UIEventArgs> SetValueHandler(Action<double?> setter, double? existingValue)
|
||||
{
|
||||
return _ => setter(double.TryParse((string)((UIChangeEventArgs)_).Value, out var tmpvalue)
|
||||
? tmpvalue
|
||||
: (double?)null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
|
|
@ -125,6 +173,16 @@ namespace Microsoft.AspNetCore.Components
|
|||
return _ => setter(decimal.Parse((string)((UIChangeEventArgs)_).Value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
public static Action<UIEventArgs> SetValueHandler(Action<decimal?> setter, decimal? existingValue)
|
||||
{
|
||||
return _ => setter(decimal.TryParse((string)((UIChangeEventArgs)_).Value, out var tmpvalue)
|
||||
? tmpvalue
|
||||
: (decimal?)null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not intended to be used directly.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
|||
Navigate(ServerPathBase, noReload: !serverFixture.UsingAspNetHost);
|
||||
MountTestComponent<BindCasesComponent>();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextbox_InitiallyBlank()
|
||||
{
|
||||
|
|
@ -73,7 +73,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
|||
Assert.Equal(string.Empty, boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextArea_InitiallyBlank()
|
||||
{
|
||||
|
|
@ -103,6 +103,26 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
|||
WaitAssert.Equal("Changed value", () => boundValue.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindCheckbox_InitiallyNull()
|
||||
{
|
||||
var target = Browser.FindElement(By.Id("checkbox-initially-null"));
|
||||
var boundValue = Browser.FindElement(By.Id("checkbox-initially-null-value"));
|
||||
var invertButton = Browser.FindElement(By.Id("checkbox-initially-null-invert"));
|
||||
Assert.False(target.Selected);
|
||||
Assert.Equal(string.Empty, boundValue.Text);
|
||||
|
||||
// Modify target; verify value is updated
|
||||
target.Click();
|
||||
WaitAssert.True(() => target.Selected);
|
||||
WaitAssert.Equal("True", () => boundValue.Text);
|
||||
|
||||
// Modify data; verify checkbox is updated
|
||||
invertButton.Click();
|
||||
WaitAssert.False(() => target.Selected);
|
||||
WaitAssert.Equal("False", () => boundValue.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindCheckbox_InitiallyUnchecked()
|
||||
{
|
||||
|
|
@ -180,6 +200,35 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
|||
Assert.Equal("42", mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextboxNullableInt()
|
||||
{
|
||||
var target = Browser.FindElement(By.Id("textbox-nullable-int"));
|
||||
var boundValue = Browser.FindElement(By.Id("textbox-nullable-int-value"));
|
||||
var mirrorValue = Browser.FindElement(By.Id("textbox-nullable-int-mirror"));
|
||||
Assert.Equal(string.Empty, target.GetAttribute("value"));
|
||||
Assert.Equal(string.Empty, boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("-42\t");
|
||||
WaitAssert.Equal("-42", () => boundValue.Text);
|
||||
Assert.Equal("-42", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("42\t");
|
||||
WaitAssert.Equal("42", () => boundValue.Text);
|
||||
Assert.Equal("42", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("\t");
|
||||
WaitAssert.Equal(string.Empty, () => boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextboxLong()
|
||||
{
|
||||
|
|
@ -197,6 +246,35 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
|||
Assert.Equal("-3000000000", mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextboxNullableLong()
|
||||
{
|
||||
var target = Browser.FindElement(By.Id("textbox-nullable-long"));
|
||||
var boundValue = Browser.FindElement(By.Id("textbox-nullable-long-value"));
|
||||
var mirrorValue = Browser.FindElement(By.Id("textbox-nullable-long-mirror"));
|
||||
Assert.Equal(string.Empty, target.GetAttribute("value"));
|
||||
Assert.Equal(string.Empty, boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("3000000000\t");
|
||||
WaitAssert.Equal("3000000000", () => boundValue.Text);
|
||||
Assert.Equal("3000000000", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("-3000000000\t");
|
||||
WaitAssert.Equal("-3000000000", () => boundValue.Text);
|
||||
Assert.Equal("-3000000000", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("\t");
|
||||
WaitAssert.Equal(string.Empty, () => boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextboxFloat()
|
||||
{
|
||||
|
|
@ -214,6 +292,35 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
|||
Assert.Equal("-3.141", mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextboxNullableFloat()
|
||||
{
|
||||
var target = Browser.FindElement(By.Id("textbox-nullable-float"));
|
||||
var boundValue = Browser.FindElement(By.Id("textbox-nullable-float-value"));
|
||||
var mirrorValue = Browser.FindElement(By.Id("textbox-nullable-float-mirror"));
|
||||
Assert.Equal(string.Empty, target.GetAttribute("value"));
|
||||
Assert.Equal(string.Empty, boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("3.141\t");
|
||||
WaitAssert.Equal("3.141", () => boundValue.Text);
|
||||
Assert.Equal("3.141", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("-3.141\t");
|
||||
WaitAssert.Equal("-3.141", () => boundValue.Text);
|
||||
Assert.Equal("-3.141", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("\t");
|
||||
WaitAssert.Equal(string.Empty, () => boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextboxDouble()
|
||||
{
|
||||
|
|
@ -238,6 +345,42 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
|||
Assert.Equal("0.01", mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextboxNullableDouble()
|
||||
{
|
||||
var target = Browser.FindElement(By.Id("textbox-nullable-double"));
|
||||
var boundValue = Browser.FindElement(By.Id("textbox-nullable-double-value"));
|
||||
var mirrorValue = Browser.FindElement(By.Id("textbox-nullable-double-mirror"));
|
||||
Assert.Equal(string.Empty, target.GetAttribute("value"));
|
||||
Assert.Equal(string.Empty, boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("3.14159265359\t");
|
||||
WaitAssert.Equal("3.14159265359", () => boundValue.Text);
|
||||
Assert.Equal("3.14159265359", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("-3.14159265359\t");
|
||||
WaitAssert.Equal("-3.14159265359", () => boundValue.Text);
|
||||
Assert.Equal("-3.14159265359", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
// Double shouldn't preserve trailing zeros
|
||||
target.Clear();
|
||||
target.SendKeys("0.010\t");
|
||||
WaitAssert.Equal("0.01", () => boundValue.Text);
|
||||
Assert.Equal("0.01", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("\t");
|
||||
WaitAssert.Equal(string.Empty, () => boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextboxDecimal()
|
||||
{
|
||||
|
|
@ -255,5 +398,35 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
|||
WaitAssert.Equal("0.010", () => boundValue.Text);
|
||||
Assert.Equal("0.010", mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanBindTextboxNullableDecimal()
|
||||
{
|
||||
var target = Browser.FindElement(By.Id("textbox-nullable-decimal"));
|
||||
var boundValue = Browser.FindElement(By.Id("textbox-nullable-decimal-value"));
|
||||
var mirrorValue = Browser.FindElement(By.Id("textbox-nullable-decimal-mirror"));
|
||||
Assert.Equal(string.Empty, target.GetAttribute("value"));
|
||||
Assert.Equal(string.Empty, boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("0.0000000000000000000000000001\t");
|
||||
WaitAssert.Equal("0.0000000000000000000000000001", () => boundValue.Text);
|
||||
Assert.Equal("0.0000000000000000000000000001", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
// Decimal should preserve trailing zeros
|
||||
target.Clear();
|
||||
target.SendKeys("0.010\t");
|
||||
WaitAssert.Equal("0.010", () => boundValue.Text);
|
||||
Assert.Equal("0.010", mirrorValue.GetAttribute("value"));
|
||||
|
||||
// Modify target; verify value is updated and that textboxes linked to the same data are updated
|
||||
target.Clear();
|
||||
target.SendKeys("\t");
|
||||
WaitAssert.Equal(string.Empty, () => boundValue.Text);
|
||||
Assert.Equal(string.Empty, mirrorValue.GetAttribute("value"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,30 +21,60 @@
|
|||
<span id="textbox-int-value">@textboxIntValue</span>
|
||||
<input id="textbox-int-mirror" bind="textboxIntValue" readonly />
|
||||
</p>
|
||||
<p>
|
||||
Nullable int:
|
||||
<input id="textbox-nullable-int" bind="textboxNullableIntValue" type="number" />
|
||||
<span id="textbox-nullable-int-value">@textboxNullableIntValue</span>
|
||||
<input id="textbox-nullable-int-mirror" bind="textboxNullableIntValue" readonly />
|
||||
</p>
|
||||
<p>
|
||||
long:
|
||||
<input id="textbox-long" bind="textboxLongValue" type="number" />
|
||||
<span id="textbox-long-value">@textboxLongValue</span>
|
||||
<input id="textbox-long-mirror" bind="textboxLongValue" readonly />
|
||||
</p>
|
||||
<p>
|
||||
Nullable long:
|
||||
<input id="textbox-nullable-long" bind="textboxNullableLongValue" type="number" />
|
||||
<span id="textbox-nullable-long-value">@textboxNullableLongValue</span>
|
||||
<input id="textbox-nullable-long-mirror" bind="textboxNullableLongValue" readonly />
|
||||
</p>
|
||||
<p>
|
||||
float:
|
||||
<input id="textbox-float" bind="textboxFloatValue" type="number" />
|
||||
<span id="textbox-float-value">@textboxFloatValue</span>
|
||||
<input id="textbox-float-mirror" bind="textboxFloatValue" readonly />
|
||||
</p>
|
||||
<p>
|
||||
Nullable float:
|
||||
<input id="textbox-nullable-float" bind="textboxNullableFloatValue" type="number" />
|
||||
<span id="textbox-nullable-float-value">@textboxNullableFloatValue</span>
|
||||
<input id="textbox-nullable-float-mirror" bind="textboxNullableFloatValue" readonly />
|
||||
</p>
|
||||
<p>
|
||||
double:
|
||||
<input id="textbox-double" bind="textboxDoubleValue" type="number" />
|
||||
<span id="textbox-double-value">@textboxDoubleValue</span>
|
||||
<input id="textbox-double-mirror" bind="textboxDoubleValue" readonly />
|
||||
</p>
|
||||
<p>
|
||||
Nullable double:
|
||||
<input id="textbox-nullable-double" bind="textboxNullableDoubleValue" type="number" />
|
||||
<span id="textbox-nullable-double-value">@textboxNullableDoubleValue</span>
|
||||
<input id="textbox-nullable-double-mirror" bind="textboxNullableDoubleValue" readonly />
|
||||
</p>
|
||||
<p>
|
||||
decimal:
|
||||
<input id="textbox-decimal" bind="textboxDecimalValue" type="number" />
|
||||
<span id="textbox-decimal-value">@textboxDecimalValue</span>
|
||||
<input id="textbox-decimal-mirror" bind="textboxDecimalValue" readonly />
|
||||
</p>
|
||||
<p>
|
||||
Nullable decimal:
|
||||
<input id="textbox-nullable-decimal" bind="textboxNullableDecimalValue" type="number" />
|
||||
<span id="textbox-nullable-decimal-value">@textboxNullableDecimalValue</span>
|
||||
<input id="textbox-nullable-decimal-mirror" bind="textboxNullableDecimalValue" readonly />
|
||||
</p>
|
||||
|
||||
<h2>Text Area</h2>
|
||||
<p>
|
||||
|
|
@ -59,6 +89,12 @@
|
|||
</p>
|
||||
|
||||
<h2>Checkbox</h2>
|
||||
<p>
|
||||
Initially null:
|
||||
<input id="checkbox-initially-null" bind="checkboxInitiallyNullValue" type="checkbox" />
|
||||
<span id="checkbox-initially-null-value">@checkboxInitiallyNullValue</span>
|
||||
<button id="checkbox-initially-null-invert" onclick="@(() => { checkboxInitiallyNullValue = !checkboxInitiallyNullValue; })">Invert</button>
|
||||
</p>
|
||||
<p>
|
||||
Initially unchecked:
|
||||
<input id="checkbox-initially-unchecked" bind="checkboxInitiallyUncheckedValue" type="checkbox" />
|
||||
|
|
@ -80,7 +116,7 @@
|
|||
<option value=@SelectableValue.Third>Third choice</option>
|
||||
@if (includeFourthOption)
|
||||
{
|
||||
<option value=@SelectableValue.Fourth>Fourth choice</option>
|
||||
<option value=@SelectableValue.Fourth>Fourth choice</option>
|
||||
}
|
||||
</select>
|
||||
<span id="select-box-value">@selectValue</span>
|
||||
|
|
@ -94,14 +130,20 @@
|
|||
string textAreaInitiallyBlankValue = null;
|
||||
string textAreaInitiallyPopulatedValue = "Hello";
|
||||
|
||||
bool? checkboxInitiallyNullValue = null;
|
||||
bool checkboxInitiallyUncheckedValue = false;
|
||||
bool checkboxInitiallyCheckedValue = true;
|
||||
|
||||
int textboxIntValue = -42;
|
||||
int? textboxNullableIntValue = null;
|
||||
long textboxLongValue = 3_000_000_000;
|
||||
long? textboxNullableLongValue = null;
|
||||
float textboxFloatValue = 3.141f;
|
||||
float? textboxNullableFloatValue = null;
|
||||
double textboxDoubleValue = 3.14159265359d;
|
||||
double? textboxNullableDoubleValue = null;
|
||||
decimal textboxDecimalValue = 0.0000000000000000000000000001M;
|
||||
decimal? textboxNullableDecimalValue = null;
|
||||
|
||||
bool includeFourthOption = false;
|
||||
enum SelectableValue { First, Second, Third, Fourth }
|
||||
|
|
|
|||
Loading…
Reference in New Issue