Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maintenance] Refactor amount input to typescript #1936

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Input, { defaultInputStyle } from './Input';
import View from './View';

type InputWithContentProps = ComponentProps<typeof Input> & {
leftContent: ReactNode;
rightContent: ReactNode;
leftContent?: ReactNode;
rightContent?: ReactNode;
inputStyle?: CSSProperties;
focusStyle?: CSSProperties;
style?: CSSProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import React, { useRef, useState } from 'react';
import React, { type MutableRefObject, useRef, useState } from 'react';

import evalArithmetic from 'loot-core/src/shared/arithmetic';
import { amountToInteger } from 'loot-core/src/shared/util';

import { useMergedRefs } from '../../hooks/useMergedRefs';
import Add from '../../icons/v1/Add';
import Subtract from '../../icons/v1/Subtract';
import { theme } from '../../style';
import { type CSSProperties, theme } from '../../style';
import Button from '../common/Button';
import InputWithContent from '../common/InputWithContent';
import View from '../common/View';
import useFormat from '../spreadsheet/useFormat';

type AmountInputProps = {
id?: string;
inputRef?: MutableRefObject<HTMLInputElement>;
initialValue: number;
zeroSign?: '-' | '+';
onChange?: (value: number) => void;
onBlur?: () => void;
style?: CSSProperties;
textStyle?: CSSProperties;
focused?: boolean;
};

export function AmountInput({
id,
inputRef,
Expand All @@ -22,7 +34,7 @@ export function AmountInput({
style,
textStyle,
focused,
}) {
}: AmountInputProps) {
let format = useFormat();
let [negative, setNegative] = useState(
(initialValue === 0 && zeroSign === '-') || initialValue < 0,
Expand All @@ -49,8 +61,8 @@ export function AmountInput({
setValue(value ? value : '');
}

let ref = useRef();
let mergedRef = useMergedRefs(inputRef, ref);
let ref = useRef<HTMLInputElement>();
let mergedRef = useMergedRefs<HTMLInputElement>(inputRef, ref);

function onInputAmountBlur(e) {
fireChange(value, negative);
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1936.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [kymckay]
---

Refactor AmountInput component to TypeScript.