-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
MudToggleItem: Enforce direct parent hierarchy #11498
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
MudToggleItem: Enforce direct parent hierarchy #11498
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enforces that MudToggleItem must always be used within a MudToggleGroup by validating the cascading parent and removing null‐conditional logic throughout the component.
- Adds a private non-nullable
Parentproperty that throws an exception if no group is found. - Renames the public cascading parameter to
NullableParentand updates registration/unregistration to use it. - Removes
?.checks in class builders and lifecycle methods in favor of the validatedParentproperty.
Comments suppressed due to low confidence (4)
src/MudBlazor/Components/Toggle/MudToggleItem.razor.cs:41
- [nitpick] The public cascading parameter property
NullableParentname is ambiguous; consider renaming it to something likeCascadingParentorParentCascadingto better convey its purpose.
public MudToggleGroup<T>? NullableParent { get; set; }
src/MudBlazor/Components/Toggle/MudToggleItem.razor.cs:134
- Consider adding a unit test to verify that a
MudToggleItemwithout a parent throws the expectedInvalidOperationExceptionduring initialization.
Parent.Register(this);
src/MudBlazor/Components/Toggle/MudToggleItem.razor.cs:41
- [nitpick] Add an XML
<summary>comment for theNullableParentcascading parameter property to explain its purpose and why it differs from the privateParentproperty.
public MudToggleGroup<T>? NullableParent { get; set; }
src/MudBlazor/Components/Toggle/MudToggleItem.razor.cs:27
- Previously there was a fallback to
Size.MediumwhenParent.Sizewas null; the new code always usesParent.Size. EnsureParent.Sizeis always initialized or reintroduce a fallback to avoid unexpected default values.
.AddClass($"mud-toggle-item-size-{Parent.Size.ToDescriptionString()}")
|
Is this a change for v9? Since it's breaking change. to Also I'm not really fan of that |
just made |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #11498 +/- ##
=======================================
Coverage 91.14% 91.15%
=======================================
Files 466 466
Lines 14497 14499 +2
Branches 2816 2815 -1
=======================================
+ Hits 13214 13217 +3
Misses 644 644
+ Partials 639 638 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
It's not about setting though. Someone might have accessed the |
Mmm im skeptical but i understand what youre going for so swapped the names around and now Parent is the same as before |
|
Sample code<MudToggleGroup T="string" SelectionMode="SelectionMode.SingleSelection" @bind-Value="_value1" Color="Color.Primary" CheckMark FixedContent>
<MudToggleItem @ref="_toggleItem" Value="@("Yes")" Text="Yes" />
<MudToggleItem Value="@("No")" Text="No" />
<MudToggleItem Value="@("Don't know")" Text="Don't know" />
</MudToggleGroup>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="ButtonOnClick">Invoke Me</MudButton>
@code {
private string _value1 = "Yes";
private MudToggleItem<string> _toggleItem;
private void ButtonOnClick()
{
var value = _toggleItem.Parent.Value; //Ups if you remove it.
Console.WriteLine(value);
}
}It doesn’t matter and it doesn't matter how far-fetched or contrived this example may seem, or how low the risk is. We don't know how others write their code, so we can't just add an API and then remove it. If we want to do that, it should only happen in the next major version. We’ve been through this before and were criticized for it when we pulled something similar in non major version. |



Description
You should never have a lone toggle item and it breaks the styles so why design the code around that?
How Has This Been Tested?
Type of Changes
Checklist
dev).