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
79
80
81
82
83
84
85
86
87
use std::borrow::Cow;
use std::fmt;
use yew::prelude::*;

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

impl ActionType {
    fn to_cow_string(&self) -> Cow<'static, str> {
        let s = match self {
            ActionType::Primary => "primaryAction",
            ActionType::Secondary => "secondaryAction",
        };
        Cow::from(s)
    }
}

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

/// Props for [`MatDialogAction`]
#[derive(Properties, Clone)]
pub struct ActionProps {
    pub action_type: ActionType,
    #[prop_or_default]
    pub action: Option<Cow<'static, str>>,
    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 {
    props: ActionProps,
}

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

    fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self {
        Self { props }
    }

    fn update(&mut self, _msg: Self::Message) -> bool {
        false
    }

    fn change(&mut self, props: Self::Properties) -> bool {
        self.props = props;
        true
    }

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

        html! {
            { children }
        }
    }
}