diff --git a/crates/story/src/list_story.rs b/crates/story/src/list_story.rs index 7bc83b7b..1aed55d3 100644 --- a/crates/story/src/list_story.rs +++ b/crates/story/src/list_story.rs @@ -136,7 +136,7 @@ impl RenderOnce for CompanyListItem { struct CompanyListDelegate { companies: Vec, matched_companies: Vec, - selected_index: usize, + selected_index: Option, confirmed_index: Option, query: String, loading: bool, @@ -168,14 +168,12 @@ impl ListDelegate for CompanyListDelegate { } fn set_selected_index(&mut self, ix: Option, cx: &mut ViewContext>) { - if let Some(ix) = ix { - self.selected_index = ix; - cx.notify(); - } + self.selected_index = ix; + cx.notify(); } fn render_item(&self, ix: usize, _cx: &mut ViewContext>) -> Option { - let selected = ix == self.selected_index || Some(ix) == self.confirmed_index; + let selected = Some(ix) == self.selected_index || Some(ix) == self.confirmed_index; if let Some(company) = self.matched_companies.get(ix) { return Some(CompanyListItem::new(ix, company.clone(), ix, selected)); } @@ -216,7 +214,11 @@ impl ListDelegate for CompanyListDelegate { impl CompanyListDelegate { fn selected_company(&self) -> Option { - self.companies.get(self.selected_index).cloned() + let Some(ix) = self.selected_index else { + return None; + }; + + self.companies.get(ix).cloned() } } @@ -254,7 +256,7 @@ impl ListStory { let delegate = CompanyListDelegate { matched_companies: companies.clone(), companies, - selected_index: 3, + selected_index: None, confirmed_index: None, query: "".to_string(), loading: false, @@ -262,9 +264,9 @@ impl ListStory { }; let company_list = cx.new_view(|cx| List::new(delegate, cx)); - company_list.update(cx, |list, cx| { - list.set_selected_index(Some(3), cx); - }); + // company_list.update(cx, |list, cx| { + // list.set_selected_index(Some(3), cx); + // }); let _subscriptions = vec![ cx.subscribe(&company_list, |_, _, ev: &ListEvent, _| match ev { diff --git a/crates/ui/src/list/list.rs b/crates/ui/src/list/list.rs index 56b49644..2cada52d 100644 --- a/crates/ui/src/list/list.rs +++ b/crates/ui/src/list/list.rs @@ -405,12 +405,19 @@ where return; } - let mut selected_index = self.selected_index.unwrap_or(0); - if selected_index < items_count - 1 { - selected_index = selected_index + 1; + let selected_index; + if let Some(ix) = self.selected_index { + if ix < items_count - 1 { + selected_index = ix + 1; + } else { + // When the last item is selected, select the first item. + selected_index = 0; + } } else { + // When no selected index, select the first item. selected_index = 0; } + self.select_item(selected_index, cx); }