From 3fe3e20f51d76bfa560bd2422f274d88b5066134 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 21 Apr 2025 16:04:08 +0200 Subject: [PATCH] Handle negative amount input --- moneymgr_web/src/widgets/NewMovementWidget.tsx | 14 ++++++++++---- moneymgr_web/src/widgets/forms/DateInput.tsx | 6 +++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/moneymgr_web/src/widgets/NewMovementWidget.tsx b/moneymgr_web/src/widgets/NewMovementWidget.tsx index 27e2a6b..23e714c 100644 --- a/moneymgr_web/src/widgets/NewMovementWidget.tsx +++ b/moneymgr_web/src/widgets/NewMovementWidget.tsx @@ -2,7 +2,7 @@ import { IconButton, Tooltip, Typography } from "@mui/material"; import { Account } from "../api/AccountApi"; import { DateInput } from "./forms/DateInput"; import { time } from "../utils/DateUtils"; -import React from "react"; +import React, { useRef } from "react"; import { TextInput } from "./forms/TextInput"; import { ServerApi } from "../api/ServerApi"; import AddIcon from "@mui/icons-material/Add"; @@ -17,6 +17,8 @@ export function NewMovementWidget(p: { const snackbar = useSnackbar(); const alert = useAlert(); + const dateInputRef = useRef(null); + const [movTime, setMovTime] = React.useState(time()); const [label, setLabel] = React.useState(""); const [amount, setAmount] = React.useState(0); @@ -49,6 +51,9 @@ export function NewMovementWidget(p: { setLabel(""); setAmount(0); + + // Give back focus to date input + dateInputRef.current?.querySelector("input")?.focus(); } catch (e) { console.error(`Failed to create movement!`, e); alert(`Failed to create movement! ${e}`); @@ -63,6 +68,7 @@ export function NewMovementWidget(p: { New movement   setAmount(Number(a))} + value={Number.isNaN(amount) ? "-" : String(amount)} + onValueChange={(a) => setAmount(a === "-" ? NaN : Number(a))} /> diff --git a/moneymgr_web/src/widgets/forms/DateInput.tsx b/moneymgr_web/src/widgets/forms/DateInput.tsx index afded24..f884f6a 100644 --- a/moneymgr_web/src/widgets/forms/DateInput.tsx +++ b/moneymgr_web/src/widgets/forms/DateInput.tsx @@ -2,6 +2,7 @@ import { DatePicker } from "@mui/x-date-pickers"; import { dateToTime, timeToDate } from "../../utils/DateUtils"; export function DateInput(p: { + ref?: React.Ref; editable?: boolean; autoFocus?: boolean; label?: string; @@ -14,7 +15,10 @@ export function DateInput(p: { autoFocus={p.autoFocus} readOnly={p.editable === false} 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)} onChange={(v) => { try {