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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
mod variant;

pub use variant::*;

use crate::prelude::{Button, ButtonType, ButtonVariant, Divider, DividerType, Icon};
use yew::html::ChildrenRenderer;
use yew::prelude::*;

/// Properties for [`Card`]
#[derive(Clone, PartialEq, Properties)]
pub struct CardProperties {
    #[prop_or_default]
    pub id: AttrValue,
    #[prop_or_default]
    pub children: ChildrenRenderer<CardBodyVariant>,
    #[prop_or_default]
    pub title: Option<Html>,
    #[prop_or_default]
    pub footer: Option<Html>,
    #[prop_or_default]
    pub compact: bool,
    #[prop_or_default]
    pub disabled: bool,
    #[prop_or_default]
    pub flat: bool,
    #[prop_or_default]
    pub onclick: Callback<MouseEvent>,
    #[prop_or_default]
    pub expandable: bool,
    #[prop_or_default]
    pub large: bool,
    #[prop_or_default]
    pub full_height: bool,
    #[prop_or_default]
    pub rounded: bool,
    #[prop_or_default]
    pub selectable: bool,
    #[prop_or_default]
    pub selected: bool,
    #[prop_or_default]
    pub style: Option<AttrValue>,
    #[prop_or_default]
    pub additional_class: Classes,
    #[prop_or_default]
    pub plain: bool,
    #[prop_or_default]
    pub actions: Option<Html>,
}

/// Card component
///
/// > A **card** is a square or rectangular container that can contain any kind of content. Cards symbolize units of information, and each one acts as an entry point for users to access more details. For example, in dashboards and catalog views, cards function as a preview of a detailed page. Cards may also be used in data displays like card views, or for positioning content on a page.
///
/// See: <https://www.patternfly.org/components/card>
///
/// ## Properties
///
/// Defined by [`CardProperties`].
///
/// ## Children
///
/// Cards can have any number of [`CardBody`] or [`CardDivider`] children.
///
/// ## Example
///
/// ```
/// use yew::prelude::*;
/// use patternfly_yew::prelude::*;
///
/// #[function_component(Example)]
/// fn example() -> Html {
///   let title = html!({"The heading"});
///   let footer = html!({"The footer"});
///
///   html!(
///     <Card
///         {title} {footer}
///     >
///       <CardBody>
///         { "Foo" }
///       </CardBody>
///       <CardDivider/>
///       <CardBody>
///         { "Bar" }
///       </CardBody>
///     </Card>
///   )
/// }
/// ```
#[function_component(Card)]
pub fn card(props: &CardProperties) -> Html {
    let expanded = use_state_eq(|| !props.expandable);

    let mut class = classes!("pf-v5-c-card");

    if props.compact {
        class.push("pf-m-compact");
    }

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

    if props.expandable && *expanded {
        class.push("pf-m-expanded");
    }

    if props.large {
        class.push("pf-m-display-lg");
    }

    if props.flat {
        class.push("pf-m-flat");
    }

    if props.selectable {
        class.push("pf-m-selectable")
    }

    if props.selected {
        class.push("pf-m-selected")
    }

    if props.full_height {
        class.push("pf-m-full-height");
    }

    if props.rounded {
        class.push("pf-m-rounded");
    }

    if props.plain {
        class.push("pf-m-plain");
    }

    class.extend(props.additional_class.clone());

    html! (
        <div
            {class}
            onclick={props.onclick.clone()}
            id={&props.id}
            style={&props.style}
        >
            { header(props, expanded.clone()) }

            if *expanded {
                { for props.children.iter() }

                if let Some(content) = &props.footer {
                    <div class="pf-v5-c-card__footer">
                        { content.clone() }
                    </div>
                }
            }
        </div>
    )
}

fn header(props: &CardProperties, expanded: UseStateHandle<bool>) -> Html {
    let (card_title, title_id) = if props.title.is_some() {
        let id = format!("{}-title", props.id);
        (
            Some(html!(<CardTitle id={ id.clone() }  content={ props.title.clone() } />)),
            Some(id),
        )
    } else {
        (None, None)
    };

    let header_toggle = props.expandable.then_some({
        let id = format!("{}-toggle", props.id);
        let mut aria_labelledby = id.clone();
        if let Some(title_id) = title_id {
            aria_labelledby = format!("{} {}", title_id, aria_labelledby);
        }

        let onclick = {
            Callback::from(move |_: MouseEvent| {
                expanded.set(!*expanded);
            })
        };

        html!(
            <div class="pf-v5-c-card__header-toggle">
                <Button
                    id={ id.clone() }
                    r#type={ ButtonType::Button }
                    variant={ ButtonVariant::Plain }
                    aria_label="Details"
                    { aria_labelledby }
                    { onclick }
                >
                    <span class="pf-v5-c-card__header-toggle-icon">
                      { Icon::AngleRight }
                    </span>
                </Button>
            </div>
        )
    });

    let selector_check = props.selectable.then_some({
        let id = format!("{}-check", props.id);
        let mut class = classes!("pf-v5-c-check__label");
        if props.disabled {
            class.push("pf-m-disabled");
        }

        html!(
            <div class="pf-v5-c-card__selectable-actions">
                <div class="pf-v5-c-check pf-m-standalone">
                    <input
                        class="pf-v5-c-check__input"
                        type="checkbox"
                        id={ id.clone() }
                        name={ id.clone() }
                        aria_labelledby={&props.id}
                        checked={ props.selected }
                        disabled={ props.disabled }
                    />
                    <label
                        id={ format!("{}-label", id) }
                        { class }
                        for={ id }
                    />
                </div>
            </div>
        )
    });

    let actions = match (selector_check, props.actions.clone()) {
        (None, None) => None,
        (a, b) => Some(html! (
            <div class="pf-v5-c-card__actions pf-m-no-offset">
                {a} {b}
            </div>
        )),
    };

    html!(
        if header_toggle.is_some() || actions.is_some() {
            <div class="pf-v5-c-card__header">
                { header_toggle }
                { actions }
                <div class="pf-v5-c-card__header-main">
                    { card_title }
                </div>
            </div>
        } else {
            { card_title }
        }
    )
}

#[derive(PartialEq, Properties)]
struct OptionalContentProperties {
    content: Option<Html>,

    #[prop_or_default]
    id: AttrValue,
}

#[function_component(CardTitle)]
fn card_title(props: &OptionalContentProperties) -> Html {
    html!(
        if let Some(content) = &props.content {
            <div id={ props.id .clone() } class="pf-v5-c-card__title">
                { content.clone() }
            </div>
        }
    )
}

#[derive(Clone, PartialEq, Properties)]
pub struct CardBodyProperties {
    #[prop_or_default]
    pub children: Html,
    #[prop_or_default]
    pub style: Option<AttrValue>,
    #[prop_or_default]
    pub additional_class: Classes,
}

#[function_component(CardBody)]
pub fn card_body(props: &CardBodyProperties) -> Html {
    let mut class = classes!("pf-v5-c-card__body");

    class.extend(props.additional_class.clone());

    html!(
        <div {class} style={&props.style}>
            { props.children.clone() }
        </div>
    )
}

/// Specialized card divider component
///
/// This component is normally used as part of a list of card bodies.
///
/// ## Properties
///
/// This component does not have properties.
#[function_component(CardDivider)]
pub fn card_divider() -> Html {
    html!(<Divider r#type={DividerType::Hr} />)
}