added multiple period input test

This commit is contained in:
Nickiel12 2023-09-25 20:20:22 -07:00
parent d8b7473576
commit 9505c47b2d

View file

@ -21,6 +21,7 @@ pub struct QuickPaycheckState {
pub gross_amount: String, pub gross_amount: String,
pub net_amount: String, pub net_amount: String,
selected: SelectedEntry, selected: SelectedEntry,
has_error: Option<SelectedEntry>,
} }
impl QuickPaycheckState { impl QuickPaycheckState {
@ -29,6 +30,7 @@ impl QuickPaycheckState {
net_amount: "".to_string(), net_amount: "".to_string(),
gross_amount: "".to_string(), gross_amount: "".to_string(),
selected: SelectedEntry::Net, selected: SelectedEntry::Net,
has_error: None,
} }
} }
@ -54,6 +56,7 @@ impl QuickPaycheckState {
"Take home:", "Take home:",
&state.net_amount, &state.net_amount,
state.selected == SelectedEntry::Net, state.selected == SelectedEntry::Net,
state.has_error == Some(SelectedEntry::Net)
); );
render_labled_entry_row( render_labled_entry_row(
@ -62,9 +65,20 @@ impl QuickPaycheckState {
"Gross:", "Gross:",
&state.gross_amount, &state.gross_amount,
state.selected == SelectedEntry::Gross, state.selected == SelectedEntry::Gross,
state.has_error == Some(SelectedEntry::Gross)
); );
} }
pub fn validate_input(&mut self) {
// The field only accepts digits and periods, so the only fail state
// should be multiple periods
if self.gross_amount.chars().filter(|c| *c == '.').count() > 1 {
self.has_error = Some(SelectedEntry::Gross);
} else if self.net_amount.chars().filter(|c| *c == '.').count() > 1 {
self.has_error = Some(SelectedEntry::Net);
}
}
pub fn handle_event(event: KeyEvent, app: &mut App) { pub fn handle_event(event: KeyEvent, app: &mut App) {
if event.kind == KeyEventKind::Press { if event.kind == KeyEventKind::Press {
match event.code { match event.code {
@ -80,6 +94,7 @@ impl QuickPaycheckState {
app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Gross; app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Gross;
} }
} }
app.states.quick_transactions.paycheck_state.validate_input();
} }
KeyCode::Down | KeyCode::Tab => { KeyCode::Down | KeyCode::Tab => {
match app.states.quick_transactions.paycheck_state.selected { match app.states.quick_transactions.paycheck_state.selected {
@ -93,14 +108,36 @@ impl QuickPaycheckState {
app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Net; app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Net;
} }
} }
app.states.quick_transactions.paycheck_state.validate_input();
}
KeyCode::Enter => {
app.states.quick_transactions.paycheck_state.validate_input();
match app.states.quick_transactions.paycheck_state.selected {
SelectedEntry::Net => {
app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Gross;
}
SelectedEntry::Gross => {
app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Execute;
}
SelectedEntry::Execute => {
if app.states.quick_transactions.paycheck_state.has_error.is_none() {
// TODO: actually do something here. E.g. Input validation, error
// messages, and actual submissions
}
}
}
} }
KeyCode::Char(value) => { KeyCode::Char(value) => {
match app.states.quick_transactions.paycheck_state.selected { match app.states.quick_transactions.paycheck_state.selected {
SelectedEntry::Net => { SelectedEntry::Net => {
app.states.quick_transactions.paycheck_state.net_amount.push(value); if value.is_ascii_digit() || value == '.' {
app.states.quick_transactions.paycheck_state.net_amount.push(value);
}
} }
SelectedEntry::Gross => { SelectedEntry::Gross => {
app.states.quick_transactions.paycheck_state.gross_amount.push(value); if value.is_ascii_digit() || value == '.' {
app.states.quick_transactions.paycheck_state.gross_amount.push(value);
}
} }
_ => {} _ => {}
} }
@ -134,6 +171,7 @@ fn render_labled_entry_row<B: Backend>(
left_text: &str, left_text: &str,
right_text: &String, right_text: &String,
do_highlight: bool, do_highlight: bool,
is_error: bool,
) { ) {
let horizontal_pieces = Layout::default() let horizontal_pieces = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
@ -150,10 +188,16 @@ fn render_labled_entry_row<B: Backend>(
horizontal_pieces[0], horizontal_pieces[0],
); );
let right_bg_color = if do_highlight { let right_bg_color = {
Color::Yellow if do_highlight {
} else { Color::Yellow
Color::White } else {
if is_error {
Color::LightRed
} else {
Color::White
}
}
}; };
f.render_widget( f.render_widget(