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
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::prelude::{AsClasses, Inset};
use std::fmt::Debug;
use yew::prelude::*;
use yew_nested_router::{components::Link, prelude::*};

use super::TabTitle;

// tab router

/// Properties for [`TabsRouter`]
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct TabsRouterProperties<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: &TabsRouterProperties<T>) -> Html
where
    T: Target,
{
    let mut classes = classes!("pf-v5-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_classes(&mut classes);
    }

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

// tab router item

/// Properties for [`TabRouterItem`]
#[derive(Properties, Clone, PartialEq)]
pub struct TabRouterItemProperties<T>
where
    T: Target,
{
    /// The tab title
    pub title: TabTitle,
    /// The switch this item references to
    pub to: T,
    /// If tab is disabled
    #[prop_or(false)]
    pub disabled: bool,
}

#[function_component(TabRouterItem)]
pub fn tab_router_item<T>(props: &TabRouterItemProperties<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-v5-c-tabs__item");

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

    let mut link_classes = Classes::from("pf-v5-c-tabs__link");

    if props.disabled {
        link_classes.push("pf-m-disabled");
    }

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