From 8dc64726e69ae6567779ba1a611790671147586f Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 8 Sep 2021 11:58:55 +0200 Subject: [PATCH] fix: Open suggestion list when focus, reflect text changes --- .../Given_AutoSuggestBox.cs | 56 +++++++++++++++++++ .../Controls/AutoSuggestBox/AutoSuggestBox.cs | 16 +++++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AutoSuggestBox.cs diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AutoSuggestBox.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AutoSuggestBox.cs new file mode 100644 index 000000000000..3b38ff96dfe6 --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AutoSuggestBox.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using static Private.Infrastructure.TestServices; + +namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls +{ + [TestClass] + [RunsOnUIThread] + public class Given_AutoSuggestBox + { + [TestMethod] + public async Task When_Text_Changed_Should_Reflect_In_DataTemplate_TextBox() + { + var SUT = new AutoSuggestBox(); + SUT.Text = "New text.."; + WindowHelper.WindowContent = SUT; + await WindowHelper.WaitForIdle(); + var textBox = (TextBox)SUT.GetTemplateChild("TextBox"); + textBox.Text.Should().Be("New text.."); + } + + [TestMethod] + public async Task When_Text_Changed_And_Not_Focused_Should_Not_Open_Suggestion_List() + { + var SUT = new AutoSuggestBox(); + SUT.ItemsSource = new List() { "ab", "abc", "abcde" }; + WindowHelper.WindowContent = SUT; + await WindowHelper.WaitForIdle(); + + var textBox = (TextBox)SUT.GetTemplateChild("TextBox"); + textBox.IsFocused.Should().BeFalse(); + SUT.Text = "a"; + SUT.IsSuggestionListOpen.Should().BeFalse(); + } + + [TestMethod] + public async Task When_Text_Changed_And_Focused_Should_Open_Suggestion_List() + { + var SUT = new AutoSuggestBox(); + SUT.ItemsSource = new List() { "ab", "abc", "abcde" }; + WindowHelper.WindowContent = SUT; + await WindowHelper.WaitForIdle(); + + var textBox = (TextBox)SUT.GetTemplateChild("TextBox"); + SUT.Focus(FocusState.Programmatic); + textBox.IsFocused.Should().BeTrue(); + SUT.Text = "a"; + SUT.IsSuggestionListOpen.Should().BeTrue(); + } + } +} diff --git a/src/Uno.UI/UI/Xaml/Controls/AutoSuggestBox/AutoSuggestBox.cs b/src/Uno.UI/UI/Xaml/Controls/AutoSuggestBox/AutoSuggestBox.cs index e3f3923e76ec..735f7c82375b 100644 --- a/src/Uno.UI/UI/Xaml/Controls/AutoSuggestBox/AutoSuggestBox.cs +++ b/src/Uno.UI/UI/Xaml/Controls/AutoSuggestBox/AutoSuggestBox.cs @@ -53,6 +53,7 @@ protected override void OnApplyTemplate() #endif UpdateQueryButton(); + UpdateTextBox(); _textBoxBinding = new BindingPath("Text", null) { DataContext = _textBox, ValueChangedListener = this }; @@ -119,7 +120,7 @@ private void UpdateSuggestionList() { IsSuggestionListOpen = false; } - else + else if (_textBox?.IsFocused ?? false) { IsSuggestionListOpen = true; _suggestionsList.ItemsSource = GetItems(); @@ -283,6 +284,16 @@ private void UpdateQueryButton() _queryButton.Visibility = QueryIcon == null ? Visibility.Collapsed : Visibility.Visible; } + private void UpdateTextBox() + { + if (_textBox == null) + { + return; + } + + _textBox.Text = Text; + } + private void OnSuggestionListItemClick(object sender, ItemClickEventArgs e) { if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug)) @@ -413,6 +424,9 @@ private static void OnTextChanged(DependencyObject dependencyObject, DependencyP if (dependencyObject is AutoSuggestBox tb) { + tb.UpdateTextBox(); + tb.UpdateSuggestionList(); + if (tb._textChangeReason == AutoSuggestionBoxTextChangeReason.UserInput) { tb.UpdateUserInput(newValue);