Skip to content

Commit 78b0ea8

Browse files
authored
Merge pull request #171 from bash/empty-list
🐛 Correctly update the selection for empty lists
2 parents 8d68b4a + 0f419f1 commit 78b0ea8

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/selection_view.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ impl<'c> SelectionView<'c> {
3737
.iter()
3838
.filter(|emoji| search_text.is_empty() || emoji.contains(&pattern))
3939
.collect();
40-
if self.state.selected().unwrap() >= emojis.len() {
41-
// Reset the selection if the list goes shorter than the selected index.
42-
self.state.select(Some(0));
43-
}
40+
41+
self.state
42+
.select(adjust_selected(self.state.selected(), emojis.len()));
4443

4544
FilteredView {
4645
emojis,
@@ -50,6 +49,16 @@ impl<'c> SelectionView<'c> {
5049
}
5150
}
5251

52+
fn adjust_selected(selected: Option<usize>, list_len: usize) -> Option<usize> {
53+
match (selected, list_len) {
54+
(_, 0) => None,
55+
(None, _) => Some(0),
56+
// Reset the selection if the list goes shorter than the selected index.
57+
(Some(selected), _) if selected >= list_len => Some(0),
58+
(Some(_), _) => selected,
59+
}
60+
}
61+
5362
pub struct FilteredView<'s, 'c> {
5463
emojis: Vec<&'s Emoji>,
5564
state: &'s mut TableState,

0 commit comments

Comments
 (0)