Skip to content

A local-first personal finance app

fix datepicker month input validation to allow mm/yyyy on resubmission (#5356)

Matt Fiddamanf8dd4897e7d5
diff --git a/packages/desktop-client/src/components/filters/FiltersMenu.jsx b/packages/desktop-client/src/components/filters/FiltersMenu.jsx
index ac7e9082f5..6835af0503 100644
--- a/packages/desktop-client/src/components/filters/FiltersMenu.jsx
+++ b/packages/desktop-client/src/components/filters/FiltersMenu.jsx
@@ -87,6 +87,18 @@ function ConfigureField({
ops = ['is'];
}
+ const formattedValue = useMemo(() => {
+ if (
+ field === 'date' &&
+ subfield === 'month' &&
+ /^\d{4}-\d{2}$/.test(value)
+ ) {
+ const [year, month] = value.split('-');
+ return `${month}/${year}`;
+ }
+ return value;
+ }, [value, field, subfield]);
+
return (
<FocusScope>
<View style={{ marginBottom: 10 }}>
@@ -231,7 +243,7 @@ function ConfigureField({
? 'string'
: type
}
- value={value}
+ value={formattedValue}
multi={op === 'oneOf' || op === 'notOneOf'}
op={op}
style={{ marginTop: 10 }}
@@ -441,6 +453,9 @@ export function FilterButton({ onApply, compact, hover, exclude }) {
}
export function FilterEditor({ field, op, value, options, onSave, onClose }) {
+ const dateFormat = useDateFormat() || 'MM/dd/yyyy';
+ const { t } = useTranslation();
+
const [state, dispatch] = useReducer(
(state, action) => {
switch (action.type) {
@@ -464,6 +479,31 @@ export function FilterEditor({ field, op, value, options, onSave, onClose }) {
dispatch={dispatch}
onApply={cond => {
cond = unparse({ ...cond, type: FIELD_TYPES.get(cond.field) });
+
+ if (cond.type === 'date' && cond.options) {
+ if (cond.options.month && !/\d{4}-\d{2}/.test(cond.value)) {
+ const date = parseDate(
+ cond.value,
+ getMonthYearFormat(dateFormat),
+ new Date(),
+ );
+ if (isDateValid(date)) {
+ cond.value = formatDate(date, 'yyyy-MM');
+ } else {
+ alert(t('Invalid date format'));
+ return;
+ }
+ } else if (cond.options.year) {
+ const date = parseDate(cond.value, 'yyyy', new Date());
+ if (isDateValid(date)) {
+ cond.value = formatDate(date, 'yyyy');
+ } else {
+ alert(t('Invalid date format'));
+ return;
+ }
+ }
+ }
+
onSave(cond);
onClose();
}}
diff --git a/upcoming-release-notes/5356.md b/upcoming-release-notes/5356.md
new file mode 100644
index 0000000000..044767203b
--- /dev/null
+++ b/upcoming-release-notes/5356.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [matt-fidd]
+---
+
+Fix datepicker month input validation to allow mm/yyyy