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
use crate::prelude::{
    AsClasses, ExtendClasses, Icon, Order, TableHeaderContext, TableHeaderSortBy, TextModifier,
};
use std::fmt::Debug;
use yew::prelude::*;

/// Properties for [`TableColumn`]
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct TableColumnProperties<C>
where
    C: Clone + Eq + 'static,
{
    /// The column (id) of the column
    pub index: C,
    #[prop_or_default]
    pub label: Option<String>,
    #[prop_or_default]
    pub center: bool,
    #[prop_or_default]
    pub width: ColumnWidth,
    #[prop_or_default]
    pub text_modifier: Option<TextModifier>,
    #[prop_or_default]
    pub expandable: bool,

    #[doc(hidden)]
    #[prop_or_default]
    pub(crate) first_tree_column: bool,

    // Current sortby status
    #[prop_or_default]
    pub sortby: Option<TableHeaderSortBy<C>>,

    #[prop_or_default]
    pub onsort: Option<Callback<TableHeaderSortBy<C>>>,
}

#[derive(Copy, Clone, Default, Eq, PartialEq, Debug)]
pub enum ColumnWidth {
    #[default]
    Default,
    /// Percentage modifier
    ///
    /// From 10 to 90, rounded to the nearest ten. Values outside of the limit will be replaced
    /// with the limit.
    Percent(u16),
    /// Maximize width
    WidthMax,
    /// Minimize with, without triggering text wrapping
    FitContent,
}

fn round(p: u16) -> u16 {
    if p <= 10 {
        return 10;
    }
    if p >= 90 {
        return 90;
    }

    // round to the nearest ten
    ((p + 5) / 10) * 10
}

impl AsClasses for ColumnWidth {
    fn extend_classes(&self, classes: &mut Classes) {
        match self {
            Self::Default => {}
            Self::Percent(p) => classes.push(classes!(format!("pf-m-width-{}", round(*p)))),
            Self::WidthMax => classes.push(classes!("pf-m-width-max")),
            Self::FitContent => classes.push(classes!("pf-m-fit-content")),
        }
    }
}

/// The Table Column component.
///
/// ## Properties
///
/// Define by [`TableColumnProperties`].
#[function_component(TableColumn)]
pub fn table_column<K>(props: &TableColumnProperties<K>) -> Html
where
    K: Clone + Eq + 'static,
{
    let table_header_context = use_context::<TableHeaderContext<K>>();

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

    if props.first_tree_column {
        class.push(classes!("pf-v5-c-table__tree-view-title-header-cell"));
    }

    if props.center {
        class.push(classes!("pf-m-center"));
    }

    if props.onsort.is_some() {
        class.push(classes!("pf-v5-c-table__sort"));
    }

    class.extend_from(&props.width);
    class.extend_from(&props.text_modifier);

    match &props.label {
        None => html! (<th></th>),
        Some(label) => {
            let th_content = if let Some(onsort) = &props.onsort {
                let header_context = table_header_context.expect(
                    "Column must be inside TableHeader, the expected context is defined there",
                );

                // If status of sort is not provided by user then use the context
                let sortby = if props.sortby.is_some() {
                    &props.sortby
                } else {
                    &header_context.sortby
                };

                let sort_by_next_status = match sortby {
                    Some(val) => {
                        if val.index == props.index {
                            class.push(classes!("pf-m-selected"));
                        }

                        if val.index == props.index {
                            let icon = match val.order {
                                Order::Ascending => Icon::LongArrowAltUp,
                                Order::Descending => Icon::LongArrowAltDown,
                            };
                            (icon, val.order)
                        } else {
                            (Icon::ArrowsAltV, Order::Descending)
                        }
                    }
                    None => (Icon::ArrowsAltV, Order::Descending),
                };

                html_nested!(
                    <button
                        title={label.clone()}
                        type="button"
                        class="pf-v5-c-table__button"
                        onclick={
                            {
                                // Emit sorting in context and in user callback
                                let onsort_context = header_context.onsort.clone();
                                let onsort = onsort.clone();

                                let index = props.index.clone();
                                let order = sort_by_next_status.1;

                                Callback::from(move |_| {
                                    let sort_by = TableHeaderSortBy {
                                        index: index.clone(),
                                        order: !order
                                    };
                                    onsort_context.emit(sort_by.clone());
                                    onsort.emit(sort_by.clone());
                                })
                            }
                        }
                    >
                        <div class="pf-v5-c-table__button-content">
                            <span class="pf-v5-c-table__text">{ &label }</span>
                            <span class="pf-v5-c-table__sort-indicator">
                                {sort_by_next_status.0}
                            </span>
                        </div>
                    </button>
                )
            } else {
                html_nested!(
                    <>{ &label }</>
                )
            };

            html!(
                <th title={label.clone()} {class} scope="col" role="columnheader">
                    {th_content}
                </th>
            )
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_round() {
        assert_eq!(round(0), 10);
        assert_eq!(round(10), 10);
        assert_eq!(round(50), 50);
        assert_eq!(round(54), 50);
        assert_eq!(round(55), 60);
        assert_eq!(round(56), 60);
        assert_eq!(round(100), 90);
        assert_eq!(round(100), 90);
        assert_eq!(round(200), 90);
    }
}