From ee943c05b3be7e76d76088f9c15a7aaf0a8aac7c Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 16 Feb 2025 22:19:41 +0100 Subject: [PATCH] better form --- src/client/components/form.rs | 53 +++++++++++++++-------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/src/client/components/form.rs b/src/client/components/form.rs index 75495b1..b8937ff 100644 --- a/src/client/components/form.rs +++ b/src/client/components/form.rs @@ -1,9 +1,9 @@ // src/client/components/form.rs use ratatui::{ - widgets::{Paragraph, Block, Borders}, + widgets::Paragraph, layout::{Layout, Constraint, Direction, Rect}, style::Style, - text::Line, + text::{Line, Span}, Frame, }; use crate::client::colors::Theme; @@ -16,44 +16,35 @@ pub fn render_form( inputs: &[&String], theme: &Theme, ) { + // Split the area into a single column layout let form_chunks = Layout::default() .direction(Direction::Vertical) - .constraints(vec![Constraint::Length(3); 8]) - .margin(1) + .constraints(vec![Constraint::Length(1); fields.len()]) // Each field takes 1 row .split(area); - let form_blocks = form_chunks.iter().enumerate().map(|(_i, chunk)| { - let chunks = Layout::default() - .direction(Direction::Horizontal) - .constraints([Constraint::Percentage(50), Constraint::Percentage(50)]) - .split(*chunk); - - vec![chunks[0], chunks[1]] - }).flatten().collect::>(); - for (i, field) in fields.iter().enumerate() { let input = inputs[i].clone(); let is_active = i == *current_field; - // Add this block construction - let block = Block::default() - .borders(Borders::ALL) - .border_style(Style::default().fg(if is_active { - theme.accent - } else { - theme.border - })) - .title(Line::from(field.to_string())) - .style(Style::default().bg(theme.bg).fg(theme.fg)); + // Split each row into two parts: label and input + let row_chunks = Layout::default() + .direction(Direction::Horizontal) + .constraints([Constraint::Percentage(30), Constraint::Percentage(70)]) + .split(form_chunks[i]); - let paragraph = Paragraph::new(input.as_str()) - .block(block) // Now using the defined block - .style(if is_active { - Style::default().fg(theme.highlight) - } else { - Style::default().fg(theme.fg) - }); + // Render the label on the left + let label = Paragraph::new(Line::from(Span::styled( + field.to_string(), + Style::default().fg(theme.fg), + ))); + f.render_widget(label, row_chunks[0]); - f.render_widget(paragraph, form_blocks[i]); + // Render the input on the right + let input_paragraph = Paragraph::new(input.as_str()).style(if is_active { + Style::default().fg(theme.highlight) + } else { + Style::default().fg(theme.fg) + }); + f.render_widget(input_paragraph, row_chunks[1]); } }