1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::fmt;
use yew::prelude::*;
use yew::virtual_dom::AttrValue;

/// Dialog action type.
#[derive(Clone, PartialEq)]
pub enum ActionType {
    /// Binds `to slot` of `primaryAction`
    Primary,
    /// Binds `to slot` of `secondaryAction`
    Secondary,
}

impl ActionType {
    fn as_str(&self) -> &'static str {
        match self {
            ActionType::Primary => "primaryAction",
            ActionType::Secondary => "secondaryAction",
        }
    }
}

impl fmt::Display for ActionType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Props for [`MatDialogAction`]
#[derive(Properties, PartialEq, Clone)]
pub struct ActionProps {
    pub action_type: ActionType,
    #[prop_or_default]
    pub action: Option<AttrValue>,
    pub children: Children,
}

/// Defines actions for [`MatDialog`][crate::MatDialog].
///
/// If the child passed is an element (a `VTag`), then it is modified to include
/// the appropriate attributes. Otherwise, the child is wrapped in a `span`
/// containing said attributes.
pub struct MatDialogAction {}

impl Component for MatDialogAction {
    type Message = ();
    type Properties = ActionProps;

    fn create(_: &Context<Self>) -> Self {
        Self {}
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let props = ctx.props();
        let children = props
            .children
            .iter()
            .map(|child| match child {
                Html::VTag(mut vtag) => {
                    vtag.add_attribute("slot", props.action_type.to_string());
                    if let Some(action) = props.action.as_ref() {
                        vtag.add_attribute("dialogAction", action.to_owned());
                    }
                    Html::VTag(vtag)
                }
                _ => html! {
                     <span slot={props.action_type.to_string()} dialogAction={props.action.clone()}>
                         {child}
                     </span>
                },
            })
            .collect::<Html>();

        html! {
             {children}
        }
    }
}