Skip to content

The open alternative to Salesforce, designed for AI.

fix(currency): prevent dropping decimal separator and multiplying amount on 0-decimal fields (#25870) (#25904)

Simon Enerstrand17ab0e3e5699
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/hooks/useCurrencyField.ts b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/hooks/useCurrencyField.ts
index 276d228d3a9..c04fe3d9dcc 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/hooks/useCurrencyField.ts
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/hooks/useCurrencyField.ts
@@ -10,7 +10,6 @@ import { recordStoreFamilySelector } from '@/object-record/record-store/states/s
import { useAtomFamilySelectorState } from '@/ui/utilities/state/jotai/hooks/useAtomFamilySelectorState';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { FieldMetadataType } from '~/generated-metadata/graphql';
-import { DEFAULT_DECIMAL_VALUE } from '~/utils/format/formatNumber';
export const useCurrencyField = () => {
const { recordId, fieldDefinition } = useContext(FieldContext);
@@ -36,9 +35,6 @@ export const useCurrencyField = () => {
const defaultValue = fieldDefinition.defaultValue;
- const decimals =
- fieldDefinition.metadata.settings?.decimals ?? DEFAULT_DECIMAL_VALUE;
-
return {
fieldDefinition,
fieldValue,
@@ -46,6 +42,5 @@ export const useCurrencyField = () => {
setDraftValue,
setFieldValue,
defaultValue,
- decimals,
};
};
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/CurrencyFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/CurrencyFieldInput.tsx
index f59d26d205f..68bcc61eb56 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/CurrencyFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/CurrencyFieldInput.tsx
@@ -18,7 +18,7 @@ import { convertCurrencyAmountToCurrencyMicros } from '~/utils/convertCurrencyTo
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
export const CurrencyFieldInput = () => {
- const { fieldValue, draftValue, setDraftValue, defaultValue, decimals } =
+ const { fieldValue, draftValue, setDraftValue, defaultValue } =
useCurrencyField();
const { onClickOutside, onEnter, onEscape, onShiftTab, onTab } = useContext(
@@ -126,7 +126,6 @@ export const CurrencyFieldInput = () => {
instanceId={instanceId}
value={draftValue?.amount?.toString() ?? ''}
currencyCode={currencyCode}
- decimals={decimals}
autoFocus
placeholder={t`Currency`}
onClickOutside={handleClickOutside}
diff --git a/packages/twenty-front/src/modules/ui/field/input/components/CurrencyInput.tsx b/packages/twenty-front/src/modules/ui/field/input/components/CurrencyInput.tsx
index 804039d4372..346f929606f 100644
--- a/packages/twenty-front/src/modules/ui/field/input/components/CurrencyInput.tsx
+++ b/packages/twenty-front/src/modules/ui/field/input/components/CurrencyInput.tsx
@@ -6,7 +6,7 @@ import { useRegisterInputEvents } from '@/object-record/record-field/ui/meta-typ
import { useNumberFormat } from '@/localization/hooks/useNumberFormat';
import { CURRENCIES } from '@/settings/data-model/constants/Currencies';
import { CurrencyPickerDropdownButton } from '@/ui/input/components/internal/currency/components/CurrencyPickerDropdownButton';
-import { getSafeScaleForCurrencyInput } from '@/ui/field/input/utils/getSafeScaleForCurrencyInput';
+import { CURRENCY_MICROS_DECIMAL_PLACES } from '@/ui/field/input/constants/CurrencyMicrosDecimalPlaces';
import { type Currency } from '@/ui/input/components/internal/types/Currency';
import { IMaskInput } from 'react-imask';
import { type IconComponent } from 'twenty-ui/icon';
@@ -62,7 +62,6 @@ export type CurrencyInputProps = {
placeholder?: string;
autoFocus?: boolean;
value: string;
- decimals?: number;
currencyCode: string;
onEnter: (newText: string) => void;
onEscape: (newText: string) => void;
@@ -86,13 +85,9 @@ export const CurrencyInput = ({
onClickOutside,
onChange,
onSelect,
- decimals,
}: CurrencyInputProps) => {
const { theme } = useContext(ThemeContext);
const [internalText, setInternalText] = useState(value);
- const [scale, setScale] = useState(() =>
- getSafeScaleForCurrencyInput({ value, decimals }),
- );
const { numberFormat } = useNumberFormat();
const wrapperRef = useRef<HTMLInputElement>(null);
@@ -128,17 +123,6 @@ export const CurrencyInput = ({
const currency = CURRENCIES.find(({ value }) => value === currencyCode);
- const scaleForCurrentValue = getSafeScaleForCurrencyInput({
- value,
- decimals,
- });
-
- // deleting a decimal must not narrow the mask for the rest of the edit,
- // it would make the digit impossible to type back
- if (scale < scaleForCurrentValue) {
- setScale(scaleForCurrentValue);
- }
-
useEffect(() => {
setInternalText(value);
}, [value]);
@@ -161,7 +145,7 @@ export const CurrencyInput = ({
mask={Number}
thousandsSeparator={thousandsSeparator}
radix={radix}
- scale={scale}
+ scale={CURRENCY_MICROS_DECIMAL_PLACES}
onAccept={(value: string, _maskRef: unknown, event?: InputEvent) =>
handleAccept(value, event)
}
diff --git a/packages/twenty-front/src/modules/ui/field/input/constants/CurrencyMicrosDecimalPlaces.ts b/packages/twenty-front/src/modules/ui/field/input/constants/CurrencyMicrosDecimalPlaces.ts
new file mode 100644
index 00000000000..d46e4ba2f99
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/field/input/constants/CurrencyMicrosDecimalPlaces.ts
@@ -0,0 +1 @@
+export const CURRENCY_MICROS_DECIMAL_PLACES = 6;
diff --git a/packages/twenty-front/src/modules/ui/field/input/utils/__tests__/getSafeScaleForCurrencyInput.test.ts b/packages/twenty-front/src/modules/ui/field/input/utils/__tests__/getSafeScaleForCurrencyInput.test.ts
deleted file mode 100644
index 14e63a7c0a1..00000000000
--- a/packages/twenty-front/src/modules/ui/field/input/utils/__tests__/getSafeScaleForCurrencyInput.test.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-import { IMask } from 'react-imask';
-
-import { NumberFormat } from '@/localization/constants/NumberFormat';
-import { getSafeScaleForCurrencyInput } from '@/ui/field/input/utils/getSafeScaleForCurrencyInput';
-import { getSeparatorsForNumberFormat } from '~/utils/format/getSeparatorsForNumberFormat';
-
-describe('getSafeScaleForCurrencyInput', () => {
- it('should keep the field decimals when the value has no decimal part', () => {
- expect(getSafeScaleForCurrencyInput({ value: '458', decimals: 2 })).toBe(2);
- });
-
- it('should keep the field decimals when the value fits in it', () => {
- expect(getSafeScaleForCurrencyInput({ value: '458.6', decimals: 2 })).toBe(
- 2,
- );
- });
-
- it('should widen the scale to the decimals present in the value', () => {
- expect(getSafeScaleForCurrencyInput({ value: '458.64', decimals: 0 })).toBe(
- 2,
- );
- });
-
- it('should handle negative values', () => {
- expect(
- getSafeScaleForCurrencyInput({ value: '-458.64', decimals: 0 }),
- ).toBe(2);
- });
-
- it('should default to no decimals when neither decimals nor value provide any', () => {
- expect(getSafeScaleForCurrencyInput({ value: '' })).toBe(0);
- });
-
- it('should ignore a value that is not a plain unmasked number', () => {
- expect(
- getSafeScaleForCurrencyInput({ value: '1.234,56', decimals: 1 }),
- ).toBe(1);
- });
-});
-
-describe('currency mask round trip with the safe scale', () => {
- it.each([
- [NumberFormat.DOTS_AND_COMMA, '458,64'],
- [NumberFormat.COMMAS_AND_DOT, '458.64'],
- [NumberFormat.SPACES_AND_COMMA, '458,64'],
- [NumberFormat.APOSTROPHE_AND_DOT, '458.64'],
- ])(
- 'should not alter 458.64 with decimals 0 under the %s format',
- (numberFormat, expectedMaskedValue) => {
- const { thousandsSeparator, radix } =
- getSeparatorsForNumberFormat(numberFormat);
-
- const mask = IMask.createMask({
- mask: Number,
- thousandsSeparator,
- radix,
- scale: getSafeScaleForCurrencyInput({ value: '458.64', decimals: 0 }),
- });
-
- mask.unmaskedValue = '458.64';
-
- expect(mask.value).toBe(expectedMaskedValue);
- expect(mask.unmaskedValue).toBe('458.64');
- },
- );
-});
diff --git a/packages/twenty-front/src/modules/ui/field/input/utils/getSafeScaleForCurrencyInput.ts b/packages/twenty-front/src/modules/ui/field/input/utils/getSafeScaleForCurrencyInput.ts
deleted file mode 100644
index 1ac77a06074..00000000000
--- a/packages/twenty-front/src/modules/ui/field/input/utils/getSafeScaleForCurrencyInput.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { DEFAULT_DECIMAL_VALUE } from '~/utils/format/formatNumber';
-
-type GetSafeScaleForCurrencyInputParams = {
- value: string;
- decimals?: number;
-};
-
-const UNMASKED_VALUE_PATTERN = /^-?\d*\.(\d+)$/;
-
-export const getSafeScaleForCurrencyInput = ({
- value,
- decimals = DEFAULT_DECIMAL_VALUE,
-}: GetSafeScaleForCurrencyInputParams): number => {
- const decimalPart = UNMASKED_VALUE_PATTERN.exec(value)?.[1];
-
- return Math.max(decimals, decimalPart?.length ?? 0);
-};