Skip to content

Conversation

@TheCrazyWolf
Copy link
Contributor

@TheCrazyWolf TheCrazyWolf commented Apr 5, 2024

Description

Fix: #7621
Fix: #5766
Using name of enum in filter of DataGrid by Display attribute, for example:

screen

public enum EducationFinance
{
    [Display(Name = "Бюджет")]
    Free = 0,
    [Display(Name = "Внебюджет")]
    Paid = 1
} 

if attribute non exists, will be returns default value of enum

Added

How Has This Been Tested?

Compiled and test in my project

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • The PR is submitted to the correct branch (dev).
  • My code follows the code style of this project.
  • I've added relevant tests.

@github-actions github-actions bot added enhancement Request for adding a new feature or enhancing existing functionality (not fixing a defect) PR: needs review labels Apr 5, 2024
@codecov
Copy link

codecov bot commented Apr 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.98%. Comparing base (28bc599) to head (6058f0e).
Report is 578 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #8581      +/-   ##
==========================================
+ Coverage   89.82%   90.98%   +1.15%     
==========================================
  Files         412      410       -2     
  Lines       11878    12546     +668     
  Branches     2364     2451      +87     
==========================================
+ Hits        10670    11415     +745     
+ Misses        681      578     -103     
- Partials      527      553      +26     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@danielchalmers
Copy link
Member

It should probably fallback so you don't need both attributes on the enum, like for example:

    /// <summary>
    /// Returns the best display name for the given type.
    /// </summary>
    /// <param name="shortName">Try to find the shorter version of the name</param>
    public static string GetDisplayName(this Type type, bool shortName = false) =>
        (shortName ? type?.GetCustomAttribute<DisplayAttribute>()?.ShortName : null) ??
            type?.GetCustomAttribute<DisplayAttribute>()?.Name ??
            type?.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ??
            type?.GetCustomAttribute<DescriptionAttribute>()?.Description ??
            type?.Name;

But Isn't there a source generator for this or something? @ScarletKuro

@ScarletKuro
Copy link
Member

ScarletKuro commented Apr 14, 2024

But Isn't there a source generator for this or something? @ScarletKuro

The source generator is only working with the DisplayNameAttribute attribute, it's not available for user code, and it won't help you if you pass an enum in the runtime which is the case here.

I do not understand why Adornment was annotated with DisplayAttribute when we talk about the Enums in DataGrid that are supplied in the model?
For unit testing the author should create own enum type and test it, and not pick any of the existing ones.
This PR also didn't fix the code quality errors.

I'm also not sure if this is fixing localization issues, what if you want multi-languages for the enum?
Theoretically it would require a method that first reads the value of the attribute, then checks via localizer that such key exist in the localization and returns a translation, if doesn't exist then fallback to the attribute value. Aka something similar what does the aspnetcore with Data Annotation attributes for the validation localization.

I will let @tjscience and maybe @mikes-gh (if he is interested) to review this instead.

@mikes-gh mikes-gh changed the title [DataGrid] Added using title of enum for filter in DataGrid #7621 DataGrid: Added using title of enum for filter in DataGrid #7621 Apr 22, 2024
@mikes-gh mikes-gh changed the title DataGrid: Added using title of enum for filter in DataGrid #7621 DataGrid: Added using title of enum for filter #7621 Apr 22, 2024
@ScarletKuro
Copy link
Member

ScarletKuro commented Jul 22, 2024

Hi.

I appreciate you keep this branch up to date. But as you can see your code is failing to build:

  1. EnumFormTraining.cs has a wrong encoding.
  2. You modified Adornment for some reason.

@ScarletKuro
Copy link
Member

@meenzen, wouldn't it be better to do something like injecting the InternalMudLocalizer into the DataGrid and then just do:

-<MudSelectItem T="Enum" Value="@((Enum)item)">@item</MudSelectItem>
+<MudSelectItem T="Enum" Value="@Localizer[item]</MudSelectItem>

It would be more universal. There is one problem that enumeration names can clash like this:

public enum Enum1
{
    Test1
}

public enum Enum2
{
    Test1
}

But maybe we could use a bit of reflection and do something like @Localizer[enumTypeName_enumMemberName], aka Enum1_Test1. This still creates a potential issue that the enums might be more or less the same but live in different namespaces and require different localization. Chances are low, though.

Is there a possibility to add support for DisplayAttribute in the localizer? What are your thoughts on that?

@EmilAlipiev

This comment was marked as off-topic.

@henon

This comment was marked as off-topic.

@EmilAlipiev

This comment was marked as off-topic.

@henon

This comment was marked as off-topic.

@ScarletKuro

This comment was marked as off-topic.

@ScarletKuro

This comment was marked as off-topic.

@ScarletKuro ScarletKuro self-assigned this Oct 13, 2024
@ScarletKuro ScarletKuro removed the request for review from mikes-gh October 13, 2024 22:36
@ScarletKuro
Copy link
Member

ScarletKuro commented Oct 13, 2024

@henon @meenzen I reworked it completely, again.

The main improvement is that it’s now part of InternalMudLocalizer.
In the previous code in DataGrid, you had to inject both InternalMudLocalizer and IEnumLocalizer which bothered me a lot.
Now it also uses the same concept, ILocalizationEnumInterceptor, as the original localizer, in case you want to override the logic for how the enum is localized.
Earlier, the enum localizer looked like an abomination, but now it actually fits into the localization ecosystem.
A lot better tests.
And you can use it in two ways:

[Display(Name = "Enum value is localized")]
ValueWithDisplay,

[Display(Name = LanguageResource.MudDataGrid_IsEmpty)]
ValueWithDisplayLanguageResource,

The first one is a hard-coded string, second if it finds the key in the LanguageResource then it will use that instead.

Copy link
Contributor

@meenzen meenzen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this now!

@meenzen
Copy link
Contributor

meenzen commented Oct 14, 2024

Should we add some simple docs?

@ScarletKuro
Copy link
Member

Should we add some simple docs?

we can do it after

@ScarletKuro ScarletKuro merged commit 8bd9e3f into MudBlazor:dev Oct 14, 2024
4 checks passed
LLauter pushed a commit to cannellamedia/MudBlazor that referenced this pull request Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Request for adding a new feature or enhancing existing functionality (not fixing a defect) localization Translations, locale formats, RTL layout, or pluralization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DataGrid] Allow providing custom text for column with enum type when filtering Add Converter parameter to Column

6 participants