Skip to content

Commit

Permalink
Merge pull request #215 from CollinAlpert/floating-price
Browse files Browse the repository at this point in the history
Ensure two decimal places for currency values
  • Loading branch information
mikonse authored May 9, 2024
2 parents 1fd057f + 9dd074d commit d26106d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
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

0 comments on commit d26106d

Please sign in to comment.