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"),
|
Country::new("Ecuador", "EC"),
|
||||||
];
|
];
|
||||||
|
|
||||||
let country_dropdown =
|
let country_dropdown = cx.new_view(|cx| {
|
||||||
cx.new_view(|cx| Dropdown::new("dropdown-country", countries, Some(6), cx));
|
Dropdown::new("dropdown-country", countries, Some(6), cx).on_change(|value, _cx| {
|
||||||
|
println!("Country changed: {:?}", value);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
let furits = vec![
|
let furits = vec![
|
||||||
"Apple",
|
"Apple",
|
||||||
|
|
@ -73,8 +76,11 @@ impl DropdownStory {
|
||||||
"Watermelon",
|
"Watermelon",
|
||||||
"Avocado",
|
"Avocado",
|
||||||
];
|
];
|
||||||
let furit_dropdown =
|
let furit_dropdown = cx.new_view(|cx| {
|
||||||
cx.new_view(|cx| Dropdown::string_list("dropdown-furits", furits, None, cx));
|
Dropdown::string_list("dropdown-furits", furits, None, cx).on_change(|value, _cx| {
|
||||||
|
println!("Furit changed: {:?}", value);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
cx.new_view(|cx| Self {
|
cx.new_view(|cx| Self {
|
||||||
country_dropdown,
|
country_dropdown,
|
||||||
|
|
|
||||||
|
|
@ -154,11 +154,16 @@ where
|
||||||
self.selected_index = ix;
|
self.selected_index = ix;
|
||||||
|
|
||||||
if let Some(view) = self.dropdown.upgrade() {
|
if let Some(view) = self.dropdown.upgrade() {
|
||||||
cx.update_view(&view, |view, _| {
|
cx.update_view(&view, |view, cx| {
|
||||||
view.selected_value = self
|
let selected_value = self
|
||||||
.selected_index
|
.selected_index
|
||||||
.and_then(|ix| self.delegate.get(ix))
|
.and_then(|ix| self.delegate.get(ix))
|
||||||
.map(|item| item.value().clone());
|
.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;
|
view.open = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -179,6 +184,9 @@ pub struct Dropdown<D: DropdownDelegate + 'static> {
|
||||||
placeholder: SharedString,
|
placeholder: SharedString,
|
||||||
title_prefix: Option<SharedString>,
|
title_prefix: Option<SharedString>,
|
||||||
selected_value: Option<<D::Item as DropdownItem>::Value>,
|
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>
|
impl<D> Dropdown<D>
|
||||||
|
|
@ -208,6 +216,7 @@ where
|
||||||
open: false,
|
open: false,
|
||||||
cleanable: true,
|
cleanable: true,
|
||||||
title_prefix: None,
|
title_prefix: None,
|
||||||
|
on_change: None,
|
||||||
};
|
};
|
||||||
this.update_selected_value(cx);
|
this.update_selected_value(cx);
|
||||||
this
|
this
|
||||||
|
|
@ -240,6 +249,14 @@ where
|
||||||
self
|
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(
|
pub fn set_selected_index(
|
||||||
&mut self,
|
&mut self,
|
||||||
selected_index: Option<usize>,
|
selected_index: Option<usize>,
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@ impl RenderOnce for Radio {
|
||||||
self.on_click.filter(|_| !self.disabled),
|
self.on_click.filter(|_| !self.disabled),
|
||||||
|this, on_click| {
|
|this, on_click| {
|
||||||
this.on_click(move |_event, cx| {
|
this.on_click(move |_event, cx| {
|
||||||
(on_click)(&!self.selected, cx);
|
on_click(&!self.selected, cx);
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue