Skip to content

A local-first personal finance app

add month picker component to automations UI (#8515)

Matt Fiddaman94f939121514
diff --git a/packages/component-library/package.json b/packages/component-library/package.json
index 60635f9f75..66c777f166 100644
--- a/packages/component-library/package.json
+++ b/packages/component-library/package.json
@@ -33,6 +33,7 @@
"./label": "./src/Label.tsx",
"./menu": "./src/Menu.tsx",
"./mode-button": "./src/ModeButton.tsx",
+ "./month-picker": "./src/MonthPicker.tsx",
"./paragraph": "./src/Paragraph.tsx",
"./popover": "./src/Popover.tsx",
"./select": "./src/Select.tsx",
diff --git a/packages/component-library/src/MonthPicker.stories.tsx b/packages/component-library/src/MonthPicker.stories.tsx
new file mode 100644
index 0000000000..cd20504cb2
--- /dev/null
+++ b/packages/component-library/src/MonthPicker.stories.tsx
@@ -0,0 +1,46 @@
+import { useState } from 'react';
+
+import type { Meta, StoryObj } from '@storybook/react-vite';
+import { fn } from 'storybook/test';
+
+import { MonthPicker } from './MonthPicker';
+
+const meta = {
+ title: 'Components/MonthPicker',
+ component: MonthPicker,
+ parameters: {
+ layout: 'centered',
+ },
+ args: {
+ value: '2024-06',
+ locale: 'en-US',
+ labels: { previous: 'Previous', next: 'Next' },
+ onChange: fn(),
+ },
+ tags: ['autodocs'],
+} satisfies Meta<typeof MonthPicker>;
+
+export default meta;
+
+type Story = StoryObj<typeof meta>;
+
+export const Default: Story = {
+ render: args => {
+ const [value, setValue] = useState(args.value);
+ return <MonthPicker {...args} value={value} onChange={setValue} />;
+ },
+};
+
+export const WithBounds: Story = {
+ args: {
+ minDate: '2023-04',
+ maxDate: '2025-09',
+ },
+};
+
+export const Empty: Story = {
+ args: {
+ value: '',
+ placeholder: 'Select month',
+ },
+};
diff --git a/packages/component-library/src/MonthPicker.tsx b/packages/component-library/src/MonthPicker.tsx
new file mode 100644
index 0000000000..488e3f9671
--- /dev/null
+++ b/packages/component-library/src/MonthPicker.tsx
@@ -0,0 +1,113 @@
+import { useRef, useState } from 'react';
+
+import { Button } from '#Button';
+import { Popover } from '#Popover';
+import type { CSSProperties } from '#styles';
+import { View } from '#View';
+
+import { MonthGrid } from './date-range-picker/MonthGrid';
+import { NavRow } from './date-range-picker/NavRow';
+import {
+ currentMonth,
+ formatDate,
+ getMonth,
+ getYear,
+} from './date-range-picker/util';
+
+export type MonthPickerLabels = {
+ previous: string;
+ next: string;
+};
+
+type MonthPickerProps = {
+ value: string;
+ minDate?: string;
+ maxDate?: string;
+ locale: string;
+ placeholder?: string;
+ labels: MonthPickerLabels;
+ id?: string;
+ style?: CSSProperties;
+ onChange: (month: string) => void;
+};
+
+// Far-past/far-future sentinels: sort before/after any real month string.
+const NO_MIN = '0001-01';
+const NO_MAX = '9999-12';
+
+export function MonthPicker({
+ value,
+ minDate,
+ maxDate,
+ locale,
+ placeholder,
+ labels,
+ id,
+ style,
+ onChange,
+}: MonthPickerProps) {
+ const month = value ? getMonth(value) : '';
+ const min = minDate ? getMonth(minDate) : NO_MIN;
+ const max = maxDate ? getMonth(maxDate) : NO_MAX;
+ const triggerRef = useRef<HTMLButtonElement>(null);
+ const [isOpen, setIsOpen] = useState(false);
+ const [viewYear, setViewYear] = useState(() =>
+ getYear(month || currentMonth()),
+ );
+
+ function openPopover() {
+ setViewYear(getYear(month || currentMonth()));
+ setIsOpen(true);
+ }
+
+ const label = month
+ ? formatDate(month, locale, { month: 'short', year: 'numeric' })
+ : (placeholder ?? '');
+
+ return (
+ <View>
+ <Button
+ id={id}
+ ref={triggerRef}
+ data-testid="month-picker-trigger"
+ onPress={() => (isOpen ? setIsOpen(false) : openPopover())}
+ style={{ justifyContent: 'flex-start', ...style }}
+ >
+ {label}
+ </Button>
+
+ <Popover
+ triggerRef={triggerRef}
+ placement="bottom start"
+ isOpen={isOpen}
+ onOpenChange={setIsOpen}
+ >
+ <View style={{ padding: 15, minWidth: 180 }}>
+ <NavRow
+ label={viewYear}
+ previousLabel={labels.previous}
+ nextLabel={labels.next}
+ canPrev={viewYear > getYear(min)}
+ canNext={viewYear < getYear(max)}
+ onPrev={() => setViewYear(String(Number(viewYear) - 1))}
+ onNext={() => setViewYear(String(Number(viewYear) + 1))}
+ />
+ <MonthGrid
+ year={viewYear}
+ rangeStart={month}
+ rangeEnd={month}
+ minMonth={min}
+ maxMonth={max}
+ locale={locale}
+ onSelect={next => {
+ setIsOpen(false);
+ if (next !== month) {
+ onChange(next);
+ }
+ }}
+ />
+ </View>
+ </Popover>
+ </View>
+ );
+}
diff --git a/packages/desktop-client/src/components/budget/goals/editor/BySaveAutomation.tsx b/packages/desktop-client/src/components/budget/goals/editor/BySaveAutomation.tsx
index faadaf21de..6ffdf0cdc2 100644
--- a/packages/desktop-client/src/components/budget/goals/editor/BySaveAutomation.tsx
+++ b/packages/desktop-client/src/components/budget/goals/editor/BySaveAutomation.tsx
@@ -1,7 +1,6 @@
import { useEffect, useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';
-import { useResponsive } from '@actual-app/components/hooks/useResponsive';
import { SvgInformationCircle } from '@actual-app/components/icons/v2';
import { Input } from '@actual-app/components/input';
import { Select } from '@actual-app/components/select';
@@ -20,56 +19,10 @@ import type { Action } from '#components/budget/goals/actions';
import { TWO_UP_FIELD_FLEX } from '#components/budget/goals/editor/fieldLayout';
import { FormField, FormLabel } from '#components/forms';
import { LabeledCheckbox } from '#components/forms/LabeledCheckbox';
-import {
- hideNativeDateIconClassName,
- InputField,
-} from '#components/mobile/MobileForms';
import { AmountInput } from '#components/util/AmountInput';
-import { GenericInput } from '#components/util/GenericInput';
+import { MonthInput } from '#components/util/MonthInput';
import { useFormat } from '#hooks/useFormat';
-type MonthFieldProps = {
- id: string;
- value: string;
- onChange: (month: string) => void;
-};
-
-// we can use a month picker for mobile because we use the native datepicker
-// no such luck on desktop (yet!)
-function MonthField({ id, value, onChange }: MonthFieldProps) {
- const { isNarrowWidth } = useResponsive();
- if (isNarrowWidth) {
- return (
- <InputField
- id={id}
- type="month"
- value={value}
- onChange={event => onChange(event.target.value)}
- className={hideNativeDateIconClassName}
- style={{
- width: '100%',
- marginLeft: 0,
- marginRight: 0,
- boxSizing: 'border-box',
- WebkitAppearance: 'none',
- appearance: 'none',
- }}
- />
- );
- }
- return (
- <GenericInput
- // remount when the stored month changes so the picker re-syncs
- // to day 01 instead of lingering on whatever day the user picked
- key={value}
- type="date"
- field="date"
- value={value ? `${value}-01` : ''}
- onChange={(next: string) => onChange(next ? next.slice(0, 7) : '')}
- />
- );
-}
-
type BySaveAutomationProps = {
template: ByTemplate | SpendTemplate;
dispatch: (action: Action) => void;
@@ -135,7 +88,7 @@ export const BySaveAutomation = ({
</FormField>
<FormField style={{ flex: TWO_UP_FIELD_FLEX }}>
<FormLabel title={t('Target month')} htmlFor="by-month-field" />
- <MonthField
+ <MonthInput
id="by-month-field"
value={template.month ?? ''}
onChange={month =>
@@ -285,7 +238,7 @@ export const BySaveAutomation = ({
title={t('Start spending in')}
htmlFor="by-spend-from-field"
/>
- <MonthField
+ <MonthInput
id="by-spend-from-field"
value={fromMonth ?? ''}
onChange={from =>
diff --git a/packages/desktop-client/src/components/util/MonthInput.tsx b/packages/desktop-client/src/components/util/MonthInput.tsx
new file mode 100644
index 0000000000..7bf870ef32
--- /dev/null
+++ b/packages/desktop-client/src/components/util/MonthInput.tsx
@@ -0,0 +1,52 @@
+import { useTranslation } from 'react-i18next';
+
+import { useResponsive } from '@actual-app/components/hooks/useResponsive';
+import { MonthPicker } from '@actual-app/components/month-picker';
+
+import {
+ hideNativeDateIconClassName,
+ InputField,
+} from '#components/mobile/MobileForms';
+import { useLanguage } from '#hooks/useLocale';
+
+type MonthInputProps = {
+ id: string;
+ value: string;
+ onChange: (month: string) => void;
+};
+
+// mobile keeps the native month input; desktop uses the custom month picker
+export function MonthInput({ id, value, onChange }: MonthInputProps) {
+ const { t } = useTranslation();
+ const language = useLanguage();
+ const { isNarrowWidth } = useResponsive();
+ if (isNarrowWidth) {
+ return (
+ <InputField
+ id={id}
+ type="month"
+ value={value}
+ onChange={event => onChange(event.target.value)}
+ className={hideNativeDateIconClassName}
+ style={{
+ width: '100%',
+ marginLeft: 0,
+ marginRight: 0,
+ boxSizing: 'border-box',
+ WebkitAppearance: 'none',
+ appearance: 'none',
+ }}
+ />
+ );
+ }
+ return (
+ <MonthPicker
+ id={id}
+ value={value}
+ locale={language}
+ placeholder={t('Select month')}
+ labels={{ previous: t('Previous'), next: t('Next') }}
+ onChange={onChange}
+ />
+ );
+}
diff --git a/upcoming-release-notes/automation-month-picker.md b/upcoming-release-notes/automation-month-picker.md
new file mode 100644
index 0000000000..dee5ec2df2
--- /dev/null
+++ b/upcoming-release-notes/automation-month-picker.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [matt-fidd]
+---
+
+Use a month picker instead of a full date picker when choosing months in budget automations