Fix budget amount not saved when clicking on another budget cell (#1965)

* Fix budget amount not saved when clicking on another budget cell

* Release notes
This commit is contained in:
Joel Jeremy Marquez
2023-11-25 16:52:38 -08:00
committed by GitHub
parent 67b9143dfc
commit 0d1b962b2f
3 changed files with 50 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
import React, { type MutableRefObject, useRef, useState } from 'react';
import React, { type Ref, useRef, useState, useEffect } from 'react';
import evalArithmetic from 'loot-core/src/shared/arithmetic';
import { amountToInteger } from 'loot-core/src/shared/util';
@@ -14,7 +14,7 @@ import useFormat from '../spreadsheet/useFormat';
type AmountInputProps = {
id?: string;
inputRef?: MutableRefObject<HTMLInputElement>;
inputRef?: Ref<HTMLInputElement>;
initialValue: number;
zeroSign?: '-' | '+';
onChange?: (value: number) => void;
@@ -39,8 +39,11 @@ export function AmountInput({
let [negative, setNegative] = useState(
(initialValue === 0 && zeroSign === '-') || initialValue < 0,
);
let initialValueAbsolute = format(Math.abs(initialValue), 'financial');
let [value, setValue] = useState(initialValueAbsolute);
useEffect(() => setValue(initialValueAbsolute), [initialValueAbsolute]);
let buttonRef = useRef();
function onSwitch() {
@@ -64,6 +67,12 @@ export function AmountInput({
let ref = useRef<HTMLInputElement>();
let mergedRef = useMergedRefs<HTMLInputElement>(inputRef, ref);
useEffect(() => {
if (focused) {
ref.current?.focus();
}
}, [focused]);
function onInputAmountBlur(e) {
fireChange(value, negative);
if (!ref.current?.contains(e.relatedTarget)) {