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
use yew::prelude::*;

use crate::Size;

#[derive(Clone, PartialEq, Eq, Ord, PartialOrd, Copy, Debug)]
pub enum Level {
    H1,
    H2,
    H3,
    H4,
    H5,
    H6,
}

impl Default for Level {
    fn default() -> Self {
        Level::H1
    }
}

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct Props {
    #[prop_or_default]
    pub children: Children,
    #[prop_or_default]
    pub level: Level,
    #[prop_or_default]
    pub size: Option<Size>,
}

pub struct Title {
    props: Props,
}

impl Component for Title {
    type Message = ();
    type Properties = Props;

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

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

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        if self.props != props {
            self.props = props;
            true
        } else {
            false
        }
    }

    fn view(&self) -> Html {
        let mut classes = Classes::from("pf-c-title");

        if let Some(size) = self.props.size {
            classes.push(size.as_class());
        }

        match self.props.level {
            Level::H1 => html! {<h1 class=classes>{ for self.props.children.iter() }</h1>},
            Level::H2 => html! {<h2 class=classes>{ for self.props.children.iter() }</h2>},
            Level::H3 => html! {<h3 class=classes>{ for self.props.children.iter() }</h3>},
            Level::H4 => html! {<h4 class=classes>{ for self.props.children.iter() }</h4>},
            Level::H5 => html! {<h5 class=classes>{ for self.props.children.iter() }</h5>},
            Level::H6 => html! {<h6 class=classes>{ for self.props.children.iter() }</h6>},
        }
    }
}