The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(front): persist percentage values that still include a percent sign (#17677) (#25555)
Chaitanya Laxman9a0be50fc677
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/NumberFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/NumberFieldInput.tsxindex 1579867f029..b2ce4acc808 100644--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/NumberFieldInput.tsx+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/NumberFieldInput.tsx@@ -1,16 +1,12 @@-import { TextInput } from '@/ui/field/input/components/TextInput';+import { useContext } from 'react';import { FieldInputEventContext } from '@/object-record/record-field/ui/contexts/FieldInputEventContext';+import { useNumberField } from '@/object-record/record-field/ui/meta-types/hooks/useNumberField';+import { getNumberValueToPersist } from '@/object-record/record-field/ui/meta-types/input/utils/getNumberValueToPersist';import { RecordFieldComponentInstanceContext } from '@/object-record/record-field/ui/states/contexts/RecordFieldComponentInstanceContext';import { FieldInputContainer } from '@/ui/field/input/components/FieldInputContainer';+import { TextInput } from '@/ui/field/input/components/TextInput';import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow';-import { isNull } from '@sniptt/guards';-import { useContext } from 'react';-import {- canBeCastAsNumberOrNull,- castAsNumberOrNull,-} from '~/utils/cast-as-number-or-null';-import { useNumberField } from '@/object-record/record-field/ui/meta-types/hooks/useNumberField';export const NumberFieldInput = () => {const { fieldDefinition, draftValue, setDraftValue } = useNumberField();@@ -23,75 +19,41 @@ export const NumberFieldInput = () => {RecordFieldComponentInstanceContext,);- const getNumberValueToPersist = (- newValue: string,- ): { success: boolean; value?: any } => {- if (fieldDefinition?.metadata?.settings?.type === 'percentage') {- const newValueEscaped = newValue.replaceAll('%', '');-- if (!canBeCastAsNumberOrNull(newValueEscaped)) {- return { success: false };- }-- const castedValue = castAsNumberOrNull(newValue);-- if (!isNull(castedValue)) {- return { success: true, value: castedValue / 100 };- }-- return { success: true, value: null };- }-- if (!canBeCastAsNumberOrNull(newValue)) {- return { success: false };- }+ const getFieldInputEventArgs = (newValue: string) => {+ const persistResult = getNumberValueToPersist({+ newValue,+ numberType: fieldDefinition.metadata.settings?.type,+ });- const castedValue = castAsNumberOrNull(newValue);-- return { success: true, value: castedValue };+ return persistResult.success+ ? { newValue: persistResult.value, skipPersist: false }+ : { skipPersist: true };};const handleEnter = (newText: string) => {- const { success, value } = getNumberValueToPersist(newText);-- const shouldNotPersist = !success;-- onEnter?.({ newValue: value, skipPersist: shouldNotPersist });+ onEnter?.(getFieldInputEventArgs(newText));};const handleEscape = (newText: string) => {- const { success, value } = getNumberValueToPersist(newText);-- const shouldNotPersist = !success;-- onEscape?.({ newValue: value, skipPersist: shouldNotPersist });+ onEscape?.(getFieldInputEventArgs(newText));};const handleClickOutside = (event: MouseEvent | TouchEvent,newText: string,) => {- const { success, value } = getNumberValueToPersist(newText);-- const shouldNotPersist = !success;-- onClickOutside?.({ newValue: value, skipPersist: shouldNotPersist, event });+ onClickOutside?.({+ ...getFieldInputEventArgs(newText),+ event,+ });};const handleTab = (newText: string) => {- const { success, value } = getNumberValueToPersist(newText);-- const shouldNotPersist = !success;-- onTab?.({ newValue: value, skipPersist: shouldNotPersist });+ onTab?.(getFieldInputEventArgs(newText));};const handleShiftTab = (newText: string) => {- const { success, value } = getNumberValueToPersist(newText);-- const shouldNotPersist = !success;-- onShiftTab?.({ newValue: value, skipPersist: shouldNotPersist });+ onShiftTab?.(getFieldInputEventArgs(newText));};const handleChange = (newText: string) => {diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/utils/__tests__/getNumberValueToPersist.test.ts b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/utils/__tests__/getNumberValueToPersist.test.tsnew file mode 100644index 00000000000..16251347928--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/utils/__tests__/getNumberValueToPersist.test.ts@@ -0,0 +1,75 @@+import { getNumberValueToPersist } from '@/object-record/record-field/ui/meta-types/input/utils/getNumberValueToPersist';++describe('getNumberValueToPersist', () => {+ it('should persist a percentage that still has the percent sign', () => {+ expect(+ getNumberValueToPersist({+ newValue: '50%',+ numberType: 'percentage',+ }),+ ).toEqual({ success: true, value: 0.5 });+ });++ it('should persist a percentage typed without the percent sign', () => {+ expect(+ getNumberValueToPersist({+ newValue: '50',+ numberType: 'percentage',+ }),+ ).toEqual({ success: true, value: 0.5 });+ });++ it('should persist an empty percentage as null', () => {+ expect(+ getNumberValueToPersist({+ newValue: '',+ numberType: 'percentage',+ }),+ ).toEqual({ success: true, value: null });+ });++ it('should not persist a percentage that is not numeric', () => {+ expect(+ getNumberValueToPersist({+ newValue: 'abc%',+ numberType: 'percentage',+ }),+ ).toEqual({ success: false });+ });++ it('should not persist a percentage with a percent sign that is not a trailing suffix', () => {+ expect(+ getNumberValueToPersist({+ newValue: '5%0',+ numberType: 'percentage',+ }),+ ).toEqual({ success: false });+ });++ it('should not persist a percentage with more than one trailing percent sign', () => {+ expect(+ getNumberValueToPersist({+ newValue: '50%%',+ numberType: 'percentage',+ }),+ ).toEqual({ success: false });+ });++ it('should persist a plain number', () => {+ expect(+ getNumberValueToPersist({+ newValue: '50',+ numberType: 'number',+ }),+ ).toEqual({ success: true, value: 50 });+ });++ it('should not persist a percent sign on a plain number field', () => {+ expect(+ getNumberValueToPersist({+ newValue: '50%',+ numberType: 'number',+ }),+ ).toEqual({ success: false });+ });+});diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/utils/getNumberValueToPersist.ts b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/utils/getNumberValueToPersist.tsnew file mode 100644index 00000000000..970c80ba632--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/utils/getNumberValueToPersist.ts@@ -0,0 +1,36 @@+import { isNull } from '@sniptt/guards';++import { type FieldNumberVariant } from '@/object-record/record-field/ui/types/FieldMetadata';+import {+ canBeCastAsNumberOrNull,+ castAsNumberOrNull,+} from '~/utils/cast-as-number-or-null';++type NumberValueToPersistResult =+ | { success: false }+ | { success: true; value: number | null };++type GetNumberValueToPersistParams = {+ newValue: string;+ numberType?: FieldNumberVariant;+};++export const getNumberValueToPersist = ({+ newValue,+ numberType,+}: GetNumberValueToPersistParams): NumberValueToPersistResult => {+ const valueToCast =+ numberType === 'percentage' ? newValue.replace(/%$/, '') : newValue;++ if (!canBeCastAsNumberOrNull(valueToCast)) {+ return { success: false };+ }++ const castedValue = castAsNumberOrNull(valueToCast);++ if (numberType === 'percentage' && !isNull(castedValue)) {+ return { success: true, value: castedValue / 100 };+ }++ return { success: true, value: castedValue };+};