mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-05 05:27:37 +08:00
fix(module: npuNumber): include parser in value evaluation (#1567)
This commit is contained in:
parent
8ab168bad7
commit
bc782a23f0
@ -240,7 +240,8 @@ namespace AntDesign
|
||||
protected override bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage)
|
||||
{
|
||||
validationErrorMessage = null;
|
||||
if (!Regex.IsMatch(value, @"^[+-]?\d*[.,]?\d*$"))
|
||||
|
||||
if (Parser is null && !Regex.IsMatch(value, @"^[+-]?\d*[.,]?\d*$"))
|
||||
{
|
||||
result = Value;
|
||||
return true;
|
||||
@ -250,27 +251,16 @@ namespace AntDesign
|
||||
{
|
||||
value = "0";
|
||||
}
|
||||
if (!_isNullable)
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
result = default;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = _parseFunc(value, Value);
|
||||
}
|
||||
result = default;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
result = default;
|
||||
}
|
||||
if (Parser is not null)
|
||||
result = _parseFunc(Parser(value), Value);
|
||||
else
|
||||
{
|
||||
result = _parseFunc(value, Value);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
23
tests/AntDesign.Tests/InputNumber/InputNumberTests.razor
Normal file
23
tests/AntDesign.Tests/InputNumber/InputNumberTests.razor
Normal file
@ -0,0 +1,23 @@
|
||||
@using System.Text.RegularExpressions
|
||||
@inherits AntDesignTestBase
|
||||
@code {
|
||||
|
||||
[Fact]
|
||||
public async Task InputNumber_formatter_parser_change_value()
|
||||
{
|
||||
//Arrange
|
||||
JSInterop.SetupVoid(JSInteropConstants.Focus, _ => true);
|
||||
AntDesign.InputNumber<double> input = null;
|
||||
Func<double, string> formatter = v => "$ " + v.ToString("n0");
|
||||
Func<string, string> parser = v => Regex.Replace(v, @"\$\s?|(,*)", "");
|
||||
|
||||
var cut = Render<AntDesign.InputNumber<double>>(
|
||||
@<AntDesign.InputNumber @ref="@input" Formatter="formatter" Parser="parser" DefaultValue="1000d"/>
|
||||
);
|
||||
//Act
|
||||
cut.Find("input").Change("$ 10");
|
||||
//Assert
|
||||
cut.Instance.Value.Should().Be(10);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user