From a5e5fc52b48a9b3d2ef8d470654901a283026a31 Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Wed, 30 Apr 2025 10:33:21 +0800 Subject: [PATCH] dropdown: Support change options for the dropdown (#824) --- crates/story/src/dropdown_story.rs | 17 ++++++++++------- crates/ui/src/dropdown.rs | 10 ++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/crates/story/src/dropdown_story.rs b/crates/story/src/dropdown_story.rs index ded9900f..c14062df 100644 --- a/crates/story/src/dropdown_story.rs +++ b/crates/story/src/dropdown_story.rs @@ -138,21 +138,24 @@ impl DropdownStory { .title_prefix("UI: ") }), simple_dropdown2: cx.new(|cx| { - Dropdown::new( - "string-list2", + let mut dropdown = + Dropdown::new("string-list2", SearchableVec::new(vec![]), None, window, cx) + .small() + .placeholder("Language") + .title_prefix("Language: "); + + dropdown.set_items( SearchableVec::new(vec![ "Rust".into(), "Go".into(), "C++".into(), "JavaScript".into(), ]), - None, window, cx, - ) - .small() - .placeholder("Language") - .title_prefix("Language: ") + ); + + dropdown }), simple_dropdown3: cx.new(|cx| { Dropdown::new("string-list3", Vec::::new(), None, window, cx) diff --git a/crates/ui/src/dropdown.rs b/crates/ui/src/dropdown.rs index 1066549f..c9b7b661 100644 --- a/crates/ui/src/dropdown.rs +++ b/crates/ui/src/dropdown.rs @@ -600,6 +600,16 @@ where }) .child(title) } + + /// Set the items for the dropdown. + pub fn set_items(&mut self, items: D, _: &mut Window, cx: &mut Context) + where + D: DropdownDelegate + 'static, + { + self.list.update(cx, |list, _| { + list.delegate_mut().delegate = items; + }); + } } impl Sizable for Dropdown