diff --git a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellItemRenderer.cs b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellItemRenderer.cs index c3fecab58e1e..985da5b65b1e 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellItemRenderer.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellItemRenderer.cs @@ -246,6 +246,9 @@ protected virtual void OnItemsCollectionChanged(object sender, NotifyCollectionC ViewControllers = viewControllers; CustomizableViewControllers = Array.Empty(); + // Apply initial IsEnabled state for each tab item + SetTabItemsEnabledState(); + if (goTo) GoTo(ShellItem.CurrentItem); } @@ -290,6 +293,28 @@ void AddRenderer(IShellSectionRenderer renderer) renderer.ShellSection.PropertyChanged += OnShellSectionPropertyChanged; } + void SetTabItemsEnabledState() + { + if (TabBar?.Items is null) + { + return; + } + + var items = ShellItemController.GetItems(); + if (items is null) + { + return; + } + + if (TabBar.Items.Length >= items.Count) + { + for (int tabIndex = 0; tabIndex < items.Count; tabIndex++) + { + TabBar.Items[tabIndex].Enabled = items[tabIndex].IsEnabled; + } + } + } + void CreateTabRenderers() { if (ShellItem.CurrentItem == null) @@ -315,6 +340,9 @@ void CreateTabRenderers() ViewControllers = viewControllers; CustomizableViewControllers = Array.Empty(); + // Apply initial IsEnabled state for newly added tab items + SetTabItemsEnabledState(); + UpdateTabBarHidden(); // Make sure we are at the right item diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue33158.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue33158.cs new file mode 100644 index 000000000000..cc0a5a974e91 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue33158.cs @@ -0,0 +1,130 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 33158, "IsEnabledProperty should work on Tabs", PlatformAffected.iOS)] +public class Issue33158 : Shell +{ + public Issue33158() + { + var mainPageTab = new Tab + { + Title = "FirstPage", + IsEnabled = true, + }; + mainPageTab.Items.Add(new ShellContent + { + ContentTemplate = new DataTemplate(() => new Issue33158MainPage()) + }); + + var secondPageTab = new Tab + { + Title = "SecondTab", + IsEnabled = false, + AutomationId = "SecondTab" + }; + secondPageTab.Items.Add(new ShellContent + { + ContentTemplate = new DataTemplate(() => new Issue33158SecondPage()) + }); + var thirdTab = new Tab + { + Title = "ThirdTab", + IsEnabled = true, + AutomationId = "ThirdTab" + }; + thirdTab.Items.Add(new ShellContent + { + ContentTemplate = new DataTemplate(() => new Issue33158ThirdPage()) + }); + var tabBar = new TabBar(); + tabBar.Items.Add(mainPageTab); + tabBar.Items.Add(secondPageTab); + tabBar.Items.Add(thirdTab); + Items.Add(tabBar); + } + + public class Issue33158MainPage : ContentPage + { + public Issue33158MainPage() + { + Content = new StackLayout + { + VerticalOptions = LayoutOptions.Center, + HorizontalOptions = LayoutOptions.Center, + Children = + { + new Label + { + Text = "This is First Page", + } + } + }; + } + } + + public class Issue33158SecondPage : ContentPage + { + public Issue33158SecondPage() + { + Content = new StackLayout + { + VerticalOptions = LayoutOptions.Center, + HorizontalOptions = LayoutOptions.Center, + Children = + { + new Label + { + Text = "This is Second Page", + AutomationId = "SecondPageLabel" + } + } + }; + } + } + public class Issue33158ThirdPage : ContentPage + { + public Issue33158ThirdPage() + { + var label = new Label + { + Text = "This is Third Page", + HorizontalOptions = LayoutOptions.Center, + VerticalOptions = LayoutOptions.Center, + AutomationId = "ThirdPageLabel" + }; + + var button = new Button + { + Text = "Enable SecondTab", + HorizontalOptions = LayoutOptions.Center, + VerticalOptions = LayoutOptions.Center, + AutomationId = "EnableSecondTab" + }; + button.Clicked += OnButtonClicked; + Content = new StackLayout + { + VerticalOptions = LayoutOptions.Center, + HorizontalOptions = LayoutOptions.Center, + Children = + { + label, + button + } + }; + } + + private void OnButtonClicked(object sender, EventArgs e) + { + if (Application.Current?.Windows.Count > 0 && + Application.Current.Windows[0].Page is Shell shell) + { + var secondTab = shell.CurrentItem?.Items[1]; + if (secondTab is not null) + secondTab.IsEnabled = true; + } + else + { + System.Diagnostics.Debug.WriteLine("Shell not found!"); + } + } + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33158.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33158.cs new file mode 100644 index 000000000000..064df4be9715 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33158.cs @@ -0,0 +1,30 @@ +#if TEST_FAILS_ON_WINDOWS // Existing PR for windows - https://github.com/dotnet/maui/pull/26728 +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue33158 : _IssuesUITest +{ + public Issue33158(TestDevice testDevice) : base(testDevice) + { + } + + public override string Issue => "IsEnabledProperty should work on Tabs"; + + [Test] + [Category(UITestCategories.Shell)] + public void Issue33158CheckIsEnabled() + { + App.WaitForElement("ThirdTab"); + App.Tap("ThirdTab"); + App.WaitForElement("ThirdPageLabel"); + App.Tap("SecondTab"); + App.WaitForNoElement("SecondPageLabel"); + App.Tap("EnableSecondTab"); + App.Tap("SecondTab"); + App.WaitForElement("SecondPageLabel"); + } +} +#endif \ No newline at end of file