Megabit / Blazorise

Blazorise is a component library built on top of Blazor with support for CSS frameworks like Bootstrap, Tailwind, Bulma, AntDesign, and Material.
https://blazorise.com/
Other
3.18k stars 517 forks source link

Allow NumericEdit to work with any numeric value type #5539

Open PunzunLtd opened 1 month ago

PunzunLtd commented 1 month ago

I have a custom Money class that I wanted to use with the NumericEdit control. I wound up having to subclass the control because NumericEdit has a hard-coded list of types that it recognizes in the FormatValueAsString(TValue) method.

Our Money class has an implicit conversion to System.Decimal and implements most of the System.Numerics interfaces.

Proposed solution In Blazorise.Utilities.Converters, replace FormatValue(byte, CultureInfo) and similar methods that simply call value?.ToString with a single method:

public static string FormatValue( ISpanFormattable? value, CultureInfo? culture = null) => value?.ToString( culture ?? CultureInfo.CurrentCulture)

Also, add to TryChangeType<TValue> before the final else condition:

else if (typeof(TValue).GetInterfaces().Contains(typeof(IParsable<TValue>))) result = (TValue)Convert.ChangeType((result as IParsable<TValue>).Parse(value.ToString()), conversionType, cultureInfo ?? CultureInfo.InvariantCulture)

In Blazorise.NumericEdit:FormatValueString, add a switch condition at the end (so that concrete conversions are tried first):

ISpanFormattable formattable => Converters.FormatValue( formattable, CurrentCultureInfo ),

Additional context

Program to interfaces, not to implementations.

stsrki commented 1 month ago

We are focused mostly on the native and primitive types, as that is what most users use. We might implement extra logic to cover some extra types, like in your example. But at the moment, we are limited in resources with some more urgent features.

That being said. We are open to contributions. For this, you might be perfect as you are most knowledgeable on what needs to be covered.

PunzunLtd commented 1 month ago

OK. When I get a minute, I'll send in a PR. I see that you've got a release scheduled for July 15th, so I'll make sure to get it in before then.