Skip to content

Commit

Permalink
fix: prevent firing onChange when only value prop changed from outside
Browse files Browse the repository at this point in the history
  • Loading branch information
saschb2b committed Oct 4, 2024
1 parent e8505f2 commit 3d70781
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/components/CurrencyTextField/CurrencyTextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ export const CurrencyTextField: React.FC<CurrencyTextFieldProps> = ({
precision: precision,
});

onChange?.(dineroValue, values.formattedValue);
if (!dineroValue.equalsTo(internalValue)) {
onChange?.(dineroValue, values.formattedValue);

setInternalValue(dineroValue);
setInternalValue(dineroValue);
}
};

/**
Expand Down
11 changes: 10 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Box, Button, Typography } from '@mui/material';

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

return (
Expand Down Expand Up @@ -38,6 +38,15 @@ export const App: React.FC = () => {
>
50 € hinzufügen
</Button>
<Button
variant="contained"
color="primary"
onClick={() => {
setBrutto(() => Dinero({ amount: 5000, currency: 'EUR' }));
}}
>
Auf 50 € setzen
</Button>
<Typography>Dinero amount: {brutto.getAmount()}</Typography>
</Box>
);
Expand Down

0 comments on commit 3d70781

Please sign in to comment.