The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Point the record creation form at blocked fields and keep its shortcuts working (#28108)
Thomas Trompettef30cedda4f0c
diff --git a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsxindex bb2fc48bacb..f1c09cec8b8 100644--- a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx+++ b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/SidePanelRecordCreationFormPage.tsx@@ -31,6 +31,7 @@ import { groupValidationRuleViolationsByField } from '@/validation-rules/utils/gimport { useAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentState';import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';import { useId, useState } from 'react';+import { flushSync } from 'react-dom';import { styled } from '@linaria/react';import { t } from '@lingui/core/macro';import { Key } from 'ts-key-enum';@@ -53,6 +54,7 @@ const StyledContainer = styled.div`display: flex;flex-direction: column;height: 100%;+ outline: none;`;const StyledValidationRuleErrors = styled.div`@@ -128,6 +130,7 @@ const SidePanelRecordCreationForm = ({const [isSubmitting, setIsSubmitting] = useState(false);const [formResetCount, setFormResetCount] = useState(0);+ const contentId = useId();const optionsDropdownId = useId();const [draftValidationRuleViolations, setDraftValidationRuleViolations] =useState<ValidationRuleViolation[]>([]);@@ -228,6 +231,12 @@ const SidePanelRecordCreationForm = ({}};+ const scrollToFirstFieldViolation = () =>+ document+ .getElementById(contentId)+ ?.querySelector('[role="alert"]')+ ?.scrollIntoView({ behavior: 'smooth', block: 'center' });+const submitDraftRecord = async ({shouldKeepOpen,}: {@@ -242,13 +251,16 @@ const SidePanelRecordCreationForm = ({if (validationRules.length > 0) {const draftViolations = computeViolations(draftRecord);- setDraftValidationRuleViolations(draftViolations);+ flushSync(() => {+ setDraftValidationRuleViolations(draftViolations);- revealHiddenFieldsIfTargeted(- draftViolations.map((violation) => violation.fieldMetadataId),- );+ revealHiddenFieldsIfTargeted(+ draftViolations.map((violation) => violation.fieldMetadataId),+ );+ });if (draftViolations.length > 0) {+ scrollToFirstFieldViolation();return;}}@@ -269,6 +281,7 @@ const SidePanelRecordCreationForm = ({if (shouldKeepOpen) {setRecordCreationFormDraft(null);setDraftValidationRuleViolations([]);+ setRecordCreationFormAreHiddenFieldsShown(false);setFormResetCount((previousFormResetCount) => previousFormResetCount + 1,);@@ -283,16 +296,20 @@ const SidePanelRecordCreationForm = ({const serverViolations = getValidationRuleViolationsFromError(error);- setServerValidationRuleViolations(serverViolations);+ flushSync(() => {+ setServerValidationRuleViolations(serverViolations);++ revealHiddenFieldsIfTargeted(+ serverViolations.map((violation) => violation.fieldMetadataId),+ );+ });if (serverViolations.length === 0) {enqueueToast(getToastOptionsFromError({ error }));return;}- revealHiddenFieldsIfTargeted(- serverViolations.map((violation) => violation.fieldMetadataId),- );+ scrollToFirstFieldViolation();} finally {setIsSubmitting(false);}@@ -316,7 +333,7 @@ const SidePanelRecordCreationForm = ({});return (- <StyledContainer ref={containerRef}>+ <StyledContainer ref={containerRef} tabIndex={-1}><PageCardHeadertitle={<HeaderIdentifier@@ -331,7 +348,7 @@ const SidePanelRecordCreationForm = ({/>}/>- <StyledContent key={formResetCount}>+ <StyledContent key={formResetCount} id={contentId}><RecordFormFieldInputsobjectMetadataItem={objectMetadataItem}fieldMetadataItems={visibleFieldMetadataItems}diff --git a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsxindex 949f88ee84e..a8041472ffb 100644--- a/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx+++ b/packages/twenty-front/src/modules/side-panel/pages/record-creation-form/components/__tests__/SidePanelRecordCreationFormPage.test.tsx@@ -1,7 +1,7 @@import { CombinedGraphQLErrors } from '@apollo/client/errors';import { i18n } from '@lingui/core';import { I18nProvider } from '@lingui/react';-import { render, screen, within } from '@testing-library/react';+import { render, screen, waitFor, within } from '@testing-library/react';import userEvent from '@testing-library/user-event';import { Provider as JotaiProvider } from 'jotai';import { type createElement, type Fragment, type ReactNode } from 'react';@@ -101,6 +101,7 @@ let mockValidationRules: ValidationRule[] = [];const openRecordCreationFormSettingsInSidePanel = jest.fn();const mockHasPermissionFlag = jest.fn();const mockEnqueueToast = jest.fn();+const scrollIntoView = jest.fn();jest.mock('twenty-ui/components/feedback', () => ({...jest.requireActual('twenty-ui/components/feedback'),@@ -291,6 +292,7 @@ const renderPage = () =>describe('SidePanelRecordCreationFormPage', () => {beforeEach(() => {jest.clearAllMocks();+ Element.prototype.scrollIntoView = scrollIntoView;resetJotaiStore();mockValidationRules = [];settleRecordCreationDraft.mockResolvedValue({});@@ -375,13 +377,15 @@ describe('SidePanelRecordCreationFormPage', () => {await user.click(screen.getByTestId('record-creation-form-create-button'));- expect(- within(screen.getByRole('group', { name: 'Nickname field' })).getByRole(- 'alert',- ),- ).toHaveTextContent('A company needs a nickname');+ const nicknameAlert = within(+ screen.getByRole('group', { name: 'Nickname field' }),+ ).getByRole('alert');++ expect(nicknameAlert).toHaveTextContent('A company needs a nickname');expect(screen.getByLabelText('Nickname')).toBeInTheDocument();expect(settleRecordCreationDraft).not.toHaveBeenCalled();+ await waitFor(() => expect(scrollIntoView).toHaveBeenCalledTimes(1));+ expect(scrollIntoView.mock.contexts[0]).toBe(nicknameAlert);});it('keeps hidden fields collapsed and shows a validation error on the whole record below the fields', async () => {@@ -418,12 +422,15 @@ describe('SidePanelRecordCreationFormPage', () => {shouldKeepOpen: false,});expect(screen.getByLabelText('Nickname')).toBeInTheDocument();- expect(- within(screen.getByRole('group', { name: 'Nickname field' })).getByRole(- 'alert',- ),- ).toHaveTextContent('A company needs a nickname');++ const nicknameAlert = within(+ screen.getByRole('group', { name: 'Nickname field' }),+ ).getByRole('alert');++ expect(nicknameAlert).toHaveTextContent('A company needs a nickname');expect(mockEnqueueToast).not.toHaveBeenCalled();+ await waitFor(() => expect(scrollIntoView).toHaveBeenCalledTimes(1));+ expect(scrollIntoView.mock.contexts[0]).toBe(nicknameAlert);});it('keeps a message from the server under its field while the user edits, until the next create', async () => {@@ -540,4 +547,23 @@ describe('SidePanelRecordCreationFormPage', () => {expect.objectContaining({ status: 'success' }),);});++ it('collapses the hidden fields again after creating another record', async () => {+ const user = userEvent.setup();++ renderPage();++ await user.click(screen.getByRole('button', { name: 'All fields (3)' }));+ await user.click(screen.getByRole('button', { name: 'Options' }));+ await user.click(+ await screen.findByRole('menuitem', { name: /Create another/ }),+ );++ expect(+ await screen.findByRole('button', { name: 'All fields (3)' }),+ ).toBeInTheDocument();+ expect(+ screen.queryByRole('button', { name: 'Hide fields' }),+ ).not.toBeInTheDocument();+ });});