Skip to content

A local-first personal finance app

Convert EditFieldModal.jsx to tsx (#3865)

Joel Jeremy Marquez590ac1f95eff
diff --git a/packages/desktop-client/src/components/modals/EditFieldModal.jsx b/packages/desktop-client/src/components/modals/EditFieldModal.tsx
similarity index 84%
rename from packages/desktop-client/src/components/modals/EditFieldModal.jsx
rename to packages/desktop-client/src/components/modals/EditFieldModal.tsx
index 017a6443c8..9754113021 100644
--- a/packages/desktop-client/src/components/modals/EditFieldModal.jsx
+++ b/packages/desktop-client/src/components/modals/EditFieldModal.tsx
@@ -1,4 +1,9 @@
-import React, { useState } from 'react';
+import React, {
+ type CSSProperties,
+ type ReactNode,
+ useRef,
+ useState,
+} from 'react';
import { parseISO, format as formatDate, parse as parseDate } from 'date-fns';
import { t } from 'i18next';
@@ -16,19 +21,43 @@ import { SectionLabel } from '../forms';
import { useResponsive } from '../responsive/ResponsiveProvider';
import { DateSelect } from '../select/DateSelect';
-export function EditFieldModal({ name, onSubmit, onClose }) {
+const itemStyle: CSSProperties = {
+ fontSize: 17,
+ fontWeight: 400,
+ paddingTop: 8,
+ paddingBottom: 8,
+};
+
+type NoteAmendMode = 'replace' | 'prepend' | 'append';
+
+type EditFieldModalProps = {
+ name: string;
+ onSubmit: (
+ name: string,
+ value: string | number,
+ mode?: NoteAmendMode,
+ ) => void;
+ onClose: () => void;
+};
+
+export function EditFieldModal({
+ name,
+ onSubmit,
+ onClose,
+}: EditFieldModalProps) {
const dateFormat = useDateFormat() || 'MM/dd/yyyy';
+ const noteInputRef = useRef<HTMLInputElement | null>(null);
- function onSelectNote(value, mode) {
+ function onSelectNote(value: string, mode?: NoteAmendMode) {
if (value != null) {
onSubmit(name, value, mode);
}
}
- function onSelect(value) {
+ function onSelect(value: string | number) {
if (value != null) {
// Process the value if needed
- if (name === 'amount') {
+ if (name === 'amount' && typeof value === 'number') {
value = amountToInteger(value);
}
@@ -36,21 +65,16 @@ export function EditFieldModal({ name, onSubmit, onClose }) {
}
}
- const itemStyle = {
- fontSize: 17,
- fontWeight: 400,
- paddingTop: 8,
- paddingBottom: 8,
- };
-
const { isNarrowWidth } = useResponsive();
- let label, editor, minWidth;
- const inputStyle = {
- ':focus': { boxShadow: 0 },
+ let label: string;
+ let editor: (props: { close: () => void }) => ReactNode;
+ let minWidth: number | undefined;
+
+ const inputStyle: CSSProperties = {
...(isNarrowWidth && itemStyle),
};
- const [noteAmend, onChangeMode] = useState('replace');
+ const [noteAmend, onChangeMode] = useState<NoteAmendMode>('replace');
switch (name) {
case 'date':
@@ -88,7 +112,6 @@ export function EditFieldModal({ name, onSubmit, onClose }) {
}}
>
<Button
- selected={noteAmend === 'prepend'}
style={{
padding: '5px 10px',
width: '33.33%',
@@ -114,13 +137,12 @@ export function EditFieldModal({ name, onSubmit, onClose }) {
}}
onPress={() => {
onChangeMode('prepend');
- document.getElementById('noteInput').focus();
+ noteInputRef.current?.focus();
}}
>
{t('Prepend')}
</Button>
<Button
- selected={noteAmend === 'replace'}
style={{
padding: '5px 10px',
width: '33.34%',
@@ -146,13 +168,12 @@ export function EditFieldModal({ name, onSubmit, onClose }) {
}}
onPress={() => {
onChangeMode('replace');
- document.getElementById('noteInput').focus();
+ noteInputRef.current?.focus();
}}
>
{t('Replace')}
</Button>
<Button
- selected={noteAmend === 'append'}
style={{
padding: '5px 10px',
width: '33.33%',
@@ -178,18 +199,18 @@ export function EditFieldModal({ name, onSubmit, onClose }) {
}}
onPress={() => {
onChangeMode('append');
- document.getElementById('noteInput').focus();
+ noteInputRef.current?.focus();
}}
>
{t('Append')}
</Button>
</View>
<Input
- id="noteInput"
+ inputRef={noteInputRef}
autoFocus
focused={true}
onEnter={e => {
- onSelectNote(e.target.value, noteAmend);
+ onSelectNote(e.currentTarget.value, noteAmend);
close();
}}
style={inputStyle}
@@ -204,7 +225,7 @@ export function EditFieldModal({ name, onSubmit, onClose }) {
<Input
focused={true}
onEnter={e => {
- onSelect(e.target.value);
+ onSelect(e.currentTarget.value);
close();
}}
style={inputStyle}
diff --git a/upcoming-release-notes/3865.md b/upcoming-release-notes/3865.md
new file mode 100644
index 0000000000..e96ae65ef2
--- /dev/null
+++ b/upcoming-release-notes/3865.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Convert EditFieldModal.jsx to tsx