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

Reset amount on reset button click #42897

Merged
merged 31 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
78f25f7
Reset amount on reset button click
kaushiktd May 31, 2024
5a4feba
Reset amount on reset button click
kaushiktd May 31, 2024
98c2c78
Reset amount on reset button click
kaushiktd May 31, 2024
37af19b
Reset amount on reset button click
kaushiktd May 31, 2024
4b30dd9
Reset amount on reset button click
kaushiktd May 31, 2024
4c4e872
Reset amount on reset button click
kaushiktd May 31, 2024
cde4e5f
Reset amount on reset button click
kaushiktd May 31, 2024
c805f32
Reset amount on reset button click
kaushiktd May 31, 2024
812ce37
Resolve Conflict
kaushiktd Jun 6, 2024
7cd56e9
Reset amount on reset button click
kaushiktd Jun 6, 2024
8f71b5d
Reset amount on reset button click
kaushiktd Jun 6, 2024
057afeb
Reset amount on reset button click
kaushiktd Jun 6, 2024
bc0b3b3
Reset amount on reset button click
kaushiktd Jun 6, 2024
96c4aca
Reset amount on reset button click
kaushiktd Jun 6, 2024
1f5660c
Reset amount on reset button click
kaushiktd Jun 6, 2024
6ea1723
Reset amount on reset button click
kaushiktd Jun 24, 2024
364708b
Reset amount on reset button click
kaushiktd Jun 24, 2024
b454ac2
Reset amount on reset button click
kaushiktd Jun 24, 2024
bc10692
Reset amount on reset button click
kaushiktd Jun 24, 2024
4db9a0a
Reset amount on reset button click
kaushiktd Jun 24, 2024
b3087ed
Reset amount on reset button click
kaushiktd Jun 24, 2024
d5df8f5
Merge branch 'Expensify:main' into reset-amount-keyboard-up
kaushiktd Jun 25, 2024
10d3696
Merge branch 'Expensify:main' into reset-amount-keyboard-up
kaushiktd Jun 27, 2024
1b40934
Reset amount on reset button click
kaushiktd Jun 27, 2024
5a81a36
Reset amount on reset button click
kaushiktd Jun 28, 2024
ff59531
Reset amount on reset button click
kaushiktd Jun 28, 2024
8042286
Reset amount on reset button click
kaushiktd Jun 28, 2024
5a2602c
Reset amount on reset button click
kaushiktd Jul 10, 2024
221b7a9
Reset amount on reset button click
kaushiktd Jul 10, 2024
bf86b78
Reset amount on reset button click
kaushiktd Jul 10, 2024
b1eda3e
Reset amount on reset button click
kaushiktd Jul 10, 2024
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
32 changes: 27 additions & 5 deletions src/components/MoneyRequestAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ type MoneyRequestAmountInputProps = {
* Autogrow input container length based on the entered text.
*/
autoGrow?: boolean;

/**
* Determines whether the amount should be reset.
*/
shouldResetAmount?: boolean;

/**
* Callback function triggered when the amount is reset.
*
* @param resetValue - A boolean indicating whether the amount should be reset.
*/
onResetAmount?: (resetValue: boolean) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add comments to both of these props please?

};

type Selection = {
Expand Down Expand Up @@ -123,6 +135,8 @@ function MoneyRequestAmountInput(
hideFocusedState = true,
shouldKeepUserInput = false,
autoGrow = true,
shouldResetAmount,
onResetAmount,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onResetAmount,
onResetAmount = () => {},

...props
}: MoneyRequestAmountInputProps,
forwardedRef: ForwardedRef<BaseTextInputRef>,
Expand Down Expand Up @@ -202,10 +216,21 @@ function MoneyRequestAmountInput(
}));

useEffect(() => {
const frontendAmount = onFormatAmount(amount, currency);
setCurrentAmount(frontendAmount);
if (shouldResetAmount) {
setSelection({
start: frontendAmount.length,
end: frontendAmount.length,
});
onResetAmount?.(false);
return;
}

if ((!currency || typeof amount !== 'number' || (formatAmountOnBlur && isTextInputFocused(textInput))) ?? shouldKeepUserInput) {
return;
}
const frontendAmount = onFormatAmount(amount, currency);

setCurrentAmount(frontendAmount);

// Only update selection if the amount prop was changed from the outside and is not the same as the current amount we just computed
Expand All @@ -216,10 +241,7 @@ function MoneyRequestAmountInput(
end: frontendAmount.length,
});
}

// we want to re-initialize the state only when the amount changes
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [amount, shouldKeepUserInput]);
}, [amount, currency, formatAmountOnBlur, shouldKeepUserInput, onFormatAmount, shouldResetAmount, onResetAmount, currentAmount]);
Comment on lines -219 to +244
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey 👋 It looks like this change introduced a bug (check the revert PR description)

cc @mountiny

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to bring those lines back

        // we want to re-initialize the state only when the amount changes
        // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
    }, [amount, shouldKeepUserInput]);

and the input is working again, so a fix should be pretty easy :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up we ended up reverting this PR because of the above mentioned critical issue.

We opted for a revert to make sure there are no other unwanted regressions

@kaushiktd can you raise a new PR and make sure to test for the input working well


// Modifies the amount to match the decimals for changed currency.
useEffect(() => {
Expand Down
6 changes: 5 additions & 1 deletion src/components/MoneyRequestConfirmationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function MoneyRequestConfirmationList({
const isMerchantRequired = isPolicyExpenseChat && (!isScanRequest || isEditingSplitBill) && shouldShowMerchant;

const isCategoryRequired = !!policy?.requiresCategory;

const [shouldResetAmount, setShouldResetAmount] = useState(false);
useEffect(() => {
if (shouldDisplayFieldError && didConfirmSplit) {
setFormError('iou.error.genericSmartscanFailureMessage');
Expand Down Expand Up @@ -466,6 +466,8 @@ function MoneyRequestConfirmationList({
onFormatAmount={CurrencyUtils.convertToDisplayStringWithoutCurrency}
onAmountChange={(value: string) => onSplitShareChange(participantOption.accountID ?? -1, Number(value))}
maxLength={formattedTotalAmount.length}
shouldResetAmount={shouldResetAmount}
onResetAmount={(resetValue) => setShouldResetAmount(resetValue)}
/>
),
}));
Expand All @@ -488,6 +490,7 @@ function MoneyRequestConfirmationList({
transaction?.comment?.splits,
transaction?.splitShares,
onSplitShareChange,
shouldResetAmount,
]);

const isSplitModified = useMemo(() => {
Expand All @@ -505,6 +508,7 @@ function MoneyRequestConfirmationList({
<PressableWithFeedback
onPress={() => {
IOU.resetSplitShares(transaction);
setShouldResetAmount(true);
}}
accessibilityLabel={CONST.ROLE.BUTTON}
role={CONST.ROLE.BUTTON}
Expand Down
Loading