diff --git a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/SearchHandlerAppearanceTracker.cs b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/SearchHandlerAppearanceTracker.cs index 3096d9eab8f4..3559d76f03ef 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/SearchHandlerAppearanceTracker.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/SearchHandlerAppearanceTracker.cs @@ -117,6 +117,10 @@ void SearchHandlerPropertyChanged(object sender, System.ComponentModel.PropertyC { UpdateSearchBarVerticalTextAlignment(_uiSearchBar.FindDescendantView()); } + else if (e.Is(SearchHandler.ShowsCancelButtonProperty)) + { + UpdateShowsCancelButton(); + } } void GetDefaultSearchBarColors(UISearchBar searchBar) @@ -319,13 +323,29 @@ void UpdateKeyboard() _uiSearchBar.ReloadInputViews(); } + void UpdateShowsCancelButton() + { + if (_searchHandler.IsFocused) + { + _uiSearchBar.SetShowsCancelButton(_searchHandler.ShowsCancelButton, true); + } + } + void OnEditingEnded(object sender, EventArgs e) { + if (_searchHandler.ShowsCancelButton) + { + _uiSearchBar.SetShowsCancelButton(false, true); + } _searchHandler.SetIsFocused(false); } void OnEditingStarted(object sender, EventArgs e) { + if (_searchHandler.ShowsCancelButton) + { + _uiSearchBar.SetShowsCancelButton(true, true); + } UpdateCancelButtonColor(_uiSearchBar.FindDescendantView()); _searchHandler.SetIsFocused(true); //ElementController?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true); diff --git a/src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt index 7dc5c58110bf..46d06ac20dc4 100644 --- a/src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt @@ -1 +1,7 @@ #nullable enable +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchBar.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchHandler.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt index 7d0110928035..784936d097a1 100644 --- a/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt @@ -1,2 +1,8 @@ #nullable enable ~override Microsoft.Maui.Controls.Platform.Compatibility.ShellFlyoutRenderer.ViewWillTransitionToSize(CoreGraphics.CGSize toSize, UIKit.IUIViewControllerTransitionCoordinator coordinator) -> void +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchBar.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchHandler.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt index 7d0110928035..784936d097a1 100644 --- a/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt @@ -1,2 +1,8 @@ #nullable enable ~override Microsoft.Maui.Controls.Platform.Compatibility.ShellFlyoutRenderer.ViewWillTransitionToSize(CoreGraphics.CGSize toSize, UIKit.IUIViewControllerTransitionCoordinator coordinator) -> void +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchBar.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchHandler.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt index 7dc5c58110bf..46d06ac20dc4 100644 --- a/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt @@ -1 +1,7 @@ #nullable enable +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchBar.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchHandler.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt index 7dc5c58110bf..46d06ac20dc4 100644 --- a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt @@ -1 +1,7 @@ #nullable enable +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchBar.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchHandler.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt index 7dc5c58110bf..46d06ac20dc4 100644 --- a/src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt @@ -1 +1,7 @@ #nullable enable +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchBar.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchHandler.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt index 7dc5c58110bf..46d06ac20dc4 100644 --- a/src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt @@ -1 +1,7 @@ #nullable enable +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchBar.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchBar.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.get -> bool +Microsoft.Maui.Controls.SearchHandler.ShowsCancelButton.set -> void +~static readonly Microsoft.Maui.Controls.SearchHandler.ShowsCancelButtonProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/SearchBar/SearchBar.cs b/src/Controls/src/Core/SearchBar/SearchBar.cs index fbc3b649e5f3..964f5e6dbb9a 100644 --- a/src/Controls/src/Core/SearchBar/SearchBar.cs +++ b/src/Controls/src/Core/SearchBar/SearchBar.cs @@ -33,6 +33,9 @@ public partial class SearchBar : InputView, ITextAlignmentElement, ISearchBarCon /// Bindable property for . public static readonly BindableProperty CancelButtonColorProperty = BindableProperty.Create(nameof(CancelButtonColor), typeof(Color), typeof(SearchBar), default(Color)); + /// Bindable property for . + public static readonly BindableProperty ShowsCancelButtonProperty = BindableProperty.Create(nameof(ShowsCancelButton), typeof(bool), typeof(SearchBar), true); + /// Bindable property for . public static readonly BindableProperty SearchIconColorProperty = BindableProperty.Create(nameof(SearchIconColor), typeof(Color), typeof(SearchBar), default(Color)); @@ -94,6 +97,20 @@ public Color CancelButtonColor get { return (Color)GetValue(CancelButtonColorProperty); } set { SetValue(CancelButtonColorProperty, value); } } + + /// + /// Gets or sets a value that indicates whether the cancel button is shown. + /// + /// + /// On iOS, the cancel button appears when the search bar is focused and allows users to dismiss the keyboard. + /// Default value is true. + /// + public bool ShowsCancelButton + { + get { return (bool)GetValue(ShowsCancelButtonProperty); } + set { SetValue(ShowsCancelButtonProperty, value); } + } + /// /// Gets or sets the color of the search icon in the . /// diff --git a/src/Controls/src/Core/Shell/SearchHandler.cs b/src/Controls/src/Core/Shell/SearchHandler.cs index 0788e13052db..1d4bf38f3d9c 100644 --- a/src/Controls/src/Core/Shell/SearchHandler.cs +++ b/src/Controls/src/Core/Shell/SearchHandler.cs @@ -157,6 +157,9 @@ public Color TextColor /// Bindable property for . public static readonly BindableProperty CancelButtonColorProperty = BindableProperty.Create(nameof(CancelButtonColor), typeof(Color), typeof(SearchHandler), default(Color)); + /// Bindable property for . + public static readonly BindableProperty ShowsCancelButtonProperty = BindableProperty.Create(nameof(ShowsCancelButton), typeof(bool), typeof(SearchHandler), true); + /// Bindable property for . public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty; @@ -200,6 +203,19 @@ public Color CancelButtonColor set { SetValue(CancelButtonColorProperty, value); } } + /// + /// Gets or sets a value that indicates whether the cancel button is shown. + /// + /// + /// On iOS, the cancel button appears when the search bar is focused and allows users to dismiss the keyboard. + /// Default value is true. + /// + public bool ShowsCancelButton + { + get { return (bool)GetValue(ShowsCancelButtonProperty); } + set { SetValue(ShowsCancelButtonProperty, value); } + } + /// public FontAttributes FontAttributes diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue33008.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue33008.xaml new file mode 100644 index 000000000000..0ce1d565057c --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue33008.xaml @@ -0,0 +1,64 @@ + + + + + + + + + +