deletion of the selected works

This commit is contained in:
filipriec
2025-04-18 11:15:15 +02:00
parent 92a9011f27
commit 305bcfcf62
4 changed files with 44 additions and 9 deletions

View File

@@ -197,13 +197,14 @@ pub fn render_add_table(
.iter()
.map(|col_def| {
Row::new(vec![
Cell::from(if col_def.selected { "[*]" } else { "[ ]" }),
Cell::from(col_def.name.clone()),
Cell::from(col_def.data_type.clone()),
])
.style(Style::default().fg(theme.fg))
})
.collect();
let header_cells = ["Name", "Type"]
let header_cells = ["Sel", "Name", "Type"]
.iter()
.map(|h| Cell::from(*h).style(Style::default().fg(theme.accent)));
let header = Row::new(header_cells).height(1).bottom_margin(1);

View File

@@ -7,6 +7,7 @@ use crate::state::{
use crossterm::event::{KeyEvent};
use ratatui::widgets::TableState;
use crate::tui::functions::common::add_table::handle_add_column_action;
use crate::tui::functions::common::add_table::handle_delete_selected_columns;
/// Handles navigation events specifically for the Add Table view.
/// Returns true if the event was handled, false otherwise.
@@ -199,11 +200,18 @@ pub fn handle_add_table_navigation(
*command_message = "Entered Links Table (Scroll with Up/Down, Select to toggle/exit)".to_string();
}
AddTableFocus::InsideColumnsTable => {
// Select does nothing here anymore, only Esc exits.
// Toggle selection when pressing select *inside* the columns table
if let Some(index) = add_table_state.column_table_state.selected() {
*command_message = format!("Selected column index {} (Press Esc to exit scroll mode)", index);
if let Some(col) = add_table_state.columns.get_mut(index) {
col.selected = !col.selected;
add_table_state.has_unsaved_changes = true;
*command_message = format!(
"Toggled selection for column: {} to {}",
col.name, col.selected
);
}
} else {
*command_message = "No column selected (Press Esc to exit scroll mode)".to_string();
*command_message = "No column highlighted to toggle selection".to_string();
}
}
AddTableFocus::InsideIndexesTable => {
@@ -247,8 +255,7 @@ pub fn handle_add_table_navigation(
// TODO: Implement logic
}
AddTableFocus::DeleteSelectedButton => {
*command_message = "Action: Delete selected".to_string();
// TODO: Implement logic
handle_delete_selected_columns(add_table_state, command_message);
}
AddTableFocus::CancelButton => {
*command_message = "Action: Cancel Add Table".to_string();

View File

@@ -6,6 +6,7 @@ use ratatui::widgets::TableState;
pub struct ColumnDefinition {
pub name: String,
pub data_type: String,
pub selected: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]

View File

@@ -36,6 +36,7 @@ pub fn handle_add_column_action(
let new_column = ColumnDefinition {
name: column_name_in.to_string(),
data_type: column_type_in.to_string(),
selected: false,
};
add_table_state.columns.push(new_column.clone()); // Clone for msg
msg.push_str(&format!("Column '{}' added.", new_column.name));
@@ -73,3 +74,28 @@ pub fn handle_add_column_action(
}
}
}
/// Handles deleting columns marked as selected in the AddTableState.
pub fn handle_delete_selected_columns(
add_table_state: &mut AddTableState,
command_message: &mut String,
) {
let initial_count = add_table_state.columns.len();
// Keep only the columns that are NOT selected
add_table_state.columns.retain(|col| !col.selected);
let deleted_count = initial_count - add_table_state.columns.len();
if deleted_count > 0 {
*command_message = format!("Deleted {} selected column(s).", deleted_count);
add_table_state.has_unsaved_changes = true;
// Reset selection highlight as indices have changed
add_table_state.column_table_state.select(None);
// Optionally, select the first item if the list is not empty
// if !add_table_state.columns.is_empty() {
// add_table_state.column_table_state.select(Some(0));
// }
} else {
*command_message = "No columns marked for deletion.".to_string();
}
}