bitplatform icon indicating copy to clipboard operation
bitplatform copied to clipboard

`BitNumberField` not working with two way binding for floating point types

Open Greexter opened this issue 8 months ago • 4 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Describe the bug

Using double? for two way binding in BitNumberField results in exception.

Expected Behavior

I did not find anywhere in documentation that nullable doubles should not work, so I am assuming it is a bug, since int? works and is also shown in documentation.

Steps To Reproduce

@page "/bug"

<BitNumberField @bind-Value=_nullableDouble />

@code {
    double? _nullableDouble = 1;
}

this example will throw on load

Exceptions (if any)

System.InvalidCastException: Invalid cast from 'System.Double' to 'System.Nullable`1[[System.Double, System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at Bit.BlazorUI.BitNumberField`1.Normalize(TValue value) in C:\Users\Jakub\source\repos\bitplatform\src\BlazorUI\Bit.BlazorUI\Components\Inputs\NumberField\BitNumberField.razor.cs:line 688
   at Bit.BlazorUI.BitNumberField`1.NormalizeValue() in C:\Users\Jakub\source\repos\bitplatform\src\BlazorUI\Bit.BlazorUI\Components\Inputs\NumberField\BitNumberField.razor.cs:line 729
   at Bit.BlazorUI.BitNumberField`1.OnInitializedAsync() in C:\Users\Jakub\source\repos\bitplatform\src\BlazorUI\Bit.BlazorUI\Components\Inputs\NumberField\BitNumberField.razor.cs:line 318
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Bit.BlazorUI.BitNumberField`1.SetParametersAsync(ParameterView parameters) in C:\Users\Jakub\source\repos\bitplatform\src\BlazorUI\Bit.BlazorUI\obj\Debug\net10.0\Bit.BlazorUI.SourceGenerators\Bit.BlazorUI.SourceGenerators.Component.ComponentSourceGenerator\BitNumberField_SetParametersAsync.AutoGenerated.cs:line 295

.NET Version

10.0.201

Anything else?

It looks like the issue is in calling Convert.ChangeType(...) in https://github.com/bitfoundation/bitplatform/blob/ff7da724f69b22de64243f632f113bb280a07182/src/BlazorUI/Bit.BlazorUI/Components/Inputs/NumberField/BitNumberField.razor.cs#L771 as the typeof(TValue) will return the nullable type instead of the underlying type. Using _typeOfValue looks like it fixes the issue.

    private TValue Normalize(TValue value)
    {
        if (value is double doubleValue)
        {
            return (TValue)Convert.ChangeType(Math.Round(doubleValue, _precision), _typeOfValue);
        }
        else if (value is float floatValue)
        {
            return (TValue)Convert.ChangeType(Math.Round(floatValue, _precision), _typeOfValue);
        }
        else if (value is decimal decimalValue)
        {
            return (TValue)Convert.ChangeType(Math.Round(decimalValue, _precision), _typeOfValue);
        }

        return value;
    }

Greexter avatar Mar 29 '26 15:03 Greexter

Thanks for contacting us. We're investigating this issue. We'll let you know the result of our investigation soon.

msynk avatar Mar 30 '26 04:03 msynk

The issue is accepted and planned. Resolving it will start ASAP.

msynk avatar Apr 03 '26 14:04 msynk

Resolving this issue has started. It will be announced when this issue is resolved.

msynk avatar Apr 03 '26 14:04 msynk

This issue has been resolved, and it will be available in the next pre-release version. It will be announced when the pre-release becomes ready.

msynk avatar Apr 06 '26 05:04 msynk