use crate::components::navigation::{
NavigationConfig, calculate_visible_range, handle_list_navigation,
};
use crate::components::{Box as TinkBox, Text};
use crate::core::{Color, Element, FlexDirection};
use crate::hooks::{use_input, use_signal};
#[derive(Debug, Clone)]
pub struct MultiSelectItem<T: Clone> {
pub label: String,
pub value: T,
pub selected: bool,
}
impl<T: Clone> MultiSelectItem<T> {
pub fn new(label: impl Into<String>, value: T) -> Self {
Self {
label: label.into(),
value,
selected: false,
}
}
pub fn selected(label: impl Into<String>, value: T) -> Self {
Self {
label: label.into(),
value,
selected: true,
}
}
pub fn with_selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
}
#[derive(Debug, Clone)]
pub struct MultiSelectStyle {
pub highlight_color: Option<Color>,
pub highlight_bg: Option<Color>,
pub highlight_bold: bool,
pub indicator: String,
pub indicator_padding: String,
pub checkbox_selected: String,
pub checkbox_unselected: String,
pub selected_color: Option<Color>,
pub item_color: Option<Color>,
}
impl Default for MultiSelectStyle {
fn default() -> Self {
Self {
highlight_color: Some(Color::Cyan),
highlight_bg: None,
highlight_bold: true,
indicator: "❯ ".to_string(),
indicator_padding: " ".to_string(),
checkbox_selected: "◉ ".to_string(),
checkbox_unselected: "◯ ".to_string(),
selected_color: Some(Color::Green),
item_color: None,
}
}
}
impl MultiSelectStyle {
pub fn new() -> Self {
Self::default()
}
pub fn highlight_color(mut self, color: Color) -> Self {
self.highlight_color = Some(color);
self
}
pub fn highlight_bg(mut self, color: Color) -> Self {
self.highlight_bg = Some(color);
self
}
pub fn highlight_bold(mut self, bold: bool) -> Self {
self.highlight_bold = bold;
self
}
pub fn indicator(mut self, indicator: impl Into<String>) -> Self {
let ind = indicator.into();
self.indicator_padding = " ".repeat(ind.chars().count());
self.indicator = ind;
self
}
pub fn checkboxes(
mut self,
selected: impl Into<String>,
unselected: impl Into<String>,
) -> Self {
self.checkbox_selected = selected.into();
self.checkbox_unselected = unselected.into();
self
}
pub fn selected_color(mut self, color: Color) -> Self {
self.selected_color = Some(color);
self
}
pub fn item_color(mut self, color: Color) -> Self {
self.item_color = Some(color);
self
}
}
pub struct MultiSelect<T: Clone + 'static> {
items: Vec<MultiSelectItem<T>>,
highlighted: usize,
limit: Option<usize>,
style: MultiSelectStyle,
is_focused: bool,
vim_navigation: bool,
number_shortcuts: bool,
}
impl<T: Clone + 'static> MultiSelect<T> {
pub fn new(items: Vec<MultiSelectItem<T>>) -> Self {
Self {
items,
highlighted: 0,
limit: None,
style: MultiSelectStyle::default(),
is_focused: true,
vim_navigation: true,
number_shortcuts: false,
}
}
pub fn from_items<I>(iter: I) -> Self
where
I: IntoIterator<Item = MultiSelectItem<T>>,
{
Self::new(iter.into_iter().collect())
}
pub fn highlighted(mut self, index: usize) -> Self {
self.highlighted = index.min(self.items.len().saturating_sub(1));
self
}
pub fn limit(mut self, limit: usize) -> Self {
self.limit = Some(limit);
self
}
pub fn style(mut self, style: MultiSelectStyle) -> Self {
self.style = style;
self
}
pub fn focused(mut self, focused: bool) -> Self {
self.is_focused = focused;
self
}
pub fn vim_navigation(mut self, enabled: bool) -> Self {
self.vim_navigation = enabled;
self
}
pub fn number_shortcuts(mut self, enabled: bool) -> Self {
self.number_shortcuts = enabled;
self
}
pub fn highlight_color(mut self, color: Color) -> Self {
self.style.highlight_color = Some(color);
self
}
pub fn indicator(mut self, indicator: impl Into<String>) -> Self {
self.style = self.style.indicator(indicator);
self
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn selected_items(&self) -> Vec<&MultiSelectItem<T>> {
self.items.iter().filter(|item| item.selected).collect()
}
pub fn selected_values(&self) -> Vec<&T> {
self.items
.iter()
.filter(|item| item.selected)
.map(|item| &item.value)
.collect()
}
pub fn into_element(self) -> Element {
if self.items.is_empty() {
return TinkBox::new().into_element();
}
let initial_highlighted = self.highlighted;
let initial_selections: Vec<bool> = self.items.iter().map(|i| i.selected).collect();
let items = self.items.clone();
let limit = self.limit;
let style = self.style.clone();
let is_focused = self.is_focused;
let vim_navigation = self.vim_navigation;
let number_shortcuts = self.number_shortcuts;
let highlighted_signal = use_signal(|| initial_highlighted);
let selections_signal = use_signal(|| initial_selections);
if is_focused {
let items_len = items.len();
let highlighted_for_input = highlighted_signal.clone();
let selections_for_input = selections_signal.clone();
use_input(move |input, key| {
let current = highlighted_for_input.get();
let config = NavigationConfig::new()
.vim_navigation(vim_navigation)
.number_shortcuts(number_shortcuts);
let result = handle_list_navigation(current, items_len, input, *key, &config);
if result.is_moved() {
let new_pos = result.unwrap_or(current);
if new_pos != current {
highlighted_for_input.set(new_pos);
}
}
if key.space {
selections_for_input.update(|selections| {
if let Some(selected) = selections.get_mut(current) {
*selected = !*selected;
}
});
}
if input == "a" && key.ctrl {
selections_for_input.update(|selections| {
for selected in selections.iter_mut() {
*selected = true;
}
});
}
if input == "d" && key.ctrl {
selections_for_input.update(|selections| {
for selected in selections.iter_mut() {
*selected = false;
}
});
}
});
}
render_multi_select_list(&items, highlighted_signal, selections_signal, limit, &style)
}
}
fn render_multi_select_list<T: Clone + 'static>(
items: &[MultiSelectItem<T>],
highlighted_signal: crate::hooks::Signal<usize>,
selections_signal: crate::hooks::Signal<Vec<bool>>,
limit: Option<usize>,
style: &MultiSelectStyle,
) -> Element {
let highlighted = highlighted_signal.get();
let selections = selections_signal.get();
let total_items = items.len();
let (start, end) = calculate_visible_range(highlighted, total_items, limit);
let mut container = TinkBox::new().flex_direction(FlexDirection::Column);
for (idx, item) in items.iter().enumerate().skip(start).take(end - start) {
let is_highlighted = idx == highlighted;
let is_selected = selections.get(idx).copied().unwrap_or(item.selected);
let prefix = if is_highlighted {
&style.indicator
} else {
&style.indicator_padding
};
let checkbox = if is_selected {
&style.checkbox_selected
} else {
&style.checkbox_unselected
};
let label = format!("{}{}{}", prefix, checkbox, item.label);
let mut text = Text::new(&label);
if is_highlighted {
if let Some(color) = style.highlight_color {
text = text.color(color);
}
if let Some(bg) = style.highlight_bg {
text = text.background(bg);
}
if style.highlight_bold {
text = text.bold();
}
} else if is_selected {
if let Some(color) = style.selected_color {
text = text.color(color);
}
} else if let Some(color) = style.item_color {
text = text.color(color);
}
container = container.child(text.into_element());
}
container.into_element()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_multi_select_item_creation() {
let item = MultiSelectItem::new("Test", 42);
assert_eq!(item.label, "Test");
assert_eq!(item.value, 42);
assert!(!item.selected);
}
#[test]
fn test_multi_select_item_selected() {
let item = MultiSelectItem::selected("Test", 42);
assert!(item.selected);
}
#[test]
fn test_multi_select_creation() {
let items = vec![
MultiSelectItem::new("One", 1),
MultiSelectItem::selected("Two", 2),
MultiSelectItem::new("Three", 3),
];
let select = MultiSelect::new(items);
assert_eq!(select.len(), 3);
assert!(!select.is_empty());
}
#[test]
fn test_multi_select_empty() {
let select: MultiSelect<i32> = MultiSelect::new(vec![]);
assert!(select.is_empty());
assert_eq!(select.len(), 0);
}
#[test]
fn test_multi_select_selected_values() {
let items = vec![
MultiSelectItem::new("One", 1),
MultiSelectItem::selected("Two", 2),
MultiSelectItem::selected("Three", 3),
];
let select = MultiSelect::new(items);
let selected = select.selected_values();
assert_eq!(selected.len(), 2);
assert!(selected.contains(&&2));
assert!(selected.contains(&&3));
}
#[test]
fn test_multi_select_style() {
let style = MultiSelectStyle::new()
.highlight_color(Color::Green)
.indicator("> ")
.checkboxes("[x]", "[ ]");
assert_eq!(style.highlight_color, Some(Color::Green));
assert_eq!(style.indicator, "> ");
assert_eq!(style.checkbox_selected, "[x]");
assert_eq!(style.checkbox_unselected, "[ ]");
}
#[test]
fn test_multi_select_builder_chain() {
let items = vec![MultiSelectItem::new("Test", 1)];
let select = MultiSelect::new(items)
.highlighted(0)
.limit(5)
.focused(true)
.vim_navigation(true)
.highlight_color(Color::Yellow)
.indicator("→ ");
assert_eq!(select.highlighted, 0);
assert_eq!(select.limit, Some(5));
assert!(select.is_focused);
assert!(select.vim_navigation);
}
#[test]
fn test_multi_select_from_items() {
let items = vec![
MultiSelectItem::new("A", 'a'),
MultiSelectItem::new("B", 'b'),
];
let select = MultiSelect::from_items(items);
assert_eq!(select.len(), 2);
}
}