Skip to content

Commit 3d70781

Browse files
committed
fix: prevent firing onChange when only value prop changed from outside
1 parent e8505f2 commit 3d70781

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/components/CurrencyTextField/CurrencyTextField.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ export const CurrencyTextField: React.FC<CurrencyTextFieldProps> = ({
149149
precision: precision,
150150
});
151151

152-
onChange?.(dineroValue, values.formattedValue);
152+
if (!dineroValue.equalsTo(internalValue)) {
153+
onChange?.(dineroValue, values.formattedValue);
153154

154-
setInternalValue(dineroValue);
155+
setInternalValue(dineroValue);
156+
}
155157
};
156158

157159
/**

src/App.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Box, Button, Typography } from '@mui/material';
55

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

1111
return (
@@ -38,6 +38,15 @@ export const App: React.FC = () => {
3838
>
3939
50 € hinzufügen
4040
</Button>
41+
<Button
42+
variant="contained"
43+
color="primary"
44+
onClick={() => {
45+
setBrutto(() => Dinero({ amount: 5000, currency: 'EUR' }));
46+
}}
47+
>
48+
Auf 50 € setzen
49+
</Button>
4150
<Typography>Dinero amount: {brutto.getAmount()}</Typography>
4251
</Box>
4352
);

0 commit comments

Comments
 (0)