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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! List
use crate::icon::Icon;
use crate::prelude::{AsClasses, ExtendClasses};
use crate::utils::Raw;
use yew::html::ChildrenRenderer;
use yew::virtual_dom::VChild;
use yew::{html::IntoPropValue, prelude::*, virtual_dom::AttrValue};

#[derive(Copy, Clone, PartialEq, Eq)]
pub enum ListType {
    Basic,
    Inline,
    Ordered(ListOrder),
    Plain,
    Bordered,
}

impl AsClasses for ListType {
    fn extend_classes(&self, classes: &mut Classes) {
        match self {
            ListType::Inline => {
                classes.push(classes!("pf-m-inline"));
            }
            ListType::Plain => {
                classes.push(classes!("pf-m-plain"));
            }
            ListType::Bordered => {
                classes.push(classes!("pf-m-plain", "pf-m-bordered"));
            }
            _ => {}
        }
    }
}

impl Default for ListType {
    fn default() -> Self {
        Self::Basic
    }
}

#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub enum ListOrder {
    #[default]
    Number,
    LowercaseLetter,
    UppercaseLetter,
    LowercaseRomanNumber,
    UppercaseRomanNumber,
}

impl IntoPropValue<Option<AttrValue>> for ListOrder {
    fn into_prop_value(self) -> Option<AttrValue> {
        Some(AttrValue::Static(match self {
            Self::Number => "1",
            Self::LowercaseLetter => "a",
            Self::UppercaseLetter => "A",
            Self::LowercaseRomanNumber => "i",
            Self::UppercaseRomanNumber => "I",
        }))
    }
}

/// Properties for [`List`]
#[derive(PartialEq, Properties)]
pub struct ListProperties {
    #[prop_or_default]
    pub children: ChildrenRenderer<ListChildVariant>,
    #[prop_or_default]
    pub r#type: ListType,
    #[prop_or_default]
    pub icon_size: ListIconSize,
}

#[derive(Clone, Copy, Default, Eq, PartialEq, Debug)]
pub enum ListIconSize {
    #[default]
    Default,
    Large,
}

impl AsClasses for ListIconSize {
    fn extend_classes(&self, classes: &mut Classes) {
        match self {
            Self::Default => {}
            Self::Large => classes.extend(classes!("pf-m-icon-lg")),
        }
    }
}

/// List component
///
/// > A **list** component embeds a formatted list (bulleted or numbered list) into page content.
///
/// See: <https://www.patternfly.org/components/list>
///
/// ## Properties
///
/// Defined by [`ListProperties`].
///
/// ## Children
///
/// Requires to use [`ListItem`] as children.
///
/// ## Example
///
/// ```rust
/// use yew::prelude::*;
/// use patternfly_yew::prelude::*;
///
/// #[function_component(Example)]
/// fn example() -> Html {
///   html!(
///     <List>
///       <ListItem>{"Foo"}</ListItem>
///       <ListItem>{"Bar"}</ListItem>
///       // you can also inject a "raw" item, just be sure to add the `li` or `ListItem` element.
///       <Raw>
///         <li>{"Baz"}</li>
///       </Raw>
///     </List>
///   )
/// }
/// ```
#[function_component(List)]
pub fn list(props: &ListProperties) -> Html {
    let mut classes = Classes::from("pf-v5-c-list");

    classes.extend_from(&props.r#type);
    classes.extend_from(&props.icon_size);

    let l = |items| match props.r#type {
        ListType::Basic | ListType::Inline | ListType::Plain | ListType::Bordered => {
            html! (<ul class={classes} role="list">{ items }</ul>)
        }
        ListType::Ordered(n) => {
            html! (<ol type={n} class={classes} role="list">{ items }</ol>)
        }
    };

    l(html! ({
         for props.children.clone()
    }))
}

#[derive(PartialEq, Properties)]
pub struct ListItemProperties {
    #[prop_or_default]
    pub children: Html,
    #[prop_or_default]
    pub icon: Option<Icon>,
}

#[function_component(ListItem)]
pub fn list_item(props: &ListItemProperties) -> Html {
    match props.icon {
        Some(icon) => {
            let class = classes!("pf-v5-c-list__item");
            html!(
                <li {class}>
                    <span class={classes!("pf-v5-c-list__item-icon")}>
                        { icon }
                    </span>
                    <span class={classes!("pf-v5-c-list__item-text")}>
                        { props.children.clone() }
                    </span>
                </li>
            )
        }
        None => html!( <li> { props.children.clone() } </li> ),
    }
}

#[derive(Clone, PartialEq)]
pub enum ListChildVariant {
    Item(VChild<ListItem>),
    Raw(VChild<Raw>),
}

impl From<VChild<ListItem>> for ListChildVariant {
    fn from(value: VChild<ListItem>) -> Self {
        Self::Item(value)
    }
}

impl From<VChild<Raw>> for ListChildVariant {
    fn from(value: VChild<Raw>) -> Self {
        Self::Raw(value)
    }
}

impl From<ListChildVariant> for Html {
    fn from(value: ListChildVariant) -> Self {
        match value {
            ListChildVariant::Item(child) => child.into(),
            ListChildVariant::Raw(child) => child.into(),
        }
    }
}