Skip to content

Commit dee5301

Browse files
committed
make the selection in the AutoSuggestBox wrap around when using the Up- and Down Arrow keys
1 parent fb1f11f commit dee5301

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/MaterialDesignThemes.Wpf/AutoSuggestBox.cs

Lines changed: 7 additions & 2 deletions
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

Lines changed: 63 additions & 1 deletion
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,67 @@ 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+
//var itemSource = (await suggestBox.GetSuggestions()).Cast<object>().ToList();
182+
183+
//var suggestions = await suggestionListBox.GetProperty<IEnumerable>(nameof(ListBox.ItemsSource));
184+
//int itemCount = suggestions.Cast<object>().Count();
185+
186+
int itemCount = CountNonGenericEnumerable(await suggestBox.GetSuggestions());
187+
188+
//Assert that initially the first item is selected
189+
int selectedIndex = await suggestionListBox.GetSelectedIndex();
190+
Assert.Equal(0, selectedIndex);
191+
await Task.Delay(delay);
192+
193+
//Assert that the last item is selected after pressing ArrowUp
194+
await suggestBox.SendInput(new KeyboardInput(Key.Up));
195+
Assert.Equal(itemCount - 1, await suggestionListBox.GetSelectedIndex());
196+
await Task.Delay(delay);
197+
198+
//Assert that the first item is selected after pressing ArrowDown
199+
await suggestBox.SendInput(new KeyboardInput(Key.Down));
200+
Assert.Equal(0, await suggestionListBox.GetSelectedIndex());
201+
202+
203+
}
204+
205+
internal static int CountNonGenericEnumerable(IEnumerable? source)
206+
{
207+
ArgumentNullException.ThrowIfNull(source);
208+
209+
if (source is ICollection collection)
210+
{
211+
return collection.Count;
212+
}
213+
214+
int count = 0;
215+
IEnumerator e = source.GetEnumerator();
216+
while (e.MoveNext())
217+
{
218+
count++;
219+
}
220+
221+
return count;
222+
}
223+
162224
private static async Task AssertExists(IVisualElement<ListBox> suggestionListBox, string text, bool existsOrNotCheck = true)
163225
{
164226
try

0 commit comments

Comments
 (0)