The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Persist multi-item field changes as they are committed (#23943)
Félix Malfait2a21cb15a77f
diff --git a/packages/twenty-front/src/modules/object-record/record-board/record-board-card/anchored-portal/components/RecordBoardCardInputContextProvider.tsx b/packages/twenty-front/src/modules/object-record/record-board/record-board-card/anchored-portal/components/RecordBoardCardInputContextProvider.tsxindex 3937c5f5730..70f21e5f96d 100644--- a/packages/twenty-front/src/modules/object-record/record-board/record-board-card/anchored-portal/components/RecordBoardCardInputContextProvider.tsx+++ b/packages/twenty-front/src/modules/object-record/record-board/record-board-card/anchored-portal/components/RecordBoardCardInputContextProvider.tsx@@ -47,12 +47,18 @@ export const RecordBoardCardInputContextProvider = ({closeInlineCellAndResetEditModePosition();};- const handleSubmit: FieldInputEvent = ({ newValue, skipPersist }) => {+ const handleSubmit: FieldInputEvent = ({+ newValue,+ skipPersist,+ skipClose,+ }) => {if (skipPersist !== true) {persistFieldFromFieldInputContext(newValue);}- closeInlineCellAndResetEditModePosition();+ if (skipClose !== true) {+ closeInlineCellAndResetEditModePosition();+ }};const handleCancel = () => {diff --git a/packages/twenty-front/src/modules/object-record/record-calendar/record-calendar-card/anchored-portal/components/RecordCalendarCardInputContextProvider.tsx b/packages/twenty-front/src/modules/object-record/record-calendar/record-calendar-card/anchored-portal/components/RecordCalendarCardInputContextProvider.tsxindex de1e8750427..16f55d3b314 100644--- a/packages/twenty-front/src/modules/object-record/record-calendar/record-calendar-card/anchored-portal/components/RecordCalendarCardInputContextProvider.tsx+++ b/packages/twenty-front/src/modules/object-record/record-calendar/record-calendar-card/anchored-portal/components/RecordCalendarCardInputContextProvider.tsx@@ -47,12 +47,18 @@ export const RecordCalendarCardInputContextProvider = ({closeInlineCellAndResetEditModePosition();};- const handleSubmit: FieldInputEvent = ({ newValue, skipPersist }) => {+ const handleSubmit: FieldInputEvent = ({+ newValue,+ skipPersist,+ skipClose,+ }) => {if (skipPersist !== true) {persistFieldFromFieldInputContext(newValue);}- closeInlineCellAndResetEditModePosition();+ if (skipClose !== true) {+ closeInlineCellAndResetEditModePosition();+ }};const handleCancel = () => {diff --git a/packages/twenty-front/src/modules/object-record/record-field-list/anchored-portal/components/RecordFieldListInputContextProvider.tsx b/packages/twenty-front/src/modules/object-record/record-field-list/anchored-portal/components/RecordFieldListInputContextProvider.tsxindex 00433f59106..d9e8ad7f317 100644--- a/packages/twenty-front/src/modules/object-record/record-field-list/anchored-portal/components/RecordFieldListInputContextProvider.tsx+++ b/packages/twenty-front/src/modules/object-record/record-field-list/anchored-portal/components/RecordFieldListInputContextProvider.tsx@@ -75,12 +75,18 @@ export const RecordFieldListInputContextProvider = ({closeInlineCellAndResetEditModePosition();};- const handleSubmit: FieldInputEvent = ({ newValue, skipPersist }) => {+ const handleSubmit: FieldInputEvent = ({+ newValue,+ skipPersist,+ skipClose,+ }) => {if (skipPersist !== true) {persistFieldFromFieldInputContext(newValue);}- closeInlineCellAndResetEditModePosition();+ if (skipClose !== true) {+ closeInlineCellAndResetEditModePosition();+ }};const handleCancel = () => {diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/contexts/FieldInputEventContext.ts b/packages/twenty-front/src/modules/object-record/record-field/ui/contexts/FieldInputEventContext.tsindex 383730a291d..6df3280e831 100644--- a/packages/twenty-front/src/modules/object-record/record-field/ui/contexts/FieldInputEventContext.ts+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/contexts/FieldInputEventContext.ts@@ -3,6 +3,7 @@ import { createContext } from 'react';export type FieldInputEventArgs = {newValue?: unknown;skipPersist?: boolean;+ skipClose?: boolean;};export type FieldInputEvent = (args: FieldInputEventArgs) => void;diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/ArrayFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/ArrayFieldInput.tsxindex f139f8e3c41..66961f7ff2e 100644--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/ArrayFieldInput.tsx+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/ArrayFieldInput.tsx@@ -13,7 +13,7 @@ import { FieldMetadataType } from '~/generated-metadata/graphql';export const ArrayFieldInput = () => {const { setDraftValue, draftValue, fieldDefinition } = useArrayField();- const { onEscape, onClickOutside, onEnter } = useContext(+ const { onEscape, onClickOutside, onEnter, onSubmit } = useContext(FieldInputEventContext,);@@ -57,6 +57,13 @@ export const ArrayFieldInput = () => {onEnter?.({ newValue: parseStringArrayToArrayValue(newValue) });};+ const handleSubmit = (newValue: string[]) => {+ onSubmit?.({+ newValue: parseStringArrayToArrayValue(newValue),+ skipClose: true,+ });+ };+const maxNumberOfValues =fieldDefinition.metadata.settings?.maxNumberOfValues ??MULTI_ITEM_FIELD_DEFAULT_MAX_VALUES;@@ -67,6 +74,7 @@ export const ArrayFieldInput = () => {items={arrayItems}onChange={handleChange}onEnter={handleEnter}+ onSubmit={handleSubmit}onEscape={handleEscape}onClickOutside={handleClickOutside}placeholder={t`Enter value`}diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/EmailsFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/EmailsFieldInput.tsxindex 1c9222e91c3..240ff5c96bf 100644--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/EmailsFieldInput.tsx+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/EmailsFieldInput.tsx@@ -19,7 +19,7 @@ export const EmailsFieldInput = () => {const { copyToClipboard } = useCopyToClipboard();const { t } = useLingui();- const { onEscape, onClickOutside, onEnter } = useContext(+ const { onEscape, onClickOutside, onEnter, onSubmit } = useContext(FieldInputEventContext,);@@ -97,6 +97,13 @@ export const EmailsFieldInput = () => {onEnter?.({ newValue: parseStringArrayToEmailsValue(updatedEmails) });};+ const handleSubmit = (updatedEmails: string[]) => {+ onSubmit?.({+ newValue: parseStringArrayToEmailsValue(updatedEmails),+ skipClose: true,+ });+ };+const maxNumberOfValues =fieldDefinition.metadata.settings?.maxNumberOfValues ??MULTI_ITEM_FIELD_DEFAULT_MAX_VALUES;@@ -106,6 +113,7 @@ export const EmailsFieldInput = () => {items={emails}onChange={handleChange}onEnter={handleEnter}+ onSubmit={handleSubmit}onEscape={handleEscape}onClickOutside={handleClickOutside}placeholder={t`Email`}diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/FilesFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/FilesFieldInput.tsxindex c1623248b75..841dfffc4d7 100644--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/FilesFieldInput.tsx+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/FilesFieldInput.tsx@@ -33,7 +33,7 @@ export const FilesFieldInput = () => {isAttachmentPreviewEnabledState,);- const { onEscape, onClickOutside, onEnter } = useContext(+ const { onEscape, onClickOutside, onEnter, onSubmit } = useContext(FieldInputEventContext,);@@ -63,8 +63,10 @@ export const FilesFieldInput = () => {if (isDefined(nextValue)) {setDraftValue(nextValue);+ // This input renders nothing without files, so it has to be closed once+ // the last one is gone, the value itself being persisted by onSubmitif (nextValue.length === 0) {- onEnter?.({ newValue: nextValue });+ onEnter?.({ newValue: nextValue, skipPersist: true });}}},@@ -148,6 +150,13 @@ export const FilesFieldInput = () => {onEnter?.({ newValue: parseFilesArrayToFilesValue(updatedFiles) });};+ const handleSubmit = (updatedFiles: FieldFilesValue[]) => {+ onSubmit?.({+ newValue: parseFilesArrayToFilesValue(updatedFiles),+ skipClose: true,+ });+ };+const handlePreview = (file: FieldFilesValue) => {if (!isAttachmentPreviewEnabled) return;setFilePreview(file);@@ -189,6 +198,7 @@ export const FilesFieldInput = () => {items={files}onChange={handleChange}onEnter={handleEnter}+ onSubmit={handleSubmit}onEscape={handleEscape}onClickOutside={handleClickOutside}placeholder={t`File label`}diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/LinksFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/LinksFieldInput.tsxindex f195afab4ab..0efb584c0e5 100644--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/LinksFieldInput.tsx+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/LinksFieldInput.tsx@@ -21,7 +21,7 @@ type LinkRecord = {export const LinksFieldInput = () => {const { draftValue, fieldDefinition, setDraftValue } = useLinksField();- const { onEscape, onClickOutside, onEnter } = useContext(+ const { onEscape, onClickOutside, onEnter, onSubmit } = useContext(FieldInputEventContext,);@@ -80,6 +80,13 @@ export const LinksFieldInput = () => {onEnter?.({ newValue: parseArrayToLinksValue(updatedLinks) });};+ const handleSubmit = (updatedLinks: LinkRecord[]) => {+ onSubmit?.({+ newValue: parseArrayToLinksValue(updatedLinks),+ skipClose: true,+ });+ };+const maxNumberOfValues =fieldDefinition.metadata.settings?.maxNumberOfValues ??MULTI_ITEM_FIELD_DEFAULT_MAX_VALUES;@@ -90,6 +97,7 @@ export const LinksFieldInput = () => {onChange={handleChange}onEscape={handleEscape}onEnter={handleEnter}+ onSubmit={handleSubmit}onClickOutside={handleClickOutside}placeholder="URL"fieldMetadataType={FieldMetadataType.LINKS}diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/MultiItemFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/MultiItemFieldInput.tsxindex fb57c474b4e..ba0d35ecbc9 100644--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/MultiItemFieldInput.tsx+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/MultiItemFieldInput.tsx@@ -28,6 +28,7 @@ import { MenuItem } from 'twenty-ui/navigation';import { FieldMetadataType } from '~/generated-metadata/graphql';import { moveArrayItem } from '~/utils/array/moveArrayItem';import { toSpliced } from '~/utils/array/toSpliced';+import { isDeeplyEqual } from '~/utils/isDeeplyEqual';import { normalizeSearchText } from '~/utils/normalizeSearchText';import { turnIntoEmptyStringIfWhitespacesOnly } from '~/utils/string/turnIntoEmptyStringIfWhitespacesOnly';@@ -36,6 +37,7 @@ type MultiItemFieldInputProps<T> = {onChange: (newItemsValue: T[]) => void;onEscape: (newItemsValue: T[]) => void;onEnter: (newItemsValue: T[]) => void;+ onSubmit: (newItemsValue: T[]) => void;onClickOutside: (newItemsValue: T[], event: MouseEvent | TouchEvent) => void;onError?: (hasError: boolean, values: any[]) => void;placeholder: string;@@ -62,6 +64,7 @@ export const MultiItemFieldInput = <T,>({onChange,onEscape,onEnter,+ onSubmit,onError,placeholder,validateInput,@@ -225,9 +228,13 @@ export const MultiItemFieldInput = <T,>({}onChange(updatedItems);+if (shouldAutoEnterBecauseOnlyOneItemIsAllowed) {onEnter(updatedItems);+ } else if (!isDeeplyEqual(items, updatedItems)) {+ onSubmit(updatedItems);}+setIsInputDisplayed(false);setIsAddingNewItem(false);setInputValue('');@@ -283,11 +290,13 @@ export const MultiItemFieldInput = <T,>({const handleSetPrimaryItem = (index: number) => {const updatedItems = moveArrayItem(items, { fromIndex: index, toIndex: 0 });onChange(updatedItems);+ onSubmit(updatedItems);};const handleDeleteItem = (index: number) => {const updatedItems = toSpliced(items, index, 1);onChange(updatedItems);+ onSubmit(updatedItems);showInputIfNoItemsRemain(updatedItems);};diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/PhonesFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/PhonesFieldInput.tsxindex 3101a6cf75d..6fed31bfe36 100644--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/PhonesFieldInput.tsx+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/PhonesFieldInput.tsx@@ -80,7 +80,7 @@ const StyledCustomPhoneInputWrapper = styled.div`export const PhonesFieldInput = () => {const { fieldDefinition, setDraftValue, draftValue } = usePhonesField();- const { onEscape, onClickOutside, onEnter } = useContext(+ const { onEscape, onClickOutside, onEnter, onSubmit } = useContext(FieldInputEventContext,);@@ -152,6 +152,13 @@ export const PhonesFieldInput = () => {onEnter?.({ newValue: parseArrayToPhonesValue(updatedPhones) });};+ const handleSubmit = (updatedPhones: PhoneRecord[]) => {+ onSubmit?.({+ newValue: parseArrayToPhonesValue(updatedPhones),+ skipClose: true,+ });+ };+return (<MultiItemFieldInputitems={phones}@@ -159,6 +166,7 @@ export const PhonesFieldInput = () => {onClickOutside={handleClickOutside}onEscape={handleEscape}onEnter={handleEnter}+ onSubmit={handleSubmit}placeholder={t`Phone`}fieldMetadataType={FieldMetadataType.PHONES}validateInput={validateInput}diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/__stories__/LinksFieldInput.stories.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/__stories__/LinksFieldInput.stories.tsxindex a6ac5c5f1db..d3116267d52 100644--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/__stories__/LinksFieldInput.stories.tsx+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/__stories__/LinksFieldInput.stories.tsx@@ -18,9 +18,16 @@ const updateRecord = fn();const {FieldInputEventContextProviderWithJestMocks,handleEscapeMocked,+ handleSubmitMocked,handleClickoutsideMocked,} = getFieldInputEventContextProviderWithJestMocks();+const EMPTY_LINKS_VALUE = {+ primaryLinkUrl: null,+ primaryLinkLabel: null,+ secondaryLinks: [],+};+const LinksValueSetterEffect = ({value,}: {@@ -305,6 +312,66 @@ export const DeletePrimaryLink: Story = {},};+export const DeletePrimaryLinkPersistsEmptyValue: Story = {+ args: {+ value: {+ primaryLinkUrl: 'https://www.twenty.com',+ primaryLinkLabel: 'Twenty Website',+ secondaryLinks: [],+ },+ },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ const openDropdownButton = await canvas.findByRole('button', {+ expanded: false,+ });+ await userEvent.click(openDropdownButton);++ const deleteOption = await within(+ canvasElement.ownerDocument.body,+ ).findByText('Delete');+ await userEvent.click(deleteOption);++ expect(handleSubmitMocked).toHaveBeenCalledWith({+ newValue: EMPTY_LINKS_VALUE,+ skipClose: true,+ });+ },+};++export const ClearPrimaryLinkAndPressEnter: Story = {+ args: {+ value: {+ primaryLinkUrl: 'https://www.twenty.com',+ primaryLinkLabel: 'Twenty Website',+ secondaryLinks: [],+ },+ },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ const openDropdownButton = await canvas.findByRole('button', {+ expanded: false,+ });+ await userEvent.click(openDropdownButton);++ const editOption = await within(+ canvasElement.ownerDocument.body,+ ).findByText('Edit');+ await userEvent.click(editOption);++ const input = await canvas.findByPlaceholderText('URL');+ await userEvent.clear(input);+ await userEvent.type(input, '{enter}');++ expect(handleSubmitMocked).toHaveBeenCalledWith({+ newValue: EMPTY_LINKS_VALUE,+ skipClose: true,+ });+ },+};+export const DeletePrimaryLinkAndUseSecondaryLinkAsTheNewPrimaryLink: Story = {args: {value: {@@ -487,6 +554,17 @@ export const MakeSecondaryLinkPrimary: Story = {canvasElement.ownerDocument.body,).findByText('Set as Primary');await userEvent.click(setPrimaryOption);++ expect(handleSubmitMocked).toHaveBeenCalledWith({+ newValue: {+ primaryLinkUrl: 'https://docs.twenty.com',+ primaryLinkLabel: 'Documentation',+ secondaryLinks: [+ { url: 'https://www.twenty.com', label: 'Twenty Website' },+ ],+ },+ skipClose: true,+ });},};diff --git a/packages/twenty-front/src/modules/object-record/record-inline-cell/components/RecordInlineCell.tsx b/packages/twenty-front/src/modules/object-record/record-inline-cell/components/RecordInlineCell.tsxindex d8ad6ea7bba..d2cc5268a90 100644--- a/packages/twenty-front/src/modules/object-record/record-inline-cell/components/RecordInlineCell.tsx+++ b/packages/twenty-front/src/modules/object-record/record-inline-cell/components/RecordInlineCell.tsx@@ -98,12 +98,18 @@ export const RecordInlineCell = ({closeInlineCell();};- const handleSubmit: FieldInputEvent = ({ newValue, skipPersist }) => {+ const handleSubmit: FieldInputEvent = ({+ newValue,+ skipPersist,+ skipClose,+ }) => {if (skipPersist !== true) {persistFieldFromFieldInputContext(newValue);}- closeInlineCell();+ if (skipClose !== true) {+ closeInlineCell();+ }};const handleCancel = () => {diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFieldInput.tsxindex 79ddbe0dde7..317870665f5 100644--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFieldInput.tsx+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFieldInput.tsx@@ -34,12 +34,18 @@ export const RecordTableCellFieldInput = () => {onMoveFocus('down');};- const handleSubmit: FieldInputEvent = ({ newValue, skipPersist }) => {+ const handleSubmit: FieldInputEvent = ({+ newValue,+ skipPersist,+ skipClose,+ }) => {if (skipPersist !== true) {persistFieldFromFieldInputContext(newValue);}- onCloseTableCell();+ if (skipClose !== true) {+ onCloseTableCell();+ }};const handleCancel = () => {diff --git a/packages/twenty-front/src/modules/page-layout/widgets/field/components/FieldWidgetInlineCell.tsx b/packages/twenty-front/src/modules/page-layout/widgets/field/components/FieldWidgetInlineCell.tsxindex cf3af62fa7a..962074b9e68 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/field/components/FieldWidgetInlineCell.tsx+++ b/packages/twenty-front/src/modules/page-layout/widgets/field/components/FieldWidgetInlineCell.tsx@@ -97,12 +97,18 @@ export const FieldWidgetInlineCell = ({closeInlineCell();};- const handleSubmit: FieldInputEvent = ({ newValue, skipPersist }) => {+ const handleSubmit: FieldInputEvent = ({+ newValue,+ skipPersist,+ skipClose,+ }) => {if (skipPersist !== true) {persistFieldFromFieldInputContext(newValue);}- closeInlineCell();+ if (skipClose !== true) {+ closeInlineCell();+ }};const handleCancel = () => {diff --git a/packages/twenty-front/src/modules/page-layout/widgets/field/components/FieldWidgetInputContextProvider.tsx b/packages/twenty-front/src/modules/page-layout/widgets/field/components/FieldWidgetInputContextProvider.tsxindex 4678a3ffa21..45ee713a8ab 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/field/components/FieldWidgetInputContextProvider.tsx+++ b/packages/twenty-front/src/modules/page-layout/widgets/field/components/FieldWidgetInputContextProvider.tsx@@ -36,12 +36,18 @@ export const FieldWidgetInputContextProvider = ({closeInlineCell();};- const handleSubmit: FieldInputEvent = ({ newValue, skipPersist }) => {+ const handleSubmit: FieldInputEvent = ({+ newValue,+ skipPersist,+ skipClose,+ }) => {if (skipPersist !== true) {persistFieldFromFieldInputContext(newValue);}- closeInlineCell();+ if (skipClose !== true) {+ closeInlineCell();+ }};const handleCancel = () => {