Skip to content

Commit e8505f2

Browse files
committed
fix: Input getting rerendered when value prop changes without manual input changes
1 parent c9f029f commit e8505f2

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/components/CurrencyTextField/CurrencyTextField.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ export const CurrencyTextField: React.FC<CurrencyTextFieldProps> = ({
122122
}));
123123
}, [currency, precision]);
124124

125+
/**
126+
* Update the internal value when the value prop changes.
127+
* This is necessary to keep the input value in sync with the value prop.
128+
*/
129+
useEffect(() => {
130+
if (value) {
131+
setInternalValue(value);
132+
}
133+
}, [value]);
134+
125135
/**
126136
* Handle changes to the input value.
127137
*

src/App.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import * as React from 'react';
22
import Dinero from 'dinero.js';
33
import { CurrencyTextField } from '../dist/material-ui-currency-textfield';
4-
import { Typography } from '@mui/material';
4+
import { Box, Button, Typography } from '@mui/material';
55

66
export const App: React.FC = () => {
77
const [brutto, setBrutto] = React.useState<Dinero.Dinero>(
88
Dinero({ amount: 133742, currency: 'EUR', precision: 2 }),
99
);
1010

1111
return (
12-
<div>
12+
<Box
13+
sx={{ display: 'flex', flexDirection: 'column', gap: 2, maxWidth: 200 }}
14+
>
1315
<CurrencyTextField
1416
label={'Einkommen'}
1517
name={'einkommen'}
@@ -25,7 +27,18 @@ export const App: React.FC = () => {
2527
setBrutto(value);
2628
}}
2729
/>
30+
<Button
31+
variant="contained"
32+
color="primary"
33+
onClick={() => {
34+
setBrutto((prev) =>
35+
prev.add(Dinero({ amount: 5000, currency: 'EUR' })),
36+
);
37+
}}
38+
>
39+
50 € hinzufügen
40+
</Button>
2841
<Typography>Dinero amount: {brutto.getAmount()}</Typography>
29-
</div>
42+
</Box>
3043
);
3144
};

0 commit comments

Comments
 (0)