Skip to content

Commit c1c4bdf

Browse files
authored
Merge pull request #7963 from AvaloniaUI/disabled-items-should-not-be-selectable-with-keyboard
Disabled items in ComboBox souldn't be selectable with arrow keys.
2 parents 3dc67c2 + 859e883 commit c1c4bdf

File tree

4 files changed

+80
-30
lines changed

4 files changed

+80
-30
lines changed

src/Avalonia.Controls/ComboBox.cs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -453,42 +453,18 @@ private void SelectFocusedItem()
453453

454454
private void SelectNext()
455455
{
456-
int next = SelectedIndex + 1;
457-
458-
if (next >= ItemCount)
456+
if (ItemCount >= 1)
459457
{
460-
if (WrapSelection == true)
461-
{
462-
next = 0;
463-
}
464-
else
465-
{
466-
return;
467-
}
458+
MoveSelection(NavigationDirection.Next, WrapSelection);
468459
}
469-
470-
471-
472-
SelectedIndex = next;
473460
}
474461

475462
private void SelectPrev()
476463
{
477-
int prev = SelectedIndex - 1;
478-
479-
if (prev < 0)
464+
if (ItemCount >= 1)
480465
{
481-
if (WrapSelection == true)
482-
{
483-
prev = ItemCount - 1;
484-
}
485-
else
486-
{
487-
return;
488-
}
466+
MoveSelection(NavigationDirection.Previous, WrapSelection);
489467
}
490-
491-
SelectedIndex = prev;
492468
}
493469
}
494470
}

src/Avalonia.Controls/ItemsControl.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,6 @@ private void UpdatePseudoClasses(int itemCount)
508508
do
509509
{
510510
result = container.GetControl(direction, c, wrap);
511-
from = from ?? result;
512511

513512
if (result != null &&
514513
result.Focusable &&

src/Avalonia.Controls/StackPanel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public Orientation Orientation
123123
index = Children.Count - 1;
124124
break;
125125
case NavigationDirection.Next:
126-
if (index != -1) ++index;
126+
++index;
127127
break;
128128
case NavigationDirection.Previous:
129129
if (index != -1) --index;

tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,81 @@ public void Clicking_On_Control_Toggles_IsDropDownOpen()
3636
Assert.False(target.IsDropDownOpen);
3737
}
3838

39+
[Fact]
40+
public void WrapSelection_Should_Work()
41+
{
42+
using (UnitTestApplication.Start(TestServices.RealFocus))
43+
{
44+
var items = new[]
45+
{
46+
new ComboBoxItem() { Content = "bla" },
47+
new ComboBoxItem() { Content = "dd" },
48+
new ComboBoxItem() { Content = "sdf", IsEnabled = false }
49+
};
50+
var target = new ComboBox
51+
{
52+
Items = items,
53+
Template = GetTemplate(),
54+
WrapSelection = true
55+
};
56+
var root = new TestRoot(target);
57+
target.ApplyTemplate();
58+
target.Presenter.ApplyTemplate();
59+
target.Focus();
60+
Assert.Equal(target.SelectedIndex, -1);
61+
Assert.True(target.IsFocused);
62+
target.RaiseEvent(new KeyEventArgs
63+
{
64+
RoutedEvent = InputElement.KeyDownEvent,
65+
Key = Key.Up,
66+
});
67+
Assert.Equal(target.SelectedIndex, 1);
68+
target.RaiseEvent(new KeyEventArgs
69+
{
70+
RoutedEvent = InputElement.KeyDownEvent,
71+
Key = Key.Down,
72+
});
73+
Assert.Equal(target.SelectedIndex, 0);
74+
}
75+
}
76+
77+
[Fact]
78+
public void Focuses_Next_Item_On_Key_Down()
79+
{
80+
using (UnitTestApplication.Start(TestServices.RealFocus))
81+
{
82+
var items = new[]
83+
{
84+
new ComboBoxItem() { Content = "bla" },
85+
new ComboBoxItem() { Content = "dd", IsEnabled = false },
86+
new ComboBoxItem() { Content = "sdf" }
87+
};
88+
var target = new ComboBox
89+
{
90+
Items = items,
91+
Template = GetTemplate()
92+
};
93+
var root = new TestRoot(target);
94+
target.ApplyTemplate();
95+
target.Presenter.ApplyTemplate();
96+
target.Focus();
97+
Assert.Equal(target.SelectedIndex, -1);
98+
Assert.True(target.IsFocused);
99+
target.RaiseEvent(new KeyEventArgs
100+
{
101+
RoutedEvent = InputElement.KeyDownEvent,
102+
Key = Key.Down,
103+
});
104+
Assert.Equal(target.SelectedIndex, 0);
105+
target.RaiseEvent(new KeyEventArgs
106+
{
107+
RoutedEvent = InputElement.KeyDownEvent,
108+
Key = Key.Down,
109+
});
110+
Assert.Equal(target.SelectedIndex, 2);
111+
}
112+
}
113+
39114
[Fact]
40115
public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
41116
{

0 commit comments

Comments
 (0)