Skip to content

Commit e3c1240

Browse files
authored
Wrap selection in the AutoSuggestBox (#3817)
* make the selection in the AutoSuggestBox wrap around when using the Up- and Down Arrow keys * get the Suggestion count of the AutoSuggestBox via RemoteExecute
1 parent 9d09976 commit e3c1240

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/MaterialDesignThemes.Wpf/AutoSuggestBox.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ private void DecrementSelection()
268268
if (_autoSuggestBoxList is null || Suggestions is null)
269269
return;
270270
ICollectionView collectionView = CollectionViewSource.GetDefaultView(Suggestions);
271-
if (collectionView.IsCurrentBeforeFirst)
271+
272+
// If we're at the first item, wrap around to the last.
273+
if (collectionView.CurrentPosition == 0)
272274
collectionView.MoveCurrentToLast();
273275
else
274276
collectionView.MoveCurrentToPrevious();
@@ -280,7 +282,10 @@ private void IncrementSelection()
280282
if (_autoSuggestBoxList is null || Suggestions is null)
281283
return;
282284
ICollectionView collectionView = CollectionViewSource.GetDefaultView(Suggestions);
283-
if (collectionView.IsCurrentAfterLast)
285+
int itemCount = collectionView.Cast<object>().Count();
286+
287+
// If we're at the last item, wrap around to the first.
288+
if (collectionView.CurrentPosition == itemCount - 1)
284289
collectionView.MoveCurrentToFirst();
285290
else
286291
collectionView.MoveCurrentToNext();

tests/MaterialDesignThemes.UITests/WPF/AutoSuggestBoxes/AutoSuggestTextBoxTests.cs

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel;
1+
using System.Collections;
2+
using System.ComponentModel;
23
using MaterialDesignThemes.UITests.Samples.AutoSuggestBoxes;
34
using MaterialDesignThemes.UITests.Samples.AutoSuggestTextBoxes;
45
using Xunit.Sdk;
@@ -159,6 +160,47 @@ public async Task AutoSuggestBox_MovesFocusToNextElement_WhenPopupIsClosed()
159160
recorder.Success();
160161
}
161162

163+
[Fact]
164+
[Description("Issue 3815")]
165+
public async Task AutoSuggestBox_KeysUpAndDown_WrapAround()
166+
{
167+
await using var recorder = new TestRecorder(App);
168+
169+
//Arrange
170+
IVisualElement<AutoSuggestBox> suggestBox = (await LoadUserControl<AutoSuggestTextBoxWithTemplate>()).As<AutoSuggestBox>();
171+
IVisualElement<Popup> popup = await suggestBox.GetElement<Popup>();
172+
IVisualElement<ListBox> suggestionListBox = await popup.GetElement<ListBox>();
173+
174+
const int delay = 50;
175+
176+
//Act & Assert
177+
await suggestBox.MoveKeyboardFocus();
178+
await suggestBox.SendInput(new KeyboardInput("e"));
179+
await Task.Delay(delay);
180+
181+
static int? GetSuggestionCount(AutoSuggestBox autoSuggestBox)
182+
{
183+
int? count = autoSuggestBox.Suggestions?.OfType<object>().Count();
184+
return count;
185+
}
186+
187+
int itemCount = await suggestBox.RemoteExecute(GetSuggestionCount) ?? 0;
188+
189+
//Assert that initially the first item is selected
190+
int selectedIndex = await suggestionListBox.GetSelectedIndex();
191+
Assert.Equal(0, selectedIndex);
192+
await Task.Delay(delay);
193+
194+
//Assert that the last item is selected after pressing ArrowUp
195+
await suggestBox.SendInput(new KeyboardInput(Key.Up));
196+
Assert.Equal(itemCount - 1, await suggestionListBox.GetSelectedIndex());
197+
await Task.Delay(delay);
198+
199+
//Assert that the first item is selected after pressing ArrowDown
200+
await suggestBox.SendInput(new KeyboardInput(Key.Down));
201+
Assert.Equal(0, await suggestionListBox.GetSelectedIndex());
202+
}
203+
162204
private static async Task AssertExists(IVisualElement<ListBox> suggestionListBox, string text, bool existsOrNotCheck = true)
163205
{
164206
try

0 commit comments

Comments
 (0)