added multiple period input test
This commit is contained in:
parent
d8b7473576
commit
9505c47b2d
1 changed files with 50 additions and 6 deletions
|
@ -21,6 +21,7 @@ pub struct QuickPaycheckState {
|
|||
pub gross_amount: String,
|
||||
pub net_amount: String,
|
||||
selected: SelectedEntry,
|
||||
has_error: Option<SelectedEntry>,
|
||||
}
|
||||
|
||||
impl QuickPaycheckState {
|
||||
|
@ -29,6 +30,7 @@ impl QuickPaycheckState {
|
|||
net_amount: "".to_string(),
|
||||
gross_amount: "".to_string(),
|
||||
selected: SelectedEntry::Net,
|
||||
has_error: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,6 +56,7 @@ impl QuickPaycheckState {
|
|||
"Take home:",
|
||||
&state.net_amount,
|
||||
state.selected == SelectedEntry::Net,
|
||||
state.has_error == Some(SelectedEntry::Net)
|
||||
);
|
||||
|
||||
render_labled_entry_row(
|
||||
|
@ -62,9 +65,20 @@ impl QuickPaycheckState {
|
|||
"Gross:",
|
||||
&state.gross_amount,
|
||||
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) {
|
||||
if event.kind == KeyEventKind::Press {
|
||||
match event.code {
|
||||
|
@ -80,6 +94,7 @@ impl QuickPaycheckState {
|
|||
app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Gross;
|
||||
}
|
||||
}
|
||||
app.states.quick_transactions.paycheck_state.validate_input();
|
||||
}
|
||||
KeyCode::Down | KeyCode::Tab => {
|
||||
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.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) => {
|
||||
match app.states.quick_transactions.paycheck_state.selected {
|
||||
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 => {
|
||||
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,
|
||||
right_text: &String,
|
||||
do_highlight: bool,
|
||||
is_error: bool,
|
||||
) {
|
||||
let horizontal_pieces = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
|
@ -150,10 +188,16 @@ fn render_labled_entry_row<B: Backend>(
|
|||
horizontal_pieces[0],
|
||||
);
|
||||
|
||||
let right_bg_color = if do_highlight {
|
||||
Color::Yellow
|
||||
} else {
|
||||
Color::White
|
||||
let right_bg_color = {
|
||||
if do_highlight {
|
||||
Color::Yellow
|
||||
} else {
|
||||
if is_error {
|
||||
Color::LightRed
|
||||
} else {
|
||||
Color::White
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
f.render_widget(
|
||||
|
|
Loading…
Reference in a new issue