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
88
89
90
91
92
use crate::{AsClasses, Inset};
use std::fmt::Debug;
use yew::prelude::*;
use yew_nested_router::{components::Link, prelude::*};

// tab router

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct Props<T>
where
    T: Target + 'static,
{
    #[prop_or_default]
    pub r#box: bool,
    #[prop_or_default]
    pub vertical: bool,
    #[prop_or_default]
    pub filled: bool,

    #[prop_or_default]
    pub inset: Option<Inset>,

    #[prop_or_default]
    pub children: ChildrenWithProps<TabRouterItem<T>>,
}

#[function_component(TabsRouter)]
pub fn tabs_router<T>(props: &Props<T>) -> Html
where
    T: Target,
{
    let mut classes = Classes::from("pf-c-tabs");

    if props.r#box {
        classes.push("pf-m-box");
    }

    if props.vertical {
        classes.push("pf-m-vertical");
    }

    if props.filled {
        classes.push("pf-m-fill");
    }

    if let Some(inset) = &props.inset {
        inset.extend(&mut classes);
    }

    html! {
        <div class={classes}>
            <ul class="pf-c-tabs__list">
                { for props.children.iter() }
            </ul>
        </div>
    }
}

// tab router item

#[derive(Properties, Clone, Debug, PartialEq)]
pub struct TabRouterItemProps<T>
where
    T: Target,
{
    /// The tab label
    pub label: String,
    /// The switch this item references to
    pub to: T,
}

#[function_component(TabRouterItem)]
pub fn tab_router_item<T>(props: &TabRouterItemProps<T>) -> Html
where
    T: Target,
{
    let router = use_router::<T>().expect("Must be used below a Router or Nested component");

    let mut classes = Classes::from("pf-c-tabs__item");

    if router.is_same(&props.to) {
        classes.push("pf-m-current");
    }

    html! {
        <li class={classes}>
            <Link<T> element="button" class="pf-c-tabs__link" target={props.to.clone()}>
                <span class="pf-c-tabs__item-text"> { &props.label } </span>
            </Link<T>>
        </li>
    }
}