Skip to content

Commit

Permalink
fix: fix 22183 RadioButton checked value (#22186)
Browse files Browse the repository at this point in the history
* fix: fix 22183 RadioButton checked value

* Update src/Controls/src/Core/RadioButton/RadioButtonGroup.cs

format code

Co-authored-by: Dan Moseley <[email protected]>

---------

Co-authored-by: Dan Moseley <[email protected]>
  • Loading branch information
maonaoda and danmoseley committed May 13, 2024
1 parent bfc9876 commit 8b10f39
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/Controls/src/Core/RadioButton/RadioButton.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable disable
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Devices;
Expand Down Expand Up @@ -426,7 +425,7 @@ bool MatchesScope(RadioButtonScopeMessage message)

void HandleRadioButtonGroupSelectionChanged(RadioButton selected, RadioButtonGroupSelectionChanged args)
{
if (!IsChecked || selected == this || string.IsNullOrEmpty(GroupName) || GroupName != selected.GroupName || !MatchesScope(args))
if (!IsChecked || selected == this || string.IsNullOrEmpty(GroupName) || GroupName != selected.GroupName || object.Equals(Value, args.Value) || !MatchesScope(args))
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/RadioButton/RadioButtonGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal static void UpdateRadioButtonGroup(RadioButton radioButton)

#pragma warning disable CS0618 // TODO: Remove when we internalize/replace MessagingCenter
MessagingCenter.Send(radioButton, GroupSelectionChangedMessage,
new RadioButtonGroupSelectionChanged(scope));
new RadioButtonGroupSelectionChanged(scope, radioButton.Value));
#pragma warning restore CS0618 // Type or member is obsolete
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ internal abstract class RadioButtonScopeMessage

internal class RadioButtonGroupSelectionChanged : RadioButtonScopeMessage
{
public RadioButtonGroupSelectionChanged(Element scope) : base(scope) { }
public object Value { get; }

public RadioButtonGroupSelectionChanged(Element scope, object value) : base(scope)
{
Value = value;
}
}

internal class RadioButtonGroupNameChanged : RadioButtonScopeMessage
Expand Down
38 changes: 19 additions & 19 deletions src/Controls/tests/Core.UnitTests/RadioButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void RadioButtonAddedToGroupGetsGroupName()
{
var layout = new StackLayout();
var groupName = "foo";
var radioButton = new RadioButton();
var radioButton = new RadioButton() { Value = 1 };

layout.SetValue(RadioButtonGroup.GroupNameProperty, groupName);
layout.Children.Add(radioButton);
Expand All @@ -26,7 +26,7 @@ public void NestedRadioButtonAddedToGroupGetsGroupName()
{
var layout = new StackLayout();
var groupName = "foo";
var radioButton = new RadioButton();
var radioButton = new RadioButton() { Value = 1 };

layout.SetValue(RadioButtonGroup.GroupNameProperty, groupName);

Expand All @@ -44,7 +44,7 @@ public void RadioButtonAddedToGroupKeepsGroupName()
var layout = new StackLayout();
var groupName = "foo";
var oldName = "bar";
var radioButton = new RadioButton() { GroupName = oldName };
var radioButton = new RadioButton() { GroupName = oldName, Value = 1 };

layout.SetValue(RadioButtonGroup.GroupNameProperty, groupName);
layout.Children.Add(radioButton);
Expand All @@ -57,7 +57,7 @@ public void LayoutGroupNameAppliesToExistingRadioButtons()
{
var layout = new StackLayout();
var groupName = "foo";
var radioButton = new RadioButton();
var radioButton = new RadioButton() { Value = 1 };

layout.Children.Add(radioButton);
layout.SetValue(RadioButtonGroup.GroupNameProperty, groupName);
Expand All @@ -72,8 +72,8 @@ public void UpdatedGroupNameAppliesToRadioButtonsWithOldGroupName()
var groupName = "foo";
var updatedGroupName = "bar";
var otherGroupName = "other";
var radioButton1 = new RadioButton();
var radioButton2 = new RadioButton() { GroupName = otherGroupName };
var radioButton1 = new RadioButton() { Value = 1 };
var radioButton2 = new RadioButton() { GroupName = otherGroupName, Value = 2 };

layout.Children.Add(radioButton1);
layout.Children.Add(radioButton2);
Expand All @@ -90,10 +90,10 @@ public void ThereCanBeOnlyOne()
{
var groupName = "foo";

var radioButton1 = new RadioButton() { GroupName = groupName };
var radioButton2 = new RadioButton() { GroupName = groupName };
var radioButton3 = new RadioButton() { GroupName = groupName };
var radioButton4 = new RadioButton() { GroupName = groupName };
var radioButton1 = new RadioButton() { GroupName = groupName, Value = 1 };
var radioButton2 = new RadioButton() { GroupName = groupName, Value = 2 };
var radioButton3 = new RadioButton() { GroupName = groupName, Value = 3 };
var radioButton4 = new RadioButton() { GroupName = groupName, Value = 4 };

var layout = new Grid();

Expand All @@ -120,9 +120,9 @@ public void ThereCanBeOnlyOne()
[Fact]
public void ImpliedGroup()
{
var radioButton1 = new RadioButton();
var radioButton2 = new RadioButton();
var radioButton3 = new RadioButton();
var radioButton1 = new RadioButton() { Value = 1 };
var radioButton2 = new RadioButton() { Value = 2 };
var radioButton3 = new RadioButton() { Value = 3 };

var layout = new Grid();

Expand All @@ -146,9 +146,9 @@ public void ImpliedGroup()
[Fact]
public void ImpliedGroupDoesNotIncludeExplicitGroups()
{
var radioButton1 = new RadioButton();
var radioButton2 = new RadioButton();
var radioButton3 = new RadioButton() { GroupName = "foo" };
var radioButton1 = new RadioButton() { Value = 1 };
var radioButton2 = new RadioButton() { Value = 2 };
var radioButton3 = new RadioButton() { GroupName = "foo", Value = 3 };

var layout = new Grid();

Expand All @@ -167,9 +167,9 @@ public void ImpliedGroupDoesNotIncludeExplicitGroups()
[Fact]
public void RemovingSelectedButtonFromGroupClearsSelection()
{
var radioButton1 = new RadioButton() { GroupName = "foo" };
var radioButton2 = new RadioButton() { GroupName = "foo" };
var radioButton3 = new RadioButton() { GroupName = "foo" };
var radioButton1 = new RadioButton() { GroupName = "foo", Value = 1 };
var radioButton2 = new RadioButton() { GroupName = "foo", Value = 2 };
var radioButton3 = new RadioButton() { GroupName = "foo", Value = 3 };

radioButton1.IsChecked = true;
radioButton2.IsChecked = true;
Expand Down

0 comments on commit 8b10f39

Please sign in to comment.