MudDataGrid: Add parameters for filter icons#11864
MudDataGrid: Add parameters for filter icons#11864danielchalmers merged 7 commits intoMudBlazor:devfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces new parameters to customize the sort and filter icons in the MudDataGrid, which is a great enhancement for user experience. The implementation is clean and directly addresses the feature request. I have two suggestions: one is a critical style guide adherence issue regarding default parameter initialization, and the other is a minor code simplification to remove a redundant if/else block. Overall, a solid contribution.
| /// <summary> | ||
| /// The color of the sort icon. | ||
| /// </summary> | ||
| [Parameter] | ||
| public Color SortIconColor { get; set; } = Color.Default; | ||
|
|
||
| /// <summary> | ||
| /// The empty filter icon shown when <see cref="Filterable"/> is <c>true</c>. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string FilterIconEmpty { get; set; } = Icons.Material.Outlined.FilterAlt; | ||
|
|
||
| /// <summary> | ||
| /// The filled filter icon shown when <see cref="Filterable"/> is <c>true</c>. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string FilterIconFilled { get; set; } = Icons.Material.Filled.FilterAlt; | ||
|
|
||
| /// <summary> | ||
| /// The clear filter icon shown when <see cref="Filterable"/> is <c>true</c>. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string FilterIconClear { get; set; } = Icons.Material.Filled.FilterAltOff; | ||
|
|
||
| /// <summary> | ||
| /// The color of the empty filter icon. | ||
| /// </summary> | ||
| [Parameter] | ||
| public Color FilterIconEmptyColor { get; set; } = Color.Default; | ||
|
|
||
| /// <summary> | ||
| /// The color of the filled filter icon. | ||
| /// </summary> | ||
| [Parameter] | ||
| public Color FilterIconFilledColor { get; set; } = Color.Default; | ||
|
|
||
| /// <summary> | ||
| /// The color of the clear filter icon. | ||
| /// </summary> | ||
| [Parameter] | ||
| public Color FilterIconClearColor { get; set; } = Color.Default; |
There was a problem hiding this comment.
The new Color parameters (SortIconColor, FilterIconEmptyColor, FilterIconFilledColor, FilterIconClearColor) are initialized to Color.Default. According to the style guide, members should not be initialized to their default values.1 Since Color.Default is the default value for the Color enum, these initializations are redundant and should be removed. Please remove the = Color.Default from these property declarations.
Style Guide References
Footnotes
| if (SortDirection == SortDirection.None) | ||
| { | ||
| <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton> | ||
| <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" Color="@Column.SortIconColor" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton> | ||
| } | ||
| else | ||
| { | ||
| <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton> | ||
| <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" Color="@Column.SortIconColor" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton> | ||
| } |
There was a problem hiding this comment.
This if/else block is redundant because both branches render the exact same MudIconButton component with identical parameters. This can be simplified to a single component declaration, which improves readability and maintainability.
<MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" Color="@Column.SortIconColor" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton>
|
This would be very useful for us as well. Our use case: we use many datagrids with filters, and we persist filter settings in cookies/local storage so users don’t need to reapply them when navigating between pages. Having the ability to customize the |
There was a problem hiding this comment.
Pull Request Overview
Adds per-column parameters to customize sort and filter icons and their colors in MudDataGrid to improve UX feedback when filtering/sorting.
- Introduces new Column parameters for icon names and colors (sort, filter-empty, filter-filled, filter-clear).
- Wires these parameters into MudDataGrid, HeaderCell, and FilterHeaderCell components.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/MudBlazor/Components/DataGrid/MudDataGrid.razor | Uses column/context-provided Sort/Filter icon colors and icons instead of hardcoded values. |
| src/MudBlazor/Components/DataGrid/HeaderCell.razor | Applies new Column icon/color parameters to sort and filter buttons. |
| src/MudBlazor/Components/DataGrid/FilterHeaderCell.razor | Applies new Column icon/color parameters to filter menu and clear button. |
| src/MudBlazor/Components/DataGrid/Column.razor.cs | Adds new [Parameter]s for icons and colors, with defaults for icon names. |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if (SortDirection == SortDirection.None) | ||
| { | ||
| <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton> | ||
| <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" Color="@Column.SortIconColor" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton> | ||
| } | ||
| else | ||
| { | ||
| <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton> | ||
| <MudIconButton Icon="@Column.SortIcon" Class="@sortIconClass" Size="@Size.Small" Color="@Column.SortIconColor" OnClick="@SortChangedAsync" aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]"></MudIconButton> | ||
| } |
There was a problem hiding this comment.
Both branches of the SortDirection check render identical content, making the conditional redundant. Remove the if/else and keep a single MudIconButton to simplify and reduce maintenance.
There was a problem hiding this comment.
The objective of this PR is to have as few as possible changes to the source code of MudBlazor to be sure my changes are non-breaking.
This redundant condition already existed before I added the SortIconColor, so I didn't removed it, but I can do it if you believe it's necessary.
|
|
||
| <div> | ||
| <MudIconButton Ripple="false" Icon="@context.SortIcon" Class="@context.HeaderCell.sortIconClass" Size="@Size.Small" title=@Localizer[LanguageResource.MudDataGrid_Sort] Disabled="!context.sortable" OnClick="@(e => context?.HeaderCell.SortChangedAsync(e))"></MudIconButton> | ||
| <MudIconButton Ripple="false" Icon="@context.SortIcon" Class="@context.HeaderCell.sortIconClass" Size="@Size.Small" Color="@context.SortIconColor" title=@Localizer[LanguageResource.MudDataGrid_Sort] Disabled="!context.sortable" OnClick="@(e => context?.HeaderCell.SortChangedAsync(e))"></MudIconButton> |
There was a problem hiding this comment.
This sort button lacks an aria-label, while similar buttons in HeaderCell.razor use one. Add aria-label='@localizer[LanguageResource.MudDataGrid_Sort]' for screen readers and consistency.
| <MudIconButton Ripple="false" Icon="@context.SortIcon" Class="@context.HeaderCell.sortIconClass" Size="@Size.Small" Color="@context.SortIconColor" title=@Localizer[LanguageResource.MudDataGrid_Sort] Disabled="!context.sortable" OnClick="@(e => context?.HeaderCell.SortChangedAsync(e))"></MudIconButton> | |
| <MudIconButton Ripple="false" Icon="@context.SortIcon" Class="@context.HeaderCell.sortIconClass" Size="@Size.Small" Color="@context.SortIconColor" title=@Localizer[LanguageResource.MudDataGrid_Sort] aria-label="@Localizer[LanguageResource.MudDataGrid_Sort]" Disabled="!context.sortable" OnClick="@(e => context?.HeaderCell.SortChangedAsync(e))"></MudIconButton> |
There was a problem hiding this comment.
Like the other comment, I didn't want to do any changes to the code beside adding the SortIconColor, so I didn't add the aria-label to the button, but I can also do this if you believe it's necessary.
| /// <summary> | ||
| /// The empty filter icon shown when <see cref="Filterable"/> is <c>true</c>. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string FilterIconEmpty { get; set; } = Icons.Material.Outlined.FilterAlt; | ||
|
|
||
| /// <summary> | ||
| /// The filled filter icon shown when <see cref="Filterable"/> is <c>true</c>. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string FilterIconFilled { get; set; } = Icons.Material.Filled.FilterAlt; | ||
|
|
||
| /// <summary> | ||
| /// The clear filter icon shown when <see cref="Filterable"/> is <c>true</c>. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string FilterIconClear { get; set; } = Icons.Material.Filled.FilterAltOff; | ||
|
|
There was a problem hiding this comment.
The summaries suggest icons are shown when Filterable is true, but their actual usage differs by state (empty when no filter is applied, filled when a filter is applied, clear in the filter header). Consider clarifying to: 'The empty filter icon used when no filter is applied,' 'The filled filter icon used when a filter is applied to the column,' and 'The clear filter icon used to remove the current filter in the filter header.'
There was a problem hiding this comment.
I can update these summaries to add a bit more precision. I suggest this:
The empty filter icon shown when Filterable is true and no filters are applied to the column.
The filled filter icon shown when Filterable is true and filters are applied to the column.
The clear filter icon shown when Filterable is true to remove filters applied to the column.
If you prefer something else, please let me know so I can change these to whatever you like the most.
|
I am looking now, hold off on changes until I'm done thinking. As for the unrelated changes the AI is suggesting, most are appreciated but not required since you didn't create the original issue. The exception is the aria changes if you are touching the code it has to be fixed. |
|
We aim to keep things as simple as possible (which you’ve also emphasized in your responses), and we really appreciate the work you’ve put into this. That said, I don’t think we should introduce Color parameters here. Since colors can be customized through CSS, it might be cleaner to rely on that. If an icon doesn’t already have a specific class attached, it would be helpful to add one (for example: As for the icons themselves, assigning them per column feels like it could get a bit heavy. With @danielchalmers ’ go-ahead, I’d suggest adding parameters for the icons at the MudDataGrid level using the defaults you’ve proposed. The column-level parameters could remain but simply fall back to the grid-level defaults. That way, users can set them once on the grid and override them per column if needed. Let’s wait for DC to confirm if that approach works before moving forward. As a final note, we will also need a unit test for the filter icons and a unit test for the sort icons verifying they change. |
Great points. I agree that if we don't include color logic then the icons themselves could be moved up to the MudDataGrid level. For sake of simplicity, I would remove the column-level parameters unless there's a solid use-case for that. |
|
@CyrilArtFX Tag me when you've made the changes. Thank you! |
|
I'll start working on the changes very soon, I'll let you know when they're done. |
|
@versile2 I just have a question to be sure I'm doing the changes you expect, you said that the filter icons parameters should be at the DataGrid level, and Daniel said that the override at Column level isn't necessary, so that's good for me, but what do I do for the sort icon?
|
Option 2 with an obsolete on column parameter would be best. V |
|
@versile2 I made the changes to move the icons parameters in the DataGrid component, remove the color parameters and add a Unit Test for the icons.
|
| .ToList().First(); | ||
| filterFilledButton.Html().Should().Contain(expectedFilterFilledIconPath); | ||
| } | ||
| } |
There was a problem hiding this comment.
The easiest way I find is to look at other components and see how they test it. In IconTests.cs there is a similar approach to this:
[Test]
public void DataGridFilterIconsTest2()
{
var comp = Context.RenderComponent<DataGridFilterIconsTest>();
MudIconButton firstButton() =>
comp.FindComponents<MudIconButton>().Where(x => x.Markup.Contains("filter-button")).FirstOrDefault().Instance;
var mudIconButton = firstButton();
mudIconButton.Icon.Should().Be(Icons.Material.Filled.Battery0Bar);
comp.Find("button.filter-button").Click();
mudIconButton = firstButton();
mudIconButton.Icon.Should().Be(Icons.Material.Filled.BatteryFull);
}That being said your test works. I do want to make sure this applies to all 3 modes of DataGridFilterMode though, the one you test is just Simple. Maybe make that a parameter you can pass and do testcases for the first the simple and ColumnMenu and a separate tests for ColumnFilterRow option?
There was a problem hiding this comment.
Thank you for your suggestion, it will make the test easier to understand, and it helped me expanding the test to other filter modes. I tried yesterday to search in other unit tests to understand how it would work to properly test an icon, but couldn't succeed.
I updated the unit test to be more complete, and also had to add a class filter-button clear to the clear filter button of the filter mode "ColumnFilterRow" to be able to find it in the unit test. It also allows it to be colored with CSS (again, tested and working).
There was a problem hiding this comment.
you can always ping me here or on discord if something stumps you. I've been through a lot of the Mud code at some point.
Please feel free to ignore the out-of-scope review from the bot |
This PR addresses the the issue #7478 by giving the possibility to customize the icons used in the filter feature of the MudDataGrid, as well as a possibility to change the color of the sort and filter icons of the MudDataGrid.
The intended use-case is to have more feedback when the filter feature is used for a better user experience.
(For example, having the filter icon of a column become yellow when filters are applied on this column.)
I made the changes as light as possible, by just creating new parameters to replace hard coded values and not adding any logic.
Feel free to suggest better names for those parameters if you have ideas.
Checklist: