Skip to content

MudSelect: Give ToStringFunc Higher Precedence#12837

Merged
danielchalmers merged 6 commits intoMudBlazor:devfrom
Anu6is:MudSelectToStringFunc
Mar 14, 2026
Merged

MudSelect: Give ToStringFunc Higher Precedence#12837
danielchalmers merged 6 commits intoMudBlazor:devfrom
Anu6is:MudSelectToStringFunc

Conversation

@Anu6is
Copy link
Contributor

@Anu6is Anu6is commented Mar 9, 2026

This PR implements the request in issue #12729 by changing the precedence of how the selected value is displayed in MudSelect.

Previously, if a MudSelectItem had ChildContent (a RenderFragment), it would always be used for the selection display in the select box, even if the parent MudSelect had a ToStringFunc defined.

With this change:

  1. If ToStringFunc is provided and returns a non-null, non-empty string for the selected value, that string will be displayed in the select box (as text).
  2. If ToStringFunc is not provided, or returns a null/empty string, the component falls back to displaying the ChildContent of the selected item (if any).
  3. The items inside the popover (dropdown list) continue to use their ChildContent as before.
  4. The FitContent property correctly accounts for this precedence when calculating the required width of the component.

Closes #12729

Checklist:

  • I've read the contribution guidelines
  • My code follows the style of this project
  • I've added or updated relevant unit tests

@Anu6is Anu6is requested a review from versile2 as a code owner March 9, 2026 14:19
@mudbot mudbot bot changed the title ToStringFunc Precedence MudSelect: Change ToStringFunc precedence Mar 9, 2026
@mudbot mudbot bot added the enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library label Mar 9, 2026
@Anu6is Anu6is changed the title MudSelect: Change ToStringFunc precedence MudSelect: Give ToStringFunc Higher Precedence Mar 9, 2026
@versile2 versile2 requested a review from Copilot March 9, 2026 14:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates MudSelect selection rendering so a provided ToStringFunc takes precedence over a selected item's ChildContent when determining what text is shown in the collapsed select box (and aligns FitContent sizing and docs/tests with that behavior).

Changes:

  • Adjust selection rendering logic to prefer ToStringFunc output (when non-empty) over selected item ChildContent.
  • Update FitContent filler rendering to reflect the new precedence.
  • Add unit tests, a test component, and documentation/example updates for the new behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/MudBlazor/Components/Select/MudSelect.razor.cs Updates render decision (CanRenderValue) so non-empty ToStringFunc disables ChildContent presenter for the selected value.
src/MudBlazor/Components/Select/MudSelect.razor Updates FitContent filler rendering to prefer ToStringFunc text when available.
src/MudBlazor.UnitTests/Components/SelectTests.cs Adds coverage for selection display precedence and FitContent behavior with ToStringFunc.
src/MudBlazor.UnitTests.Viewer/TestComponents/Select/SelectPrecedenceTest.razor Adds a viewer test component used by the new unit tests.
src/MudBlazor.Docs/Pages/Components/Select/SelectPage.razor Documents the new precedence behavior.
src/MudBlazor.Docs/Pages/Components/Select/Examples/SelectPresentationExample.razor Adds an example demonstrating ToStringFunc precedence with item render fragments.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@versile2 versile2 left a comment

Choose a reason for hiding this comment

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

Ok all of the code is good. I like it, but it is a breaking change. I do not believe we can implement a breaking change in the beginning of a release cycle unless it is directly related to a major bug so I have a suggestion, perhaps @ScarletKuro can provide direction as to the best path forward.

Currently all consumers are forced to use ChildContent when it exists for their MudSelectItem, then ToStringFunc, and finally defaults to a default implemention of .ToString() of the object/class.

Keep that behavior but add a property on the MudSelectItem that says ShowContent with a default to ChildContent that can be toggled to ChildContent and ToStringFunc, add a note on the V10 project board to make the breaking change.

V

@Anu6is
Copy link
Contributor Author

Anu6is commented Mar 11, 2026

Currently all consumers are forced to use ChildContent when it exists for their MudSelectItem, then ToStringFunc, and finally defaults to a default implemention of .ToString() of the object/class.

When you use ChildContent currently, you don't have the option of using ToStringFunc it will not do anything. Previously, when using ChildContent on MudSelectItem, the selected value text did not respect ToStringFunc. As such, you'd never have them both set at the same time on a MudSelectItem.

This change allows both to work together.

  • ChildContent controls how items appear in the dropdown list,
  • ToStringFunc determines how the selected value is displayed in the MudSelect input.

@versile2
Copy link
Contributor

Currently all consumers are forced to use ChildContent when it exists for their MudSelectItem, then ToStringFunc, and finally defaults to a default implemention of .ToString() of the object/class.

When you use ChildContent currently, you don't have the option of using ToStringFunc it will not do anything. Previously, when using ChildContent on MudSelectItem, the selected value text did not respect ToStringFunc. As such, you'd never have them both set at the same time on a MudSelectItem.

This change allows both to work together.

  • ChildContent controls how items appear in the dropdown list,
  • ToStringFunc determines how the selected value is displayed in the MudSelect input.

Solid Reasoning and I agree. Can you clarify the documentation a bit for future people looking.

Screenshot_20260311-185213.png

@Anu6is
Copy link
Contributor Author

Anu6is commented Mar 11, 2026

The PR includes the following addition to the doc

If you provide a ToStringFunc, it takes precedence over the item's render fragment for the selection display, provided it returns a non-empty string.

@Anu6is
Copy link
Contributor Author

Anu6is commented Mar 12, 2026

image

@versile2 versile2 self-requested a review March 12, 2026 01:28
Copy link
Contributor

@versile2 versile2 left a comment

Choose a reason for hiding this comment

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

Lgtm, ty!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow MudSelect to use ToStringFunc for selection display when items have custom RenderFragments

5 participants