use yew::prelude::*; pub struct Select { node_ref: NodeRef, } #[derive(yew::Properties, Clone, PartialEq, Debug)] pub struct SelectProps { pub children: ChildrenWithProps, pub on_selection_change: Callback>, } pub enum SelectMsg { OnSelectChange, } impl Select { fn get_nth_child_props(&self, ctx: &Context, nth: i32) -> Option { if nth == -1 { return None; } ctx.props() .children .iter() .nth(nth as usize) .map(|child| (*child.props).clone()) } fn send_selection_update(&self, ctx: &Context) { let select_node = self.node_ref.cast::().unwrap(); ctx.props() .on_selection_change .emit(self.get_nth_child_props(ctx, select_node.selected_index())) } } impl Component for Select { type Message = SelectMsg; type Properties = SelectProps; fn create(_: &Context) -> Self { Self { node_ref: NodeRef::default(), } } fn rendered(&mut self, ctx: &Context, _first_render: bool) { self.send_selection_update(ctx); } fn update(&mut self, ctx: &Context, _: Self::Message) -> bool { self.send_selection_update(ctx); false } fn view(&self, ctx: &Context) -> Html { html! { } } } #[derive(yew::Properties, Clone, PartialEq, Eq, Debug)] pub struct SelectOptionProps { pub value: String, pub text: String, } #[function_component(SelectOption)] pub fn select_option(props: &SelectOptionProps) -> Html { html! { } }