dropdown: Add on change callback
This commit is contained in:
parent
4ccbe9bd71
commit
07a1f3099e
3 changed files with 30 additions and 7 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<D: DropdownDelegate + 'static> {
|
|||
placeholder: SharedString,
|
||||
title_prefix: Option<SharedString>,
|
||||
selected_value: Option<<D::Item as DropdownItem>::Value>,
|
||||
on_change: Option<
|
||||
Box<dyn Fn(&Option<<D::Item as DropdownItem>::Value>, &mut WindowContext) + 'static>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl<D> Dropdown<D>
|
||||
|
|
@ -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<<D::Item as DropdownItem>::Value>, &mut WindowContext) + 'static,
|
||||
) -> Self {
|
||||
self.on_change = Some(Box::new(on_change));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_selected_index(
|
||||
&mut self,
|
||||
selected_index: Option<usize>,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
})
|
||||
},
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue