From cff99f47b535ea1ee7ec0d5451207e2083402c5c Mon Sep 17 00:00:00 2001 From: maonaoda Date: Fri, 3 May 2024 17:39:07 +0900 Subject: [PATCH 1/2] fix: fix 22183 RadioButton checked value --- .../src/Core/RadioButton/RadioButton.cs | 3 +- .../src/Core/RadioButton/RadioButtonGroup.cs | 2 +- .../RadioButtonGroupSelectionChanged.cs | 7 +++- .../tests/Core.UnitTests/RadioButtonTests.cs | 38 +++++++++---------- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/Controls/src/Core/RadioButton/RadioButton.cs b/src/Controls/src/Core/RadioButton/RadioButton.cs index c367ddc1ae5b..39ef9b6291e1 100644 --- a/src/Controls/src/Core/RadioButton/RadioButton.cs +++ b/src/Controls/src/Core/RadioButton/RadioButton.cs @@ -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; @@ -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; } diff --git a/src/Controls/src/Core/RadioButton/RadioButtonGroup.cs b/src/Controls/src/Core/RadioButton/RadioButtonGroup.cs index 179e9adde3f5..aae5bee19e64 100644 --- a/src/Controls/src/Core/RadioButton/RadioButtonGroup.cs +++ b/src/Controls/src/Core/RadioButton/RadioButtonGroup.cs @@ -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 } diff --git a/src/Controls/src/Core/RadioButton/RadioButtonGroupSelectionChanged.cs b/src/Controls/src/Core/RadioButton/RadioButtonGroupSelectionChanged.cs index d801c2b0ff62..816081426f1d 100644 --- a/src/Controls/src/Core/RadioButton/RadioButtonGroupSelectionChanged.cs +++ b/src/Controls/src/Core/RadioButton/RadioButtonGroupSelectionChanged.cs @@ -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 diff --git a/src/Controls/tests/Core.UnitTests/RadioButtonTests.cs b/src/Controls/tests/Core.UnitTests/RadioButtonTests.cs index 03791f5d422a..1bdab98ed4aa 100644 --- a/src/Controls/tests/Core.UnitTests/RadioButtonTests.cs +++ b/src/Controls/tests/Core.UnitTests/RadioButtonTests.cs @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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(); @@ -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(); @@ -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(); @@ -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; From 8aebcdec4e266ea7d275ca7e2b7cb271b5036e50 Mon Sep 17 00:00:00 2001 From: maonaoda <32831595+maonaoda@users.noreply.github.com> Date: Mon, 6 May 2024 09:59:33 +0900 Subject: [PATCH 2/2] Update src/Controls/src/Core/RadioButton/RadioButtonGroup.cs format code Co-authored-by: Dan Moseley --- src/Controls/src/Core/RadioButton/RadioButtonGroup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controls/src/Core/RadioButton/RadioButtonGroup.cs b/src/Controls/src/Core/RadioButton/RadioButtonGroup.cs index aae5bee19e64..a5855eeebb8b 100644 --- a/src/Controls/src/Core/RadioButton/RadioButtonGroup.cs +++ b/src/Controls/src/Core/RadioButton/RadioButtonGroup.cs @@ -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,radioButton.Value)); + new RadioButtonGroupSelectionChanged(scope, radioButton.Value)); #pragma warning restore CS0618 // Type or member is obsolete }