A local-first personal finance app
Allow-return-to-trigger-default-actions (#4912)
Samuel Barnes351e252129d7
diff --git a/packages/component-library/package.json b/packages/component-library/package.jsonindex 01bafe442a..a3ba9e3ad7 100644--- a/packages/component-library/package.json+++ b/packages/component-library/package.json@@ -15,7 +15,8 @@"@svgr/cli": "^8.1.0","@types/react": "^19.1.0","react": "19.1.0",- "react-dom": "19.1.0"+ "react-dom": "19.1.0",+ "vitest": "^3.0.2"},"exports": {"./hooks/*": "./src/hooks/*.ts",@@ -50,6 +51,8 @@"./view": "./src/View.tsx"},"scripts": {- "generate:icons": "rm src/icons/*/*.tsx; cd src/icons && svgr --template template.ts --index-template index-template.ts --typescript --expand-props start -d . ."+ "generate:icons": "rm src/icons/*/*.tsx; cd src/icons && svgr --template template.ts --index-template index-template.ts --typescript --expand-props start -d . .",+ "test": "npm-run-all -cp 'test:*'",+ "test:web": "ENV=web vitest -c vitest.web.config.ts"}}diff --git a/packages/component-library/src/InitialFocus.ts b/packages/component-library/src/InitialFocus.tsindex c7ee965b08..81b01e8a23 100644--- a/packages/component-library/src/InitialFocus.ts+++ b/packages/component-library/src/InitialFocus.ts@@ -1,19 +1,32 @@import {- type ReactElement,- type Ref,+ Children,cloneElement,+ isValidElement,+ type ReactElement,+ Ref,useEffect,useRef,} from 'react';+type FocusableElement = HTMLElement;+type InitialFocusProps = {+ /**+ * The child element to focus when the component mounts. This can be either a single React element or a function that returns a React element.+ */children:- | ReactElement<{ inputRef: Ref<HTMLInputElement> }>- | ((node: Ref<HTMLInputElement>) => ReactElement);+ | ReactElement<{ ref: Ref<HTMLElement> }>+ | ((node: Ref<HTMLElement>) => ReactElement);};+/**+ * InitialFocus sets focus on its child element+ * when it mounts.+ * @param {Object} props - The component props.+ * @param {ReactElement | function} props.children - A single React element or a function that returns a React element.+ */export function InitialFocus({ children }: InitialFocusProps) {- const node = useRef<HTMLInputElement>(null);+ const node = useRef<FocusableElement>(null);useEffect(() => {if (node.current) {@@ -23,7 +36,12 @@ export function InitialFocus({ children }: InitialFocusProps) {setTimeout(() => {if (node.current) {node.current.focus();- node.current.setSelectionRange(0, 10000);+ if (+ node.current instanceof HTMLInputElement ||+ node.current instanceof HTMLTextAreaElement+ ) {+ node.current.setSelectionRange(0, 10000);+ }}}, 0);}@@ -32,5 +50,12 @@ export function InitialFocus({ children }: InitialFocusProps) {if (typeof children === 'function') {return children(node);}- return cloneElement(children, { inputRef: node });++ const child = Children.only(children);+ if (isValidElement(child)) {+ return cloneElement(child, { ref: node });+ }+ throw new Error(+ 'InitialFocus expects a single valid React element as its child.',+ );}diff --git a/packages/component-library/src/InitialFocus.web.test.tsx b/packages/component-library/src/InitialFocus.web.test.tsxnew file mode 100644index 0000000000..3ce1df74f8--- /dev/null+++ b/packages/component-library/src/InitialFocus.web.test.tsx@@ -0,0 +1,117 @@+import * as React from 'react';+import { forwardRef, Ref } from 'react';++import { render } from '@testing-library/react';++import { InitialFocus } from './InitialFocus';+import { View } from './View';++describe('InitialFocus', () => {+ it('should focus a text input', async () => {+ const component = render(+ <View>+ <InitialFocus>+ <input type="text" title="focused" />+ </InitialFocus>+ <input type="text" title="unfocused" />+ </View>,+ );++ // This is needed bc of the `setTimeout` in the `InitialFocus` component.+ await new Promise(resolve => setTimeout(resolve, 0));++ const input = component.getByTitle('focused') as HTMLInputElement;+ const unfocusedInput = component.getByTitle(+ 'unfocused',+ ) as HTMLInputElement;+ expect(document.activeElement).toBe(input);+ expect(document.activeElement).not.toBe(unfocusedInput);+ });++ it('should focus a textarea', async () => {+ const component = render(+ <View>+ <InitialFocus>+ <textarea title="focused" />+ </InitialFocus>+ <textarea title="unfocused" />+ </View>,+ );++ // This is needed bc of the `setTimeout` in the `InitialFocus` component.+ await new Promise(resolve => setTimeout(resolve, 0));++ const textarea = component.getByTitle('focused') as HTMLTextAreaElement;+ const unfocusedTextarea = component.getByTitle(+ 'unfocused',+ ) as HTMLTextAreaElement;+ expect(document.activeElement).toBe(textarea);+ expect(document.activeElement).not.toBe(unfocusedTextarea);+ });++ it('should select text in an input', async () => {+ const component = render(+ <View>+ <InitialFocus>+ <input type="text" title="focused" defaultValue="Hello World" />+ </InitialFocus>+ <input type="text" title="unfocused" />+ </View>,+ );++ // This is needed bc of the `setTimeout` in the `InitialFocus` component.+ await new Promise(resolve => setTimeout(resolve, 0));++ const input = component.getByTitle('focused') as HTMLInputElement;+ expect(document.activeElement).toBe(input);+ expect(input.selectionStart).toBe(0);+ expect(input.selectionEnd).toBe(11); // Length of "Hello World"+ });++ it('should focus a button', async () => {+ const component = render(+ <View>+ <InitialFocus>+ <button title="focused">Click me</button>+ </InitialFocus>+ <button title="unfocused">Do not click me</button>+ </View>,+ );++ // This is needed bc of the `setTimeout` in the `InitialFocus` component.+ await new Promise(resolve => setTimeout(resolve, 0));++ const button = component.getByTitle('focused') as HTMLButtonElement;+ const unfocusedButton = component.getByTitle(+ 'unfocused',+ ) as HTMLButtonElement;+ expect(document.activeElement).toBe(button);+ expect(document.activeElement).not.toBe(unfocusedButton);+ });++ it('should focus a custom component with ref forwarding', async () => {+ const CustomInput = forwardRef<HTMLInputElement>((props, ref) => (+ <input type="text" ref={ref} {...props} title="focused" />+ ));+ CustomInput.displayName = 'CustomInput';++ const component = render(+ <View>+ <InitialFocus>+ {node => <CustomInput ref={node as Ref<HTMLInputElement>} />}+ </InitialFocus>+ <input type="text" title="unfocused" />+ </View>,+ );++ // This is needed bc of the `setTimeout` in the `InitialFocus` component.+ await new Promise(resolve => setTimeout(resolve, 0));++ const input = component.getByTitle('focused') as HTMLInputElement;+ const unfocusedInput = component.getByTitle(+ 'unfocused',+ ) as HTMLInputElement;+ expect(document.activeElement).toBe(input);+ expect(document.activeElement).not.toBe(unfocusedInput);+ });+});diff --git a/packages/component-library/vitest.web.config.ts b/packages/component-library/vitest.web.config.tsnew file mode 100644index 0000000000..1b2a9ee7c7--- /dev/null+++ b/packages/component-library/vitest.web.config.ts@@ -0,0 +1,35 @@+import path from 'path';++import peggyLoader from 'vite-plugin-peggy-loader';+import { defineConfig } from 'vitest/config';++const resolveExtensions = [+ '.testing.ts',+ '.web.ts',+ '.mjs',+ '.js',+ '.mts',+ '.ts',+ '.jsx',+ '.tsx',+ '.json',+ '.wasm',+];++export default defineConfig({+ test: {+ environment: 'jsdom',+ globals: true,+ include: ['src/**/*.web.test.(js|jsx|ts|tsx)'],+ },+ resolve: {+ alias: [+ {+ find: /^@actual-app\/crdt(\/.*)?$/,+ replacement: path.resolve('../../../crdt/src$1'),+ },+ ],+ extensions: resolveExtensions,+ },+ plugins: [peggyLoader()],+});diff --git a/packages/desktop-client/src/components/accounts/Header.tsx b/packages/desktop-client/src/components/accounts/Header.tsxindex d7bdbdf546..ac70c442b1 100644--- a/packages/desktop-client/src/components/accounts/Header.tsx+++ b/packages/desktop-client/src/components/accounts/Header.tsx@@ -1,9 +1,9 @@import React, {- useState,- useRef,+ type ComponentProps,Fragment,type ReactNode,- type ComponentProps,+ useRef,+ useState,} from 'react';import { Dialog, DialogTrigger } from 'react-aria-components';import { useHotkeys } from 'react-hotkeys-hook';@@ -50,7 +50,7 @@ import { SelectedTransactionsButton } from '../transactions/SelectedTransactionsimport { type TableRef } from './Account';import { Balances } from './Balance';-import { ReconcilingMessage, ReconcileMenu } from './Reconcile';+import { ReconcileMenu, ReconcilingMessage } from './Reconcile';import { useLocale } from '@desktop-client/hooks/useLocale';import { useLocalPref } from '@desktop-client/hooks/useLocalPref';diff --git a/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx b/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsxindex cecf9de3bb..ecdd82637e 100644--- a/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx+++ b/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx@@ -1,4 +1,4 @@-import React, { useMemo, useState } from 'react';+import React, { type Ref, useMemo, useState } from 'react';import { Trans, useTranslation } from 'react-i18next';import { Button } from '@actual-app/components/button';@@ -47,6 +47,7 @@ export function CoverMenu({}onClose();}+return (<View style={{ padding: 10 }}><View style={{ marginBottom: 5 }}>@@ -61,7 +62,7 @@ export function CoverMenu({openOnFocus={true}onSelect={(id: string | undefined) => setFromCategoryId(id || null)}inputProps={{- inputRef: node,+ inputRef: node as Ref<HTMLInputElement>,onEnter: event => !event.defaultPrevented && submit(),placeholder: t('(none)'),}}diff --git a/packages/desktop-client/src/components/sidebar/Account.tsx b/packages/desktop-client/src/components/sidebar/Account.tsxindex 22cb3befee..e775331210 100644--- a/packages/desktop-client/src/components/sidebar/Account.tsx+++ b/packages/desktop-client/src/components/sidebar/Account.tsx@@ -22,13 +22,13 @@ import { useDispatch } from '../../redux';import { Link } from '../common/Link';import { Notes } from '../Notes';import {- useDraggable,- useDroppable,DropHighlight,type OnDragChangeCallback,type OnDropCallback,+ useDraggable,+ useDroppable,} from '../sort';-import { type SheetFields, type Binding } from '../spreadsheet';+import { type Binding, type SheetFields } from '../spreadsheet';import { CellValue } from '../spreadsheet/CellValue';import { useContextMenu } from '@desktop-client/hooks/useContextMenu';diff --git a/upcoming-release-notes/4912.md b/upcoming-release-notes/4912.mdnew file mode 100644index 0000000000..e363cab9a8--- /dev/null+++ b/upcoming-release-notes/4912.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [ sambobbarnes ]+---++Fix autofocus of `Button` elements when enclosed in the `InitialFocus` componentdiff --git a/yarn.lock b/yarn.lockindex 4343134d9a..3c3f69dfed 100644--- a/yarn.lock+++ b/yarn.lock@@ -46,6 +46,7 @@ __metadata:react-aria-components: "npm:^1.7.1"react-dom: "npm:19.1.0"usehooks-ts: "npm:^3.1.1"+ vitest: "npm:^3.0.2"peerDependencies:react: ">=18.2"react-dom: ">=18.2"