moved form.rs into the state where it really belongs to
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
// src/state/mod.rs
|
||||
pub mod state;
|
||||
pub mod pages;
|
||||
|
||||
3
client/src/state/pages.rs
Normal file
3
client/src/state/pages.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
// src/state/pages.rs
|
||||
|
||||
pub mod form;
|
||||
73
client/src/state/pages/form.rs
Normal file
73
client/src/state/pages/form.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
// src/state/pages/form.rs
|
||||
use crate::config::colors::themes::Theme;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::Frame;
|
||||
|
||||
pub struct FormState {
|
||||
pub id: i64,
|
||||
pub fields: Vec<String>, // Use Vec<String> for dynamic field names
|
||||
pub values: Vec<String>, // Store field values dynamically
|
||||
pub current_field: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub current_cursor_pos: usize,
|
||||
}
|
||||
|
||||
impl FormState {
|
||||
/// Create a new FormState with dynamic fields.
|
||||
pub fn new(fields: Vec<String>) -> Self {
|
||||
let values = vec![String::new(); fields.len()]; // Initialize values for each field
|
||||
FormState {
|
||||
id: 0,
|
||||
fields,
|
||||
values,
|
||||
current_field: 0,
|
||||
has_unsaved_changes: false,
|
||||
current_cursor_pos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
&self,
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
) {
|
||||
let fields: Vec<&str> = self.fields.iter().map(|s| s.as_str()).collect();
|
||||
let values: Vec<&String> = self.values.iter().collect();
|
||||
|
||||
crate::components::form::form::render_form(
|
||||
f,
|
||||
area,
|
||||
self,
|
||||
&fields,
|
||||
&self.current_field,
|
||||
&values,
|
||||
theme,
|
||||
is_edit_mode,
|
||||
total_count,
|
||||
current_position,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn reset_to_empty(&mut self) {
|
||||
self.id = 0; // Reset ID to 0 for new entries
|
||||
self.values.iter_mut().for_each(|v| v.clear()); // Clear all values
|
||||
self.has_unsaved_changes = false;
|
||||
}
|
||||
|
||||
pub fn get_current_input(&self) -> &str {
|
||||
self.values
|
||||
.get(self.current_field)
|
||||
.map(|s| s.as_str())
|
||||
.unwrap_or("")
|
||||
}
|
||||
|
||||
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||
self.values
|
||||
.get_mut(self.current_field)
|
||||
.expect("Invalid current_field index")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user