hikari-extra-components 0.2.1

Advanced UI components (node graph, rich text, etc.) for the Hikari design system
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Timeline component - Framework Agnostic State Model
//!
//! ## Migration Notice
//!
//! Previously a Dioxus component. Now provides a pure state model
//! for timeline/event display.

/// Timeline position
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum TimelinePosition {
    #[default]
    Left,
    Center,
    Right,
}

/// Timeline item status
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum TimelineStatus {
    #[default]
    Pending,
    InProgress,
    Completed,
    Cancelled,
}

/// A single item in the timeline
#[derive(Clone, PartialEq, Debug)]
pub struct TimelineItem {
    /// Item ID
    pub id: String,

    /// Item title
    pub title: String,

    /// Item description
    pub description: String,

    /// Item time/date
    pub time: String,

    /// Item icon (optional emoji)
    pub icon: String,

    /// Item status
    pub status: TimelineStatus,

    /// Whether item is expanded
    pub expanded: bool,

    /// Whether to show connecting line
    pub show_line: bool,
}

impl TimelineItem {
    /// Create a new timeline item
    pub fn new(id: impl Into<String>, title: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            title: title.into(),
            description: String::new(),
            time: String::new(),
            icon: String::new(),
            status: TimelineStatus::default(),
            expanded: false,
            show_line: true,
        }
    }

    /// Set the description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Set the time
    pub fn with_time(mut self, time: impl Into<String>) -> Self {
        self.time = time.into();
        self
    }

    /// Set the icon
    pub fn with_icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = icon.into();
        self
    }

    /// Set the status
    pub fn with_status(mut self, status: TimelineStatus) -> Self {
        self.status = status;
        self
    }

    /// Set whether expanded
    pub fn with_expanded(mut self, expanded: bool) -> Self {
        self.expanded = expanded;
        self
    }

    /// Get the status class name
    pub fn status_class(&self) -> &'static str {
        match self.status {
            TimelineStatus::Pending => "hi-timeline-pending",
            TimelineStatus::InProgress => "hi-timeline-in-progress",
            TimelineStatus::Completed => "hi-timeline-completed",
            TimelineStatus::Cancelled => "hi-timeline-cancelled",
        }
    }

    /// Get the dot status class name
    pub fn dot_status_class(&self) -> &'static str {
        match self.status {
            TimelineStatus::Pending => "hi-timeline-dot-pending",
            TimelineStatus::InProgress => "hi-timeline-dot-in-progress",
            TimelineStatus::Completed => "hi-timeline-dot-completed",
            TimelineStatus::Cancelled => "hi-timeline-dot-cancelled",
        }
    }
}

/// State model for a timeline
///
/// ## Example
///
/// ```rust
/// use hikari_extra_components::extra::{TimelineState, TimelineItem, TimelineStatus, TimelinePosition};
///
/// let mut state = TimelineState::new();
/// state.position = TimelinePosition::Left;
/// state.items.push(
///     TimelineItem::new("1", "Project Started")
///         .with_status(TimelineStatus::Completed)
///         .with_time("2024-01-01")
///         .with_icon("🚀")
/// );
/// ```
#[derive(Clone, PartialEq, Debug)]
pub struct TimelineState {
    /// Timeline items
    pub items: Vec<TimelineItem>,

    /// Timeline position
    pub position: TimelinePosition,

    /// Whether to show connecting line
    pub show_line: bool,

    /// Additional CSS classes
    pub class: String,
}

impl TimelineState {
    /// Create a new timeline state
    pub fn new() -> Self {
        Self {
            items: Vec::new(),
            position: TimelinePosition::default(),
            show_line: true,
            class: String::new(),
        }
    }

    /// Set the position
    pub fn with_position(mut self, position: TimelinePosition) -> Self {
        self.position = position;
        self
    }

    /// Set whether to show line
    pub fn with_show_line(mut self, show: bool) -> Self {
        self.show_line = show;
        self
    }

    /// Set custom CSS class
    pub fn with_class(mut self, class: impl Into<String>) -> Self {
        self.class = class.into();
        self
    }

    /// Add an item
    pub fn add_item(&mut self, item: TimelineItem) {
        self.items.push(item);
    }

    /// Toggle item expansion
    pub fn toggle_item(&mut self, id: &str) -> bool {
        if let Some(item) = self.items.iter_mut().find(|i| i.id == id) {
            item.expanded = !item.expanded;
            true
        } else {
            false
        }
    }

    /// Update item status
    pub fn update_status(&mut self, id: &str, status: TimelineStatus) -> bool {
        if let Some(item) = self.items.iter_mut().find(|i| i.id == id) {
            item.status = status;
            true
        } else {
            false
        }
    }

    /// Get the position class name
    pub fn position_class(&self) -> &'static str {
        match self.position {
            TimelinePosition::Left => "hi-timeline-left",
            TimelinePosition::Center => "hi-timeline-center",
            TimelinePosition::Right => "hi-timeline-right",
        }
    }

    /// Get the CSS class string
    pub fn class_string(&self) -> String {
        let base = if self.class.is_empty() {
            self.position_class().to_string()
        } else {
            format!("{} {}", self.position_class(), self.class)
        };

        if self.show_line {
            format!("{} hi-timeline-line", base)
        } else {
            base
        }
    }
}

impl Default for TimelineState {
    fn default() -> Self {
        Self::new()
    }
}

pub fn render_timeline(state: &TimelineState) -> tairitsu_vdom::VNode {
    use tairitsu_vdom::{VElement, VNode, VText};

    let mut item_nodes: Vec<VNode> = Vec::with_capacity(state.items.len());

    for item in &state.items {
        let mut dot_children: Vec<VNode> = Vec::new();
        if !item.icon.is_empty() {
            dot_children.push(VNode::Element(
                VElement::new("span")
                    .class("hi-timeline-icon")
                    .child(VNode::Text(VText::new(&item.icon))),
            ));
        }

        let mut content_children: Vec<VNode> = Vec::new();

        let mut header_children: Vec<VNode> = Vec::new();
        header_children.push(VNode::Element(
            VElement::new("h4")
                .class("hi-timeline-title")
                .child(VNode::Text(VText::new(&item.title))),
        ));
        if !item.time.is_empty() {
            header_children.push(VNode::Element(
                VElement::new("span")
                    .class("hi-timeline-time")
                    .child(VNode::Text(VText::new(&item.time))),
            ));
        }

        content_children.push(VNode::Element(
            VElement::new("div")
                .class("hi-timeline-header")
                .children(header_children),
        ));

        if item.expanded && !item.description.is_empty() {
            content_children.push(VNode::Element(
                VElement::new("div")
                    .class("hi-timeline-description hi-timeline-description-expanded")
                    .child(VNode::Element(
                        VElement::new("p").child(VNode::Text(VText::new(&item.description))),
                    )),
            ));
        }

        let item_class = format!("hi-timeline-item {}", item.status_class());
        let dot_class = format!("hi-timeline-dot {}", item.dot_status_class());

        let item_node = VNode::Element(
            VElement::new("div")
                .class(item_class)
                .child(VNode::Element(
                    VElement::new("div").class(dot_class).children(dot_children),
                ))
                .child(VNode::Element(
                    VElement::new("div")
                        .class("hi-timeline-content")
                        .children(content_children),
                )),
        );

        item_nodes.push(item_node);
    }

    let container_class = format!("hi-timeline {}", state.class_string());

    VNode::Element(
        VElement::new("div")
            .class(container_class)
            .children(item_nodes),
    )
}

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

    #[test]
    fn test_timeline_item_new() {
        let item = TimelineItem::new("1", "Test Item");
        assert_eq!(item.id, "1");
        assert_eq!(item.title, "Test Item");
        assert_eq!(item.status, TimelineStatus::Pending);
    }

    #[test]
    fn test_timeline_item_builder() {
        let item = TimelineItem::new("1", "Test")
            .with_description("Description")
            .with_time("2024-01-01")
            .with_icon("🎯")
            .with_status(TimelineStatus::Completed)
            .with_expanded(true);

        assert_eq!(item.description, "Description");
        assert_eq!(item.time, "2024-01-01");
        assert_eq!(item.icon, "🎯");
        assert_eq!(item.status, TimelineStatus::Completed);
        assert!(item.expanded);
    }

    #[test]
    fn test_timeline_state_new() {
        let state = TimelineState::new();
        assert!(state.items.is_empty());
        assert_eq!(state.position, TimelinePosition::Left);
        assert!(state.show_line);
    }

    #[test]
    fn test_add_item() {
        let mut state = TimelineState::new();
        state.add_item(TimelineItem::new("1", "First"));
        state.add_item(TimelineItem::new("2", "Second"));

        assert_eq!(state.items.len(), 2);
        assert_eq!(state.items[0].title, "First");
        assert_eq!(state.items[1].title, "Second");
    }

    #[test]
    fn test_toggle_item() {
        let mut state = TimelineState::new();
        state.add_item(TimelineItem::new("1", "First"));

        assert!(!state.items[0].expanded);
        assert!(state.toggle_item("1"));
        assert!(state.items[0].expanded);
        assert!(state.toggle_item("1"));
        assert!(!state.items[0].expanded);
    }

    #[test]
    fn test_update_status() {
        let mut state = TimelineState::new();
        state.add_item(TimelineItem::new("1", "First"));

        assert!(state.update_status("1", TimelineStatus::Completed));
        assert_eq!(state.items[0].status, TimelineStatus::Completed);

        assert!(!state.update_status("2", TimelineStatus::Completed));
    }

    #[test]
    fn test_status_classes() {
        let item = TimelineItem::new("1", "Test");

        assert_eq!(item.status_class(), "hi-timeline-pending");
        assert_eq!(item.dot_status_class(), "hi-timeline-dot-pending");
    }

    #[test]
    fn test_position_classes() {
        let state = TimelineState::new();
        assert_eq!(state.position_class(), "hi-timeline-left");

        let state = state.with_position(TimelinePosition::Center);
        assert_eq!(state.position_class(), "hi-timeline-center");
    }

    #[test]
    fn test_class_string() {
        let state = TimelineState::new()
            .with_show_line(true)
            .with_class("custom");
        let class = state.class_string();
        assert!(class.contains("hi-timeline-left"));
        assert!(class.contains("hi-timeline-line"));
        assert!(class.contains("custom"));
    }
}