Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow NumericEdit to work with any numeric value type #5539

Open
PunzunLtd opened this issue May 27, 2024 · 2 comments
Open

Allow NumericEdit to work with any numeric value type #5539

PunzunLtd opened this issue May 27, 2024 · 2 comments
Labels
Type: Feature ⚙ Request or idea for a new feature.

Comments

@PunzunLtd
Copy link

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.

@PunzunLtd PunzunLtd added the Type: Feature ⚙ Request or idea for a new feature. label May 27, 2024
@stsrki
Copy link
Collaborator

stsrki commented May 27, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature ⚙ Request or idea for a new feature.
Projects
None yet
Development

No branches or pull requests

2 participants