Skip to content

Commit

Permalink
Fix typecheck errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Apr 2, 2024
1 parent 04b054f commit 87bfbd5
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function CategoryBudgetMenu({
...props
}: CategoryBudgetMenuProps) {
const isGoalTemplatesEnabled = useFeatureFlag('goalTemplatesEnabled');
const onMenuSelect = name => {
const onMenuSelect = (name: string) => {
switch (name) {
case 'copy-single-last':
onCopyLastMonthAverage?.();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { memo, useEffect, useRef, useState } from 'react';
import React, {
type MutableRefObject,
type Ref,
type ComponentPropsWithRef,
type HTMLProps,
memo,
useEffect,
useRef,
useState,
} from 'react';

import {
toRelaxedNumber,
Expand All @@ -7,22 +16,40 @@ import {
} from 'loot-core/src/shared/util';

import { useMergedRefs } from '../../../hooks/useMergedRefs';
import { theme } from '../../../style';
import { type CSSProperties, theme } from '../../../style';
import { Button } from '../../common/Button';
import { Text } from '../../common/Text';
import { View } from '../../common/View';

type AmountInputProps = {
value: number;
focused?: boolean;
style?: CSSProperties;
textStyle?: CSSProperties;
inputRef?: Ref<HTMLInputElement>;
onFocus?: HTMLProps<HTMLInputElement>['onFocus'];
onBlur?: HTMLProps<HTMLInputElement>['onBlur'];
onChangeValue?: (value: string) => void;
onUpdate?: (value: string) => void;
onUpdateAmount?: (value: number) => void;
};

const AmountInput = memo(function AmountInput({
focused,
style,
textStyle,
...props
}) {
}: AmountInputProps) {
const [editing, setEditing] = useState(false);
const [text, setText] = useState('');
const [value, setValue] = useState(0);
const inputRef = useRef();
const mergedInputRef = useMergedRefs(props.inputRef, inputRef);
const inputRef = useRef<HTMLInputElement>();

// Need to update useMergedRefs to support Ref.
const mergedInputRef = useMergedRefs<HTMLInputElement>(
props.inputRef as MutableRefObject<HTMLInputElement>,
inputRef as MutableRefObject<HTMLInputElement>,
);

const initialValue = Math.abs(props.value);

Expand All @@ -42,11 +69,12 @@ const AmountInput = memo(function AmountInput({
return toRelaxedNumber(text.replace(/[,.]/, getNumberFormat().separator));
};

const onKeyPress = e => {
const onKeyPress: HTMLProps<HTMLInputElement>['onKeyUp'] = e => {
if (e.key === 'Backspace' && text === '') {
setEditing(true);
}
};

const applyText = () => {
const parsed = parseText();
const newValue = editing ? parsed : value;
Expand All @@ -58,20 +86,25 @@ const AmountInput = memo(function AmountInput({
return newValue;
};

const onFocus = e => {
const onFocus: HTMLProps<HTMLInputElement>['onFocus'] = e => {
props.onFocus?.(e);
};

const onBlur = e => {
const value = applyText();
const onUpdate = (value: string) => {
props.onUpdate?.(value);
const amount = applyText();
props.onUpdateAmount?.(amount);
};

const onBlur: HTMLProps<HTMLInputElement>['onBlur'] = e => {
onUpdate(e.target.value);
props.onBlur?.(e);
};

const onChangeText = text => {
const onChangeText = (text: string) => {
setEditing(true);
setText(text);
props.onChange?.(text);
props.onChangeValue?.(text);
};

const input = (
Expand Down Expand Up @@ -105,20 +138,32 @@ const AmountInput = memo(function AmountInput({
>
<View style={{ overflowY: 'auto', overflowX: 'hidden' }}>{input}</View>
<Text
style={textStyle}
style={{
pointerEvents: 'none',
...textStyle,
}}
data-testid="amount-fake-input"
pointerEvents="none"
>
{editing ? text : amountToCurrency(value)}
</Text>
</View>
);
});

type FocusableAmountInputProps = Omit<AmountInputProps, 'onFocus'> & {
sign?: '+' | '-';
zeroSign?: '+' | '-';
focused?: boolean;
disabled?: boolean;
focusedStyle?: CSSProperties;
buttonProps?: ComponentPropsWithRef<typeof Button>;
onFocus?: () => void;
};

export const FocusableAmountInput = memo(function FocusableAmountInput({
value,
sign, // + or -
zeroSign, // + or -
sign,
zeroSign,
focused,
disabled,
textStyle,
Expand All @@ -128,9 +173,18 @@ export const FocusableAmountInput = memo(function FocusableAmountInput({
onFocus,
onBlur,
...props
}) {
}: FocusableAmountInputProps) {
const [isNegative, setIsNegative] = useState(true);

const maybeApplyNegative = (amount: number, negative: boolean) => {
const absValue = Math.abs(amount);
return negative ? -absValue : absValue;
};

const onUpdateAmount = (amount: number, negative: boolean) => {
props.onUpdateAmount?.(maybeApplyNegative(amount, negative));
};

useEffect(() => {
if (sign) {
setIsNegative(sign === '-');
Expand All @@ -144,17 +198,8 @@ export const FocusableAmountInput = memo(function FocusableAmountInput({
return;
}

onUpdateAmount(value, !isNegative);
setIsNegative(!isNegative);
props.onUpdate?.(maybeApplyNegative(value, !isNegative));
};

const maybeApplyNegative = (val, negative) => {
const absValue = Math.abs(val);
return negative ? -absValue : absValue;
};

const onUpdate = val => {
props.onUpdate?.(maybeApplyNegative(val, isNegative));
};

return (
Expand All @@ -164,7 +209,7 @@ export const FocusableAmountInput = memo(function FocusableAmountInput({
value={value}
onFocus={onFocus}
onBlur={onBlur}
onUpdate={onUpdate}
onUpdateAmount={amount => onUpdateAmount(amount, isNegative)}
focused={focused && !disabled}
style={{
width: 80,
Expand Down Expand Up @@ -210,7 +255,7 @@ export const FocusableAmountInput = memo(function FocusableAmountInput({
borderBottomWidth: 1,
borderColor: '#e0e0e0',
justifyContent: 'center',
transform: [{ translateY: 0.5 }],
// transform: [{ translateY: 0.5 }],
...style,
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ const TransactionEditInner = memo(function TransactionEditInner({
zeroSign="-"
focused={totalAmountFocused}
onFocus={onTotalAmountEdit}
onUpdate={onTotalAmountUpdate}
onUpdateAmount={onTotalAmountUpdate}
focusedStyle={{
width: 'auto',
padding: '5px',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { type ComponentPropsWithoutRef } from 'react';
import React, { useState, type ComponentPropsWithoutRef } from 'react';

import { reportBudget } from 'loot-core/client/queries';
import { amountToInteger, integerToAmount } from 'loot-core/shared/util';
Expand Down Expand Up @@ -37,14 +37,15 @@ export function ReportCategoryBudgetMenuModal({

const budgeted = useSheetValue(reportBudget.catBudgeted(categoryId));
const category = useCategory(categoryId);
const [amountFocused, setAmountFocused] = useState(false);

const _onUpdateBudget = amount => {
const _onUpdateBudget = (amount: number) => {
onUpdateBudget?.(amountToInteger(amount));
};

return (
<Modal
title={category?.name}
title={`Budget: ${category?.name}`}
showHeader
focusAfterClose={false}
{...modalProps}
Expand All @@ -65,10 +66,19 @@ export function ReportCategoryBudgetMenuModal({
>
<FocusableAmountInput
value={integerToAmount(budgeted || 0)}
focused={true}
focused={amountFocused}
onFocus={() => setAmountFocused(true)}
onBlur={() => setAmountFocused(false)}
zeroSign="+"
focusedStyle={{
width: 'auto',
padding: '5px',
paddingLeft: '20px',
paddingRight: '20px',
minWidth: '100%',
}}
textStyle={{ ...styles.veryLargeText, textAlign: 'center' }}
onUpdate={_onUpdateBudget}
onUpdateAmount={_onUpdateBudget}
/>
</View>
<CategoryBudgetMenu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function RolloverCategoryBudgetMenuModal({
const category = useCategory(categoryId);
const [amountFocused, setAmountFocused] = useState(false);

const _onUpdateBudget = amount => {
const _onUpdateBudget = (amount: number) => {
onUpdateBudget?.(amountToInteger(amount));
};

Expand Down Expand Up @@ -76,10 +76,9 @@ export function RolloverCategoryBudgetMenuModal({
paddingLeft: '20px',
paddingRight: '20px',
minWidth: '100%',
transform: [{ translateY: -0.5 }],
}}
textStyle={{ ...styles.veryLargeText, textAlign: 'center' }}
onUpdate={_onUpdateBudget}
onUpdateAmount={_onUpdateBudget}
/>
</View>
<CategoryBudgetMenu
Expand Down

0 comments on commit 87bfbd5

Please sign in to comment.