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
//! Accordion component for collapsible sections
//!
//! Displays collapsible content sections.
//!
//! # Example
//!
//! ```rust,ignore
//! use rnk::prelude::*;
//! use rnk::components::Accordion;
//!
//! fn app() -> Element {
//! Accordion::new()
//! .item("Section 1", "Content for section 1")
//! .item("Section 2", "Content for section 2")
//! .expanded(0)
//! .into_element()
//! }
//! ```
use crate::components::{Box as RnkBox, Text};
use crate::core::{Color, Element, FlexDirection};
/// An accordion item
#[derive(Debug, Clone)]
pub struct AccordionItem {
title: String,
content: String,
}
/// An accordion component with collapsible sections
#[derive(Debug, Clone)]
pub struct Accordion {
items: Vec<AccordionItem>,
expanded: Option<usize>,
allow_multiple: bool,
expanded_set: Vec<usize>,
}
impl Accordion {
/// Create a new accordion
pub fn new() -> Self {
Self {
items: Vec::new(),
expanded: None,
allow_multiple: false,
expanded_set: Vec::new(),
}
}
/// Add an item to the accordion
pub fn item(mut self, title: impl Into<String>, content: impl Into<String>) -> Self {
self.items.push(AccordionItem {
title: title.into(),
content: content.into(),
});
self
}
/// Set the expanded item index (single mode)
pub fn expanded(mut self, index: usize) -> Self {
self.expanded = Some(index);
self
}
/// Allow multiple items to be expanded
pub fn allow_multiple(mut self) -> Self {
self.allow_multiple = true;
self
}
/// Set multiple expanded items
pub fn expanded_items(mut self, indices: Vec<usize>) -> Self {
self.expanded_set = indices;
self.allow_multiple = true;
self
}
/// Convert to Element
pub fn into_element(self) -> Element {
let mut children = Vec::new();
for (i, item) in self.items.iter().enumerate() {
let is_expanded = if self.allow_multiple {
self.expanded_set.contains(&i)
} else {
self.expanded == Some(i)
};
// Header
let indicator = if is_expanded { "▼" } else { "▶" };
children.push(
RnkBox::new()
.flex_direction(FlexDirection::Row)
.padding_x(1.0)
.background(Color::Ansi256(238))
.children(vec![
Text::new(indicator)
.color(Color::Cyan)
.into_element(),
Text::new(format!(" {}", item.title))
.color(Color::White)
.bold()
.into_element(),
])
.into_element(),
);
// Content (if expanded)
if is_expanded {
children.push(
RnkBox::new()
.padding_x(2.0)
.padding_y(0.5)
.background(Color::Ansi256(236))
.child(
Text::new(&item.content)
.color(Color::White)
.into_element(),
)
.into_element(),
);
}
}
RnkBox::new()
.flex_direction(FlexDirection::Column)
.children(children)
.into_element()
}
}
impl Default for Accordion {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_accordion_creation() {
let acc = Accordion::new();
assert!(acc.items.is_empty());
}
#[test]
fn test_accordion_items() {
let acc = Accordion::new()
.item("Title 1", "Content 1")
.item("Title 2", "Content 2");
assert_eq!(acc.items.len(), 2);
}
#[test]
fn test_accordion_expanded() {
let acc = Accordion::new()
.item("A", "a")
.item("B", "b")
.expanded(0);
assert_eq!(acc.expanded, Some(0));
}
#[test]
fn test_accordion_into_element() {
let _ = Accordion::new()
.item("Section", "Content")
.expanded(0)
.into_element();
}
#[test]
fn test_accordion_multiple() {
let _ = Accordion::new()
.item("A", "a")
.item("B", "b")
.allow_multiple()
.expanded_items(vec![0, 1])
.into_element();
}
}