The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Use table-like currency and phone inputs in record forms (#28137)
Thomas Trompette32825ec85468
diff --git a/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormCurrencyFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormCurrencyFieldInput.tsxnew file mode 100644index 00000000000..11d61308e08--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormCurrencyFieldInput.tsx@@ -0,0 +1,108 @@+import {+ type FieldCurrencyValue,+ type FormFieldCurrencyValue,+} from '@/object-record/record-field/ui/types/FieldMetadata';+import { RecordFormPickerFieldInput } from '@/object-record/record-form/components/RecordFormPickerFieldInput';+import { CurrencyPickerDropdownButton } from '@/ui/input/components/internal/currency/components/CurrencyPickerDropdownButton';+import { t } from '@lingui/core/macro';+import { isNonEmptyString } from '@sniptt/guards';+import { useState } from 'react';+import { CurrencyCode } from 'twenty-shared/constants';+import {+ convertCurrencyAmountToCurrencyMicros,+ convertCurrencyMicrosToCurrencyAmount,+ isDefined,+} from 'twenty-shared/utils';+import {+ canBeCastAsNumberOrNull,+ castAsNumberOrNull,+} from '~/utils/cast-as-number-or-null';++type RecordFormCurrencyFieldInputProps = {+ label?: string;+ defaultValue?: FieldCurrencyValue;+ defaultCurrencyCode?: CurrencyCode;+ onChange: (value: FormFieldCurrencyValue) => void;+ readonly?: boolean;+ placeholder?: string;+};++export const RecordFormCurrencyFieldInput = ({+ label,+ defaultValue,+ defaultCurrencyCode,+ onChange,+ readonly,+ placeholder,+}: RecordFormCurrencyFieldInputProps) => {+ const amountMicros = defaultValue?.amountMicros ?? null;++ const [currencyCode, setCurrencyCode] = useState<CurrencyCode>(+ isNonEmptyString(defaultValue?.currencyCode)+ ? defaultValue.currencyCode+ : (defaultCurrencyCode ?? CurrencyCode.USD),+ );+ const [amountText, setAmountText] = useState(+ isDefined(amountMicros)+ ? String(convertCurrencyMicrosToCurrencyAmount(amountMicros))+ : '',+ );+ const [errorMessage, setErrorMessage] = useState<string | undefined>(+ undefined,+ );++ const emitCurrencyValue = (+ newCurrencyCode: CurrencyCode,+ newAmountMicros: number | null,+ ) =>+ onChange(+ isDefined(newAmountMicros)+ ? { currencyCode: newCurrencyCode, amountMicros: newAmountMicros }+ : { currencyCode: null, amountMicros: null },+ );++ const handleCurrencyCodeChange = (newCurrencyCode: string) => {+ setCurrencyCode(newCurrencyCode as CurrencyCode);+ emitCurrencyValue(newCurrencyCode as CurrencyCode, amountMicros);+ };++ const handleAmountTextChange = (newAmountText: string) => {+ setAmountText(newAmountText);++ const trimmedAmountText = newAmountText.trim();++ if (!canBeCastAsNumberOrNull(trimmedAmountText)) {+ setErrorMessage(t`Invalid number`);+ return;+ }++ setErrorMessage(undefined);++ const amount = castAsNumberOrNull(trimmedAmountText);++ emitCurrencyValue(+ currencyCode,+ isDefined(amount) ? convertCurrencyAmountToCurrencyMicros(amount) : null,+ );+ };++ return (+ <RecordFormPickerFieldInput+ label={label}+ value={amountText}+ onChange={handleAmountTextChange}+ placeholder={+ isNonEmptyString(placeholder) ? placeholder : t`Enter a number`+ }+ readonly={readonly}+ error={errorMessage}+ picker={+ <CurrencyPickerDropdownButton+ selectedCurrencyCode={currencyCode}+ onChange={handleCurrencyCodeChange}+ disabled={readonly}+ />+ }+ />+ );+};diff --git a/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormFieldInput.tsxindex af597ab64b6..df8155653c1 100644--- a/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormFieldInput.tsx+++ b/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormFieldInput.tsx@@ -4,15 +4,26 @@ import { FormLinksFieldInput } from '@/object-record/record-field/ui/form-types/import { type FormFieldInputSubFieldPlaceholders } from '@/object-record/record-field/ui/form-types/types/FormFieldInputSubFieldPlaceholders';import { type FieldDefinition } from '@/object-record/record-field/ui/types/FieldDefinition';import { type FieldMetadata } from '@/object-record/record-field/ui/types/FieldMetadata';+import { isFieldCurrency } from '@/object-record/record-field/ui/types/guards/isFieldCurrency';+import { isFieldCurrencyValue } from '@/object-record/record-field/ui/types/guards/isFieldCurrencyValue';import { isFieldEmails } from '@/object-record/record-field/ui/types/guards/isFieldEmails';import { isFieldEmailsValue } from '@/object-record/record-field/ui/types/guards/isFieldEmailsValue';import { isFieldLinks } from '@/object-record/record-field/ui/types/guards/isFieldLinks';import { isFieldLinksValue } from '@/object-record/record-field/ui/types/guards/isFieldLinksValue';-import { getRecordFormFieldInputSettings } from '@/object-record/record-form/utils/getRecordFormFieldInputSettings';+import { isFieldPhones } from '@/object-record/record-field/ui/types/guards/isFieldPhones';+import { isFieldPhonesValue } from '@/object-record/record-field/ui/types/guards/isFieldPhonesValue';+import { RecordFormCurrencyFieldInput } from '@/object-record/record-form/components/RecordFormCurrencyFieldInput';+import { RecordFormPhoneFieldInput } from '@/object-record/record-form/components/RecordFormPhoneFieldInput';+import { getRecordFormCurrencyFieldDefaultValue } from '@/object-record/record-form/utils/getRecordFormCurrencyFieldDefaultValue';+import { isNonEmptyString } from '@sniptt/guards';import { type JsonValue } from 'type-fest';+import { stripSimpleQuotesFromString } from '~/utils/string/stripSimpleQuotesFromString';type RecordFormFieldInputProps = {- field: Pick<FieldDefinition<FieldMetadata>, 'label' | 'metadata' | 'type'>;+ field: Pick<+ FieldDefinition<FieldMetadata>,+ 'label' | 'metadata' | 'type' | 'defaultValue'+ >;defaultValue: JsonValue;onChange: (value: JsonValue) => void;onClear?: () => void;@@ -60,6 +71,44 @@ export const RecordFormFieldInput = ({);}+ if (isFieldCurrency(field)) {+ return (+ <RecordFormCurrencyFieldInput+ label={field.label}+ defaultValue={+ isFieldCurrencyValue(defaultValue) ? defaultValue : undefined+ }+ defaultCurrencyCode={+ getRecordFormCurrencyFieldDefaultValue(field)?.currencyCode+ }+ onChange={onChange}+ readonly={readonly}+ placeholder={subFieldPlaceholders?.amountMicros}+ />+ );+ }++ if (isFieldPhones(field)) {+ const defaultCountryCode = field.defaultValue?.primaryPhoneCountryCode;++ return (+ <RecordFormPhoneFieldInput+ label={field.label}+ defaultValue={+ isFieldPhonesValue(defaultValue) ? defaultValue : undefined+ }+ defaultCountryCode={+ isNonEmptyString(defaultCountryCode)+ ? stripSimpleQuotesFromString(defaultCountryCode)+ : undefined+ }+ onChange={onChange}+ readonly={readonly}+ placeholder={subFieldPlaceholders?.primaryPhoneNumber}+ />+ );+ }+return (<FormFieldInputfield={field}@@ -69,7 +118,6 @@ export const RecordFormFieldInput = ({readonly={readonly}placeholder={placeholder}subFieldPlaceholders={subFieldPlaceholders}- settings={getRecordFormFieldInputSettings(field.type)}/>);};diff --git a/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormPhoneFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormPhoneFieldInput.tsxnew file mode 100644index 00000000000..24451a3901b--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormPhoneFieldInput.tsx@@ -0,0 +1,76 @@+import { type FieldPhonesValue } from '@/object-record/record-field/ui/types/FieldMetadata';+import { RecordFormPickerFieldInput } from '@/object-record/record-form/components/RecordFormPickerFieldInput';+import { useCountries } from '@/ui/input/components/internal/hooks/useCountries';+import { PhoneCountryPickerDropdownButton } from '@/ui/input/components/internal/phone/components/PhoneCountryPickerDropdownButton';+import { t } from '@lingui/core/macro';+import { isNonEmptyString } from '@sniptt/guards';+import { useState } from 'react';+import { isDefined } from 'twenty-shared/utils';++type RecordFormPhoneFieldInputProps = {+ label?: string;+ defaultValue?: FieldPhonesValue;+ defaultCountryCode?: string;+ onChange: (value: FieldPhonesValue) => void;+ readonly?: boolean;+ placeholder?: string;+};++export const RecordFormPhoneFieldInput = ({+ label,+ defaultValue,+ defaultCountryCode,+ onChange,+ readonly,+ placeholder,+}: RecordFormPhoneFieldInputProps) => {+ const countries = useCountries();++ const [countryCode, setCountryCode] = useState(+ isNonEmptyString(defaultValue?.primaryPhoneCountryCode)+ ? defaultValue.primaryPhoneCountryCode+ : defaultCountryCode,+ );++ const phoneNumber = defaultValue?.primaryPhoneNumber ?? '';++ const emitPhoneValue = (+ newCountryCode: string | undefined,+ newNumber: string,+ ) => {+ const country = isNonEmptyString(newNumber)+ ? countries.find((candidate) => candidate.countryCode === newCountryCode)+ : undefined;++ onChange({+ primaryPhoneNumber: newNumber,+ primaryPhoneCountryCode: country?.countryCode ?? '',+ primaryPhoneCallingCode: isDefined(country)+ ? `+${country.callingCode}`+ : '',+ additionalPhones: defaultValue?.additionalPhones ?? null,+ });+ };++ const handleCountryCodeChange = (newCountryCode: string) => {+ setCountryCode(newCountryCode);+ emitPhoneValue(newCountryCode, phoneNumber);+ };++ return (+ <RecordFormPickerFieldInput+ label={label}+ value={phoneNumber}+ onChange={(newNumber) => emitPhoneValue(countryCode, newNumber)}+ placeholder={placeholder ?? t`Enter phone number`}+ readonly={readonly}+ picker={+ <PhoneCountryPickerDropdownButton+ value={countryCode}+ onChange={handleCountryCodeChange}+ disabled={readonly}+ />+ }+ />+ );+};diff --git a/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormPickerFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormPickerFieldInput.tsxnew file mode 100644index 00000000000..ab3dad1c017--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/record-form/components/RecordFormPickerFieldInput.tsx@@ -0,0 +1,58 @@+import { FormFieldInputInnerContainer } from '@/object-record/record-field/ui/form-types/components/FormFieldInputInnerContainer';+import { FormFieldInputRowContainer } from '@/object-record/record-field/ui/form-types/components/FormFieldInputRowContainer';+import { StyledTextInput } from '@/ui/field/input/components/TextInput';+import { FormFieldInputContainer } from '@/ui/input/components/FormFieldInputContainer';+import { isNonEmptyString } from '@sniptt/guards';+import { type ReactNode, useId } from 'react';+import { Field } from 'twenty-ui/primitives/input';++type RecordFormPickerFieldInputProps = {+ label?: string;+ picker: ReactNode;+ value: string;+ onChange: (value: string) => void;+ placeholder: string;+ readonly?: boolean;+ error?: string;+};++export const RecordFormPickerFieldInput = ({+ label,+ picker,+ value,+ onChange,+ placeholder,+ readonly,+ error,+}: RecordFormPickerFieldInputProps) => {+ const instanceId = useId();++ return (+ <FormFieldInputContainer>+ {isNonEmptyString(label) ? (+ <Field.Label htmlFor={instanceId}>{label}</Field.Label>+ ) : null}++ <FormFieldInputRowContainer>+ <FormFieldInputInnerContainer+ formFieldInputInstanceId={instanceId}+ hasRightElement={false}+ >+ {picker}+ <StyledTextInput+ id={instanceId}+ autoComplete="off"+ placeholder={placeholder}+ value={value}+ onChange={(event) => onChange(event.target.value)}+ disabled={readonly}+ />+ </FormFieldInputInnerContainer>+ </FormFieldInputRowContainer>++ {isNonEmptyString(error) ? (+ <Field.Error match>{error}</Field.Error>+ ) : null}+ </FormFieldInputContainer>+ );+};diff --git a/packages/twenty-front/src/modules/object-record/record-form/components/__tests__/RecordFormFieldInputs.clearing.test.tsx b/packages/twenty-front/src/modules/object-record/record-form/components/__tests__/RecordFormFieldInputs.clearing.test.tsxindex 29d4fe32417..af16ce096f5 100644--- a/packages/twenty-front/src/modules/object-record/record-form/components/__tests__/RecordFormFieldInputs.clearing.test.tsx+++ b/packages/twenty-front/src/modules/object-record/record-form/components/__tests__/RecordFormFieldInputs.clearing.test.tsx@@ -100,40 +100,9 @@ describe('RecordFormFieldInputs with the real currency input', () => {it('pre-fills the metadata default currency', () => {renderInputs();- expect(screen.getByText(/\(USD\)/)).toBeInTheDocument();- });-- it('does not resurrect the default after the user clears the currency', async () => {- const user = userEvent.setup();- const { onFieldValueChange, onFieldValueClear } = renderInputs();-- await user.click(screen.getByText(/\(USD\)/));- await user.click(await screen.findByText('No currency'));-- // Clearing is a change carrying the emptied composite value, not a clear.- expect(onFieldValueChange).toHaveBeenCalledWith('amount', {- currencyCode: null,- amountMicros: null,- });- expect(onFieldValueClear).not.toHaveBeenCalled();-- expect(screen.queryByText(/\(USD\)/)).not.toBeInTheDocument();- expect(screen.getByText('Select a currency')).toBeInTheDocument();- });-- it('keeps the currency cleared when an amount is typed after clearing', async () => {- const user = userEvent.setup();- const { onFieldValueChange } = renderInputs();-- await user.click(screen.getByText(/\(USD\)/));- await user.click(await screen.findByText('No currency'));- await user.type(screen.getByRole('textbox'), '25');-- expect(onFieldValueChange).toHaveBeenLastCalledWith('amount', {- currencyCode: null,- amountMicros: 25_000_000,- });- expect(screen.queryByText(/\(USD\)/)).not.toBeInTheDocument();+ expect(+ screen.getByRole('button', { name: 'Currency: USD' }),+ ).toBeInTheDocument();});it('keeps the default when an amount is typed without touching the currency', async () => {@@ -146,6 +115,5 @@ describe('RecordFormFieldInputs with the real currency input', () => {currencyCode: CurrencyCode.USD,amountMicros: 3_210_000,});- expect(screen.getByText(/\(USD\)/)).toBeInTheDocument();});});diff --git a/packages/twenty-front/src/modules/object-record/record-form/components/__tests__/RecordFormFieldInputs.test.tsx b/packages/twenty-front/src/modules/object-record/record-form/components/__tests__/RecordFormFieldInputs.test.tsxindex 7779e4f59ab..105353d9dff 100644--- a/packages/twenty-front/src/modules/object-record/record-form/components/__tests__/RecordFormFieldInputs.test.tsx+++ b/packages/twenty-front/src/modules/object-record/record-form/components/__tests__/RecordFormFieldInputs.test.tsx@@ -5,11 +5,14 @@ import { render } from '@testing-library/react';import { CurrencyCode } from 'twenty-shared/constants';import { FieldMetadataType } from '~/generated-metadata/graphql';-const mockFormFieldInput = jest.fn((_: unknown) => null);+const mockRecordFormFieldInput = jest.fn((_: unknown) => null);-jest.mock('@/object-record/record-field/ui/components/FormFieldInput', () => ({- FormFieldInput: (props: unknown) => mockFormFieldInput(props),-}));+jest.mock(+ '@/object-record/record-form/components/RecordFormFieldInput',+ () => ({+ RecordFormFieldInput: (props: unknown) => mockRecordFormFieldInput(props),+ }),+);const currencyFieldMetadataItem = {id: 'amount-field-id',@@ -42,12 +45,12 @@ const renderInputs = (};const getPassedDefaultValue = () =>- (mockFormFieldInput.mock.calls.at(-1)?.[0] as { defaultValue: unknown })+ (mockRecordFormFieldInput.mock.calls.at(-1)?.[0] as { defaultValue: unknown }).defaultValue;describe('RecordFormFieldInputs', () => {beforeEach(() => {- mockFormFieldInput.mockClear();+ mockRecordFormFieldInput.mockClear();});it('pre-fills the currency field default when the draft has no value yet', () => {diff --git a/packages/twenty-front/src/modules/object-record/record-form/utils/__tests__/getRecordFormFieldInputSettings.test.ts b/packages/twenty-front/src/modules/object-record/record-form/utils/__tests__/getRecordFormFieldInputSettings.test.tsdeleted file mode 100644index 39f3546b275..00000000000--- a/packages/twenty-front/src/modules/object-record/record-form/utils/__tests__/getRecordFormFieldInputSettings.test.ts+++ /dev/null@@ -1,17 +0,0 @@-import { getRecordFormFieldInputSettings } from '@/object-record/record-form/utils/getRecordFormFieldInputSettings';-import { FieldMetadataType } from 'twenty-shared/types';--describe('getRecordFormFieldInputSettings', () => {- it('should take currency amounts in units', () => {- expect(getRecordFormFieldInputSettings(FieldMetadataType.CURRENCY)).toEqual(- { type: FieldMetadataType.CURRENCY, amountUnit: 'units' },- );- });-- it.each([FieldMetadataType.NUMBER, FieldMetadataType.TEXT])(- 'should have no settings for %s fields',- (fieldType) => {- expect(getRecordFormFieldInputSettings(fieldType)).toBeUndefined();- },- );-});diff --git a/packages/twenty-front/src/modules/object-record/record-form/utils/getRecordFormFieldInputSettings.ts b/packages/twenty-front/src/modules/object-record/record-form/utils/getRecordFormFieldInputSettings.tsdeleted file mode 100644index 473b4cf53ce..00000000000--- a/packages/twenty-front/src/modules/object-record/record-form/utils/getRecordFormFieldInputSettings.ts+++ /dev/null@@ -1,9 +0,0 @@-import { type FormFieldInputSettings } from '@/object-record/record-field/ui/form-types/types/FormFieldInputSettings';-import { FieldMetadataType } from 'twenty-shared/types';--export const getRecordFormFieldInputSettings = (- fieldType: FieldMetadataType,-): FormFieldInputSettings | undefined =>- fieldType === FieldMetadataType.CURRENCY- ? { type: FieldMetadataType.CURRENCY, amountUnit: 'units' }- : undefined;diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/currency/components/CurrencyPickerDropdownButton.tsx b/packages/twenty-front/src/modules/ui/input/components/internal/currency/components/CurrencyPickerDropdownButton.tsxindex 85f3b699360..8d32068f1ae 100644--- a/packages/twenty-front/src/modules/ui/input/components/internal/currency/components/CurrencyPickerDropdownButton.tsx+++ b/packages/twenty-front/src/modules/ui/input/components/internal/currency/components/CurrencyPickerDropdownButton.tsx@@ -10,6 +10,7 @@ import { DropdownRoot } from '@/ui/layout/dropdown/components/DropdownRoot';type CurrencyPickerDropdownButtonProps = {selectedCurrencyCode: string;onChange: (currencyCode: string) => void;+ disabled?: boolean;};const keepEnterAwayFromFieldHotkeys = (@@ -25,6 +26,7 @@ const keepEnterAwayFromFieldHotkeys = (export const CurrencyPickerDropdownButton = ({selectedCurrencyCode,onChange,+ disabled,}: CurrencyPickerDropdownButtonProps) => {const dropdownId = useId();const currencyPickerCurrencies = useCurrencyPickerCurrencies();@@ -37,6 +39,7 @@ export const CurrencyPickerDropdownButton = ({<DropdownRoot dropdownId={dropdownId} type="picker"><CurrencyPicker.Triggervalue={currencyCode}+ disabled={disabled}aria-label={t`Currency: ${currencyCode}`}onKeyDown={keepEnterAwayFromFieldHotkeys}/>