Handle negative amount input

This commit is contained in:
Pierre HUBERT 2025-04-21 16:04:08 +02:00
parent 09e44da46e
commit 3fe3e20f51
2 changed files with 15 additions and 5 deletions

View File

@ -2,7 +2,7 @@ import { IconButton, Tooltip, Typography } from "@mui/material";
import { Account } from "../api/AccountApi"; import { Account } from "../api/AccountApi";
import { DateInput } from "./forms/DateInput"; import { DateInput } from "./forms/DateInput";
import { time } from "../utils/DateUtils"; import { time } from "../utils/DateUtils";
import React from "react"; import React, { useRef } from "react";
import { TextInput } from "./forms/TextInput"; import { TextInput } from "./forms/TextInput";
import { ServerApi } from "../api/ServerApi"; import { ServerApi } from "../api/ServerApi";
import AddIcon from "@mui/icons-material/Add"; import AddIcon from "@mui/icons-material/Add";
@ -17,6 +17,8 @@ export function NewMovementWidget(p: {
const snackbar = useSnackbar(); const snackbar = useSnackbar();
const alert = useAlert(); const alert = useAlert();
const dateInputRef = useRef<HTMLInputElement>(null);
const [movTime, setMovTime] = React.useState<number | undefined>(time()); const [movTime, setMovTime] = React.useState<number | undefined>(time());
const [label, setLabel] = React.useState<string | undefined>(""); const [label, setLabel] = React.useState<string | undefined>("");
const [amount, setAmount] = React.useState<number | undefined>(0); const [amount, setAmount] = React.useState<number | undefined>(0);
@ -49,6 +51,9 @@ export function NewMovementWidget(p: {
setLabel(""); setLabel("");
setAmount(0); setAmount(0);
// Give back focus to date input
dateInputRef.current?.querySelector("input")?.focus();
} catch (e) { } catch (e) {
console.error(`Failed to create movement!`, e); console.error(`Failed to create movement!`, e);
alert(`Failed to create movement! ${e}`); alert(`Failed to create movement! ${e}`);
@ -63,6 +68,7 @@ export function NewMovementWidget(p: {
<Typography style={{ marginRight: "10px" }}>New movement</Typography> <Typography style={{ marginRight: "10px" }}>New movement</Typography>
&nbsp; &nbsp;
<DateInput <DateInput
ref={dateInputRef}
autoFocus autoFocus
editable editable
style={{ flex: 1, maxWidth: "140px" }} style={{ flex: 1, maxWidth: "140px" }}
@ -81,11 +87,11 @@ export function NewMovementWidget(p: {
&nbsp; &nbsp;
<TextInput <TextInput
editable editable
type="number" type="text"
placeholder="Amount" placeholder="Amount"
style={{ flex: 1, maxWidth: "110px" }} style={{ flex: 1, maxWidth: "110px" }}
value={String(amount)} value={Number.isNaN(amount) ? "-" : String(amount)}
onValueChange={(a) => setAmount(Number(a))} onValueChange={(a) => setAmount(a === "-" ? NaN : Number(a))}
/> />
<Tooltip title="Add new movement"> <Tooltip title="Add new movement">
<IconButton onClick={submit}> <IconButton onClick={submit}>

View File

@ -2,6 +2,7 @@ import { DatePicker } from "@mui/x-date-pickers";
import { dateToTime, timeToDate } from "../../utils/DateUtils"; import { dateToTime, timeToDate } from "../../utils/DateUtils";
export function DateInput(p: { export function DateInput(p: {
ref?: React.Ref<HTMLInputElement>;
editable?: boolean; editable?: boolean;
autoFocus?: boolean; autoFocus?: boolean;
label?: string; label?: string;
@ -14,7 +15,10 @@ export function DateInput(p: {
autoFocus={p.autoFocus} autoFocus={p.autoFocus}
readOnly={p.editable === false} readOnly={p.editable === false}
label={p.label} label={p.label}
slotProps={{ textField: { variant: "standard", style: p.style } }} slotProps={{
field: { ref: p.ref },
textField: { variant: "standard", style: p.style },
}}
value={timeToDate(p.value)} value={timeToDate(p.value)}
onChange={(v) => { onChange={(v) => {
try { try {