Skip to content

Commit

Permalink
fix: Open suggestion list when focus, reflect text changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Sep 8, 2021
1 parent fbadc09 commit 8dc6472
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<string>() { "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<string>() { "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();
}
}
}
16 changes: 15 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/AutoSuggestBox/AutoSuggestBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected override void OnApplyTemplate()
#endif

UpdateQueryButton();
UpdateTextBox();

_textBoxBinding = new BindingPath("Text", null) { DataContext = _textBox, ValueChangedListener = this };

Expand Down Expand Up @@ -119,7 +120,7 @@ private void UpdateSuggestionList()
{
IsSuggestionListOpen = false;
}
else
else if (_textBox?.IsFocused ?? false)
{
IsSuggestionListOpen = true;
_suggestionsList.ItemsSource = GetItems();
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8dc6472

Please sign in to comment.