A local-first personal finance app
automations UI: reuse percentInput for adjustments (#7905)
Matt Fiddaman2879c86f8b03
diff --git a/packages/desktop-client/src/components/budget/goals/editor/AmountAdjustment.tsx b/packages/desktop-client/src/components/budget/goals/editor/AmountAdjustment.tsxindex 9597f30950..46720b09cd 100644--- a/packages/desktop-client/src/components/budget/goals/editor/AmountAdjustment.tsx+++ b/packages/desktop-client/src/components/budget/goals/editor/AmountAdjustment.tsx@@ -1,7 +1,5 @@-import { useEffect, useState } from 'react';import { useTranslation } from 'react-i18next';-import { Input } from '@actual-app/components/input';import { Select } from '@actual-app/components/select';import { SpaceBetween } from '@actual-app/components/space-between';import { amountToInteger, integerToAmount } from '@actual-app/core/shared/util';@@ -14,6 +12,7 @@ import { updateTemplate } from '#components/budget/goals/actions';import type { Action } from '#components/budget/goals/actions';import { FormField, FormLabel } from '#components/forms';import { AmountInput } from '#components/util/AmountInput';+import { PercentInput } from '#components/util/PercentInput';import { useFormat } from '#hooks/useFormat';type AdjustableTemplate = ScheduleTemplate | AverageTemplate;@@ -24,6 +23,8 @@ type Direction = 'increase' | 'decrease';// Seeded when an adjustment is first switched on.const DEFAULT_MAGNITUDE = 10;+const MAX_PERCENT = 1000;+type AmountAdjustmentProps = {template: AdjustableTemplate;dispatch: (action: Action) => void;@@ -47,13 +48,6 @@ export const AmountAdjustment = ({const increasing = adjustment >= 0;const magnitude = Math.abs(adjustment);- const [rawMagnitude, setRawMagnitude] = useState(String(magnitude));- // Resync when a different automation row is selected (the component- // instance is reused across rows).- useEffect(() => {- setRawMagnitude(String(magnitude));- }, [magnitude]);-const apply = (next: number | undefined,type: AdjustmentType | undefined,@@ -91,13 +85,6 @@ export const AmountAdjustment = ({apply(direction === 'decrease' ? -magnitude : magnitude, 'percent');};- const commitMagnitude = () => {- const parsed = Number(rawMagnitude);- const size = Number.isFinite(parsed) ? Math.max(0, parsed) : 0;- setRawMagnitude(String(size));- apply(increasing ? size : -size, 'percent');- };-return (<SpaceBetween gap={50} style={{ marginTop: 10 }}><FormField style={{ flex: 1 }}>@@ -143,13 +130,14 @@ export const AmountAdjustment = ({]}style={{ width: 150 }}/>- <Input+ <PercentInputid="adjustment-amount-field"- inputMode="decimal"- style={{ width: 120 }}- value={rawMagnitude}- onChangeValue={setRawMagnitude}- onBlur={commitMagnitude}+ max={MAX_PERCENT}+ value={magnitude}+ onUpdatePercent={percent =>+ apply(increasing ? percent : -percent, 'percent')+ }+ style={{ flex: 'none', width: 120 }}/></>))}diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/AutomationEditorPane.tsx b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/AutomationEditorPane.tsxindex 591106fdf0..6aba9f5f55 100644--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/AutomationEditorPane.tsx+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/AutomationEditorPane.tsx@@ -236,6 +236,7 @@ export function AutomationEditorPane({</Text>)}<ActiveEditor+ key={active.id}state={state}dispatch={dispatch}schedules={schedules}@@ -249,6 +250,7 @@ export function AutomationEditorPane({{state.displayType === 'refill' && (<ActiveEditor+ key={active.id}state={state}dispatch={dispatch}schedules={schedules}diff --git a/packages/desktop-client/src/components/util/PercentInput.tsx b/packages/desktop-client/src/components/util/PercentInput.tsxindex 25f581dd0a..8a9761bbd6 100644--- a/packages/desktop-client/src/components/util/PercentInput.tsx+++ b/packages/desktop-client/src/components/util/PercentInput.tsx@@ -18,9 +18,11 @@ type PercentInputProps = {style?: CSSProperties;focused?: boolean;disabled?: boolean;+ max?: number;};-const clampToPercent = (value: number) => Math.max(Math.min(value, 100), 0);+const clampToPercent = (value: number, max: number) =>+ Math.max(Math.min(value, max), 0);export function PercentInput({id,@@ -33,19 +35,20 @@ export function PercentInput({style,focused,disabled = false,+ max = 100,}: PercentInputProps) {const format = useFormat();const [value, setValue] = useState(() =>- format(clampToPercent(initialValue), 'percentage'),+ format(clampToPercent(initialValue, max), 'percentage'),);useEffect(() => {- const clampedInitialValue = clampToPercent(initialValue);+ const clampedInitialValue = clampToPercent(initialValue, max);if (clampedInitialValue !== initialValue) {setValue(format(clampedInitialValue, 'percentage'));onUpdatePercent?.(clampedInitialValue);}- }, [initialValue, onUpdatePercent, format]);+ }, [initialValue, max, onUpdatePercent, format]);const ref = useRef<HTMLInputElement>(null);const mergedRef = useMergedRefs<HTMLInputElement>(inputRef, ref);@@ -84,6 +87,7 @@ export function PercentInput({function fireUpdate() {const clampedValue = clampToPercent(evalArithmetic(value.replace('%', ''), 0) ?? 0,+ max,);onUpdatePercent?.(clampedValue);onInputTextChange(String(clampedValue));diff --git a/upcoming-release-notes/7905.md b/upcoming-release-notes/7905.mdnew file mode 100644index 0000000000..f08b85615c--- /dev/null+++ b/upcoming-release-notes/7905.md@@ -0,0 +1,6 @@+---+category: Bugfixes+authors: [matt-fidd]+---++Make percentage inputs in budget automations more consistent