fix(module: npuNumber): include parser in value evaluation (#1567)

This commit is contained in:
Andrzej Bakun 2021-05-24 17:51:44 +02:00 committed by GitHub
parent 8ab168bad7
commit bc782a23f0
2 changed files with 29 additions and 16 deletions

View File

@ -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;
}

View 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);
}
}