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

Save AmountInput changes on unmount #1883

Closed
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
11 changes: 10 additions & 1 deletion packages/desktop-client/src/components/util/AmountInput.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';

import evalArithmetic from 'loot-core/src/shared/arithmetic';
import { amountToInteger } from 'loot-core/src/shared/util';
Expand Down Expand Up @@ -59,6 +59,15 @@ export function AmountInput({
}
}

// Save changes when the component unmounts.
useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

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

💭 thought: ‏I'm afraid this won't be that simple.

Currently fireChange will trigger whenever value !== initialValue. Not just on unmount. So you'll be triggering much more events than expected.

What you actually want is to trigger the onBlur event when unmounting the Input component. But that's not really straight-forward.

See the thread here: facebook/react#12363

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I read that issue and tried the display: none solution. It worked on other parts of the codebase but for some reason when used here, the value state is always zero.

return () => {
if (value !== initialValue) {
fireChange(value, negative);
}
};
}, [initialValue, value, negative]);

return (
<InputWithContent
id={id}
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1883.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [joel-jeremy]
---

Fix AmountInput change not being saved when another editable field is clicked while editing
Loading