diff --git a/crates/story/src/dropdown_story.rs b/crates/story/src/dropdown_story.rs index 1b2daa69..360b8435 100644 --- a/crates/story/src/dropdown_story.rs +++ b/crates/story/src/dropdown_story.rs @@ -61,8 +61,11 @@ impl DropdownStory { Country::new("Ecuador", "EC"), ]; - let country_dropdown = - cx.new_view(|cx| Dropdown::new("dropdown-country", countries, Some(6), cx)); + let country_dropdown = cx.new_view(|cx| { + Dropdown::new("dropdown-country", countries, Some(6), cx).on_change(|value, _cx| { + println!("Country changed: {:?}", value); + }) + }); let furits = vec![ "Apple", @@ -73,8 +76,11 @@ impl DropdownStory { "Watermelon", "Avocado", ]; - let furit_dropdown = - cx.new_view(|cx| Dropdown::string_list("dropdown-furits", furits, None, cx)); + let furit_dropdown = cx.new_view(|cx| { + Dropdown::string_list("dropdown-furits", furits, None, cx).on_change(|value, _cx| { + println!("Furit changed: {:?}", value); + }) + }); cx.new_view(|cx| Self { country_dropdown, diff --git a/crates/ui/src/dropdown.rs b/crates/ui/src/dropdown.rs index 3d35897a..e5efecf4 100644 --- a/crates/ui/src/dropdown.rs +++ b/crates/ui/src/dropdown.rs @@ -154,11 +154,16 @@ where self.selected_index = ix; if let Some(view) = self.dropdown.upgrade() { - cx.update_view(&view, |view, _| { - view.selected_value = self + cx.update_view(&view, |view, cx| { + let selected_value = self .selected_index .and_then(|ix| self.delegate.get(ix)) .map(|item| item.value().clone()); + + if let Some(on_change) = &view.on_change { + on_change(&selected_value, cx); + } + view.selected_value = selected_value; view.open = false; }); } @@ -179,6 +184,9 @@ pub struct Dropdown { placeholder: SharedString, title_prefix: Option, selected_value: Option<::Value>, + on_change: Option< + Box::Value>, &mut WindowContext) + 'static>, + >, } impl Dropdown @@ -208,6 +216,7 @@ where open: false, cleanable: true, title_prefix: None, + on_change: None, }; this.update_selected_value(cx); this @@ -240,6 +249,14 @@ where self } + pub fn on_change( + mut self, + on_change: impl Fn(&Option<::Value>, &mut WindowContext) + 'static, + ) -> Self { + self.on_change = Some(Box::new(on_change)); + self + } + pub fn set_selected_index( &mut self, selected_index: Option, diff --git a/crates/ui/src/radio.rs b/crates/ui/src/radio.rs index e780c832..dbfcb297 100644 --- a/crates/ui/src/radio.rs +++ b/crates/ui/src/radio.rs @@ -90,7 +90,7 @@ impl RenderOnce for Radio { self.on_click.filter(|_| !self.disabled), |this, on_click| { this.on_click(move |_event, cx| { - (on_click)(&!self.selected, cx); + on_click(&!self.selected, cx); }) }, )