Skip to content

A local-first personal finance app

:chart_with_upwards_trend: Monte carlo input focus fix (#9164)

Michael Clark832f43da6c85
diff --git a/packages/desktop-client/src/components/reports/reports/monte-carlo/MonteCarloNumberInput.tsx b/packages/desktop-client/src/components/reports/reports/monte-carlo/MonteCarloNumberInput.tsx
index ad305bb19f..780e74c594 100644
--- a/packages/desktop-client/src/components/reports/reports/monte-carlo/MonteCarloNumberInput.tsx
+++ b/packages/desktop-client/src/components/reports/reports/monte-carlo/MonteCarloNumberInput.tsx
@@ -53,6 +53,11 @@ export function MonteCarloNumberInput({
style,
}: MonteCarloNumberInputProps) {
const inputRef = useRef<HTMLInputElement>(null);
+ // A mouse-down is in progress; when that is what focuses the field,
+ // the first mouse-up is cancelled so the click keeps the whole value
+ // selected (see onMouseUp). Keyboard focus leaves clicks alone
+ const isPointerDown = useRef(false);
+ const shouldKeepSelection = useRef(false);
const [text, setText] = useState(() => toDisplayText(value, scale));
const [isFocused, setIsFocused] = useState(false);
@@ -107,11 +112,26 @@ export function MonteCarloNumberInput({
aria-label={ariaLabel}
style={style}
onChangeValue={setText}
- onFocus={() => {
+ onFocus={event => {
setIsFocused(true);
// Highlight the whole value so typing replaces it, matching the
- // currency inputs
- setTimeout(() => inputRef.current?.select(), 0);
+ // currency inputs. Synchronous, so nothing runs after the user
+ // has moved on (a deferred select() would steal focus back)
+ event.currentTarget.select();
+ shouldKeepSelection.current = isPointerDown.current;
+ }}
+ onMouseDown={() => {
+ isPointerDown.current = true;
+ }}
+ onMouseUp={event => {
+ // The browser's mouse-up would otherwise replace the selection
+ // made on focus with a caret at the click point; later clicks in
+ // an already-focused field place the caret as normal
+ if (shouldKeepSelection.current) {
+ event.preventDefault();
+ }
+ shouldKeepSelection.current = false;
+ isPointerDown.current = false;
}}
onBlur={event => {
setIsFocused(false);
diff --git a/packages/desktop-client/src/components/util/FinancialInput.tsx b/packages/desktop-client/src/components/util/FinancialInput.tsx
index 0b92a0ddb7..30f9d37e76 100644
--- a/packages/desktop-client/src/components/util/FinancialInput.tsx
+++ b/packages/desktop-client/src/components/util/FinancialInput.tsx
@@ -1,5 +1,5 @@
-import { useEffect, useRef, useState } from 'react';
-import type { FocusEvent } from 'react';
+import { useEffect, useLayoutEffect, useRef, useState } from 'react';
+import type { FocusEvent, MouseEvent } from 'react';
import { useResponsive } from '@actual-app/components/hooks/useResponsive';
import { Input } from '@actual-app/components/input';
@@ -35,6 +35,8 @@ export function FinancialInput({
onChangeValue,
onBlur,
onFocus,
+ onMouseDown,
+ onMouseUp,
onEnter,
className,
...restProps
@@ -44,6 +46,11 @@ export function FinancialInput({
// Like PrivacyFilter, redaction is desktop-only for now
const { isNarrowWidth } = useResponsive();
const inputRef = useRef<HTMLInputElement>(null);
+ // A mouse-down is in progress; when that is what focuses the field,
+ // the first mouse-up is cancelled so the click keeps the whole value
+ // selected (see handleMouseUp). Keyboard focus leaves clicks alone
+ const isPointerDown = useRef(false);
+ const shouldKeepSelection = useRef(false);
const [internalValue, setInternalValue] = useState(() =>
format(integerValue, 'financial'),
);
@@ -55,26 +62,60 @@ export function FinancialInput({
}
}, [integerValue, format, isFocused]);
+ // Highlight the whole value on focus so typing replaces it. Focusing
+ // swaps the text to its edit form, which would collapse a selection
+ // made in the focus handler, so select once React has committed the
+ // swap - still synchronously within the focus event, so nothing runs
+ // after the user has moved on (a deferred select() would steal focus
+ // back and set two fields bouncing focus between each other)
+ useLayoutEffect(() => {
+ if (isFocused) {
+ inputRef.current?.select();
+ }
+ }, [isFocused]);
+
const handleFocus = (e: FocusEvent<HTMLInputElement>) => {
setIsFocused(true);
setInternalValue(format.forEdit(integerValue));
- setTimeout(() => {
- inputRef.current?.select();
- }, 0);
+ // The selection itself is made in the layout effect above
+ shouldKeepSelection.current = isPointerDown.current;
onFocus?.(e);
};
+ const handleMouseDown = (e: MouseEvent<HTMLInputElement>) => {
+ isPointerDown.current = true;
+ onMouseDown?.(e);
+ };
+
+ const handleMouseUp = (e: MouseEvent<HTMLInputElement>) => {
+ // The browser's mouse-up would otherwise replace the selection made
+ // on focus with a caret at the click point; later clicks in an
+ // already-focused field place the caret as normal
+ if (shouldKeepSelection.current) {
+ e.preventDefault();
+ }
+ shouldKeepSelection.current = false;
+ isPointerDown.current = false;
+ onMouseUp?.(e);
+ };
+
const handleBlur = (e: FocusEvent<HTMLInputElement>) => {
setIsFocused(false);
const finalInteger = format.fromEdit(internalValue, 0) ?? 0;
- onUpdate?.(finalInteger);
+ // Only report a real change: tabbing through an untouched field is
+ // not an update, and callers may do expensive work on one
+ if (finalInteger !== integerValue) {
+ onUpdate?.(finalInteger);
+ }
setInternalValue(format(finalInteger, 'financial'));
onBlur?.(e);
};
const handleEnter = (stringValue: string) => {
const finalInteger = format.fromEdit(stringValue, 0) ?? 0;
- onUpdate?.(finalInteger);
+ if (finalInteger !== integerValue) {
+ onUpdate?.(finalInteger);
+ }
onEnter?.(finalInteger);
};
@@ -118,6 +159,8 @@ export function FinancialInput({
style={{ ...restProps.style, ...styles.tnum }}
onChangeValue={handleChange}
onFocus={handleFocus}
+ onMouseDown={handleMouseDown}
+ onMouseUp={handleMouseUp}
onBlur={handleBlur}
onEnter={handleEnter}
/>
diff --git a/upcoming-release-notes/fix-number-input-focus-bounce.md b/upcoming-release-notes/fix-number-input-focus-bounce.md
new file mode 100644
index 0000000000..815bea8aac
--- /dev/null
+++ b/upcoming-release-notes/fix-number-input-focus-bounce.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [MikesGlitch]
+---
+
+Monte carlo - Fix amount and number fields bouncing focus when tabbing through them quickly