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

DataGrid: Added using title of enum for filter #7621 #8581

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from

Conversation

TheCrazyWolf
Copy link

@TheCrazyWolf TheCrazyWolf commented Apr 5, 2024

Description

Fix: #7621 , #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 New feature or request PR: needs review labels Apr 5, 2024
Copy link

codecov bot commented Apr 6, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 89.77%. Comparing base (28bc599) to head (bf5c1fd).
Report is 2 commits behind head on dev.

❗ Current head bf5c1fd differs from pull request most recent head a34c2b2. Consider uploading reports for the commit a34c2b2 to get more accurate results

Files Patch % Lines
src/MudBlazor/Extensions/EnumExtensions.cs 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #8581      +/-   ##
==========================================
- Coverage   89.82%   89.77%   -0.06%     
==========================================
  Files         412      411       -1     
  Lines       11878    11828      -50     
  Branches     2364     2363       -1     
==========================================
- Hits        10670    10618      -52     
- Misses        681      682       +1     
- Partials      527      528       +1     

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

@danielchalmers
Copy link
Contributor

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request PR: needs review
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
3 participants