use super::types::MultiSelect;
impl MultiSelect {
pub fn is_open(&self) -> bool {
self.open
}
pub fn get_selected_indices(&self) -> &[usize] {
&self.selected
}
pub fn get_selected_values(&self) -> Vec<&str> {
self.selected
.iter()
.filter_map(|&i| self.options.get(i).map(|o| o.value.as_str()))
.collect()
}
pub fn get_selected_labels(&self) -> Vec<&str> {
self.selected
.iter()
.filter_map(|&i| self.options.get(i).map(|o| o.label.as_str()))
.collect()
}
pub fn selection_count(&self) -> usize {
self.selected.len()
}
pub fn is_selected(&self, index: usize) -> bool {
self.selected.contains(&index)
}
pub fn can_select_more(&self) -> bool {
match self.max_selections {
Some(max) => self.selected.len() < max,
None => true,
}
}
pub fn len(&self) -> usize {
self.options.len()
}
pub fn is_empty(&self) -> bool {
self.options.is_empty()
}
pub fn open(&mut self) {
self.open = true;
self.tag_cursor = None;
self.reset_filter();
}
pub fn close(&mut self) {
self.open = false;
self.query.clear();
self.reset_filter();
}
pub fn toggle(&mut self) {
if self.open {
self.close();
} else {
self.open();
}
}
pub fn select_option(&mut self, index: usize) {
if index >= self.options.len() {
return;
}
if self.options[index].disabled {
return;
}
if !self.selected.contains(&index) && self.can_select_more() {
self.selected.push(index);
}
}
pub fn deselect_option(&mut self, index: usize) {
self.selected.retain(|&i| i != index);
}
pub fn toggle_option(&mut self, index: usize) {
if self.is_selected(index) {
self.deselect_option(index);
} else {
self.select_option(index);
}
}
pub fn clear_selection(&mut self) {
self.selected.clear();
}
pub fn select_all(&mut self) {
self.selected = (0..self.options.len())
.filter(|&i| !self.options[i].disabled)
.collect();
if let Some(max) = self.max_selections {
self.selected.truncate(max);
}
}
pub fn remove_last_tag(&mut self) {
self.selected.pop();
}
pub fn remove_tag_at_cursor(&mut self) {
if let Some(cursor) = self.tag_cursor {
if cursor < self.selected.len() {
self.selected.remove(cursor);
if self.selected.is_empty() {
self.tag_cursor = None;
} else if cursor >= self.selected.len() {
self.tag_cursor = Some(self.selected.len() - 1);
}
}
}
}
}