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

Ensure two decimal places for currency values #215

Merged
merged 2 commits into from
May 9, 2024
Merged
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
9 changes: 5 additions & 4 deletions frontend/apps/web/src/components/NumericInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { parseAbrechnungFloat } from "@abrechnung/utils";
export type NumericInputProps = {
onChange: (value: number) => void;
value?: number | undefined;
isCurrency?: boolean | false;
} & Omit<TextFieldProps, "value" | "onChange" | "onBlur" | "onKeyUp">;

export const NumericInput: React.FC<NumericInputProps> = ({ value, onChange, ...props }) => {
export const NumericInput: React.FC<NumericInputProps> = ({ value, isCurrency, onChange, ...props }) => {
const [internalValue, setInternalValue] = React.useState("");

React.useEffect(() => {
setInternalValue(String(value));
}, [value, setInternalValue]);
setInternalValue(isCurrency ? value.toFixed(2) : String(value));
}, [value, setInternalValue, isCurrency]);

const onInternalChange = (event) => {
setInternalValue(event.target.value);
Expand All @@ -23,7 +24,7 @@ export const NumericInput: React.FC<NumericInputProps> = ({ value, onChange, ...
const propagateChange = () => {
const parsedValue = parseAbrechnungFloat(internalValue);
if (!isNaN(parsedValue)) {
setInternalValue(String(parsedValue));
setInternalValue(isCurrency ? parsedValue.toFixed(2) : String(parsedValue));
onChange(parsedValue);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const TransactionMetadata: React.FC<Props> = ({
helperText={validationErrors.fieldErrors.value}
onChange={(value) => pushChanges({ value })}
value={transaction.value}
isCurrency={true}
disabled={!transaction.is_wip}
InputProps={{
endAdornment: <InputAdornment position="end">{transaction.currency_symbol}</InputAdornment>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const PositionTableRow: React.FC<PositionTableRowProps> = ({
<TableCell align="right">
<NumericInput
value={position.price}
isCurrency={true}
style={{ width: 70 }}
error={validationError && !!validationError.fieldErrors.price}
helperText={validationError && validationError.fieldErrors.price}
Expand Down
Loading