material_dioxus/list/
radio_list_item.rs

1use crate::{list::request_selected::make_request_selected_listener, StaticCallback};
2use dioxus::prelude::*;
3use wasm_bindgen::prelude::*;
4
5use super::{GraphicType, RequestSelectedDetail};
6
7#[wasm_bindgen(module = "/build/mwc-radio-list-item.js")]
8extern "C" {
9    #[derive(Debug)]
10    type RadioListItem;
11
12    #[wasm_bindgen(getter, static_method_of = RadioListItem)]
13    fn _dummy_loader() -> JsValue;
14
15    #[wasm_bindgen(method, setter)]
16    fn set_selected(this: &RadioListItem, value: bool);
17}
18
19loader_hack!(RadioListItem);
20
21/// Props for [`MatRadioListItem`]
22///
23/// MWC Documentation [properties](https://github.com/material-components/material-components-web-components/tree/v0.27.0/packages/list#mwc-radio-list-item-1)
24/// and [events](https://github.com/material-components/material-components-web-components/tree/v0.27.0/packages/list#mwc-radio-list-item-2)
25#[derive(Props)]
26pub struct RadioListItemProps<'a> {
27    #[props(default)]
28    pub left: bool,
29    #[props(into)]
30    pub group: Option<String>,
31    #[props(default = GraphicType::Control)]
32    pub graphic: GraphicType,
33    /// Binds to `request-selected` event on `mwc-list-item`.
34    #[props(into)]
35    pub _on_request_selected: Option<StaticCallback<RequestSelectedDetail>>,
36    #[props(default)]
37    pub initially_selected: bool,
38    pub children: Element<'a>,
39
40    #[props(into, default)]
41    pub style: String,
42    #[props(into, default)]
43    pub class: String,
44}
45
46fn render<'a>(cx: Scope<'a, RadioListItemProps<'a>>) -> Element<'a> {
47    let id = crate::use_id(cx, "radio-list-item");
48    let request_selected_listener = cx.use_hook(|| None);
49    if let Some(elem) = crate::get_elem_by_id(id) {
50        let target = elem;
51        if let Some(listener) = cx.props._on_request_selected.clone() {
52            *request_selected_listener = Some(make_request_selected_listener(&target, listener));
53        }
54    }
55
56    render! {
57        mwc-radio-list-item {
58            onmounted: move |_| {
59                if let Some(elem) = crate::get_elem_by_id(id) {
60                    let item = JsValue::from(elem).dyn_into::<RadioListItem>().unwrap();
61                    item.set_selected(cx.props.initially_selected);
62                }
63            },
64
65            id: id,
66
67            left: bool_attr!(cx.props.left),
68            graphic: cx.props.graphic.as_str(),
69            group: optional_string_attr!(cx.props.group),
70
71            style: string_attr!(cx.props.style),
72            class: string_attr!(cx.props.class),
73
74            &cx.props.children
75        }
76    }
77}
78
79component!('a, MatRadioListItem, RadioListItemProps, render, RadioListItem, "radio-list-item");