Skip to content

Commit

Permalink
Fixes #3106: Combo Box selection fixes (#3117)
Browse files Browse the repository at this point in the history
* Search selected item on filtered set

* Fix unit test - don't return first item when combobox is not focused or showed

* Improve method readability

* Clean selection from previous search

* Update unit test - after changing search text unselect previously selected item
  • Loading branch information
MaciekWin3 committed Jan 4, 2024
1 parent 8954fa5 commit 3d829d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
13 changes: 8 additions & 5 deletions Terminal.Gui/Views/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,13 @@ private void Selected ()
isShow = false;
}

private int GetSelectedItemFromSource (string value)
private int GetSelectedItemFromSource (string searchText)
{
if (source == null) {
if (source is null) {
return -1;
}
for (int i = 0; i < source.Count; i++) {
if (source.ToList () [i].ToString () == value) {
for (int i = 0; i < searchset.Count; i++) {
if (searchset [i].ToString () == searchText) {
return i;
}
}
Expand Down Expand Up @@ -815,13 +815,16 @@ private void SetSearchSet ()

private void Search_Changed (object sender, TextChangedEventArgs e)
{
if (source == null) { // Object initialization
if (source is null) { // Object initialization
return;
}

if (string.IsNullOrEmpty (search.Text) && string.IsNullOrEmpty (e.OldValue)) {
ResetSearchSet ();
} else if (search.Text != e.OldValue) {
if (search.Text.Length < e.OldValue.Length) {
selectedItem = -1;
}
isShow = true;
ResetSearchSet (noCopy: true);

Expand Down
8 changes: 4 additions & 4 deletions UnitTests/Views/ComboBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,17 @@ public void KeyBindings_Command ()
Assert.True (cb.NewKeyDownEvent (new (KeyCode.CursorDown))); // losing focus
Assert.False (cb.HasFocus);
Assert.False (cb.IsShow);
Assert.Equal (0, cb.SelectedItem);
Assert.Equal (-1, cb.SelectedItem);
Assert.Equal ("One", cb.Text);
Application.Top.FocusFirst (); // Gets focus again
Assert.True (cb.HasFocus);
Assert.False (cb.IsShow);
Assert.Equal (0, cb.SelectedItem);
Assert.Equal (-1, cb.SelectedItem);
Assert.Equal ("One", cb.Text);
Assert.True (cb.NewKeyDownEvent (new (KeyCode.U | KeyCode.CtrlMask)));
Assert.True (cb.HasFocus);
Assert.True (cb.IsShow);
Assert.Equal (0, cb.SelectedItem);
Assert.Equal (-1, cb.SelectedItem);
Assert.Equal ("", cb.Text);
Assert.Equal (3, cb.Source.Count);
}
Expand Down Expand Up @@ -256,7 +256,7 @@ public void Source_Equal_Null_Or_Count_Equal_Zero_Sets_SelectedItem_Equal_To_Min
Assert.Equal ("One", cb.Text);
cb.Text = "T";
Assert.True (cb.IsShow);
Assert.Equal (0, cb.SelectedItem);
Assert.Equal (-1, cb.SelectedItem);
Assert.Equal ("T", cb.Text);
Assert.True (cb.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.False (cb.IsShow);
Expand Down

0 comments on commit 3d829d7

Please sign in to comment.