Skip to content

The open alternative to Salesforce, designed for AI.

[Forms 4/5] Let Tab move through form text inputs (#26425)

Thomas des Francs4408b6b80766
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormArrayFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormArrayFieldInput.tsx
index b94fbc25447..c7f46029794 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormArrayFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormArrayFieldInput.tsx
@@ -9,7 +9,7 @@ import { ArrayFieldMenuItem } from '@/object-record/record-field/ui/meta-types/i
import { MultiItemBaseInput } from '@/object-record/record-field/ui/meta-types/input/components/MultiItemBaseInput';
import { type FieldArrayValue } from '@/object-record/record-field/ui/types/FieldMetadata';
import { ArrayDisplay } from '@/ui/field/display/components/ArrayDisplay';
-import { TextInput } from '@/ui/field/input/components/TextInput';
+import { StyledTextInput } from '@/ui/field/input/components/TextInput';
import { Field } from 'twenty-ui/primitives/input';
import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown';
import { LegacyDropdownContent } from '@/ui/layout/dropdown/components/LegacyDropdownContent';
@@ -25,8 +25,16 @@ import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/use
import { isStandaloneVariableString } from 'twenty-shared/workflow';
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
-import { isNonEmptyArray } from '@sniptt/guards';
-import { useContext, useId, useRef, useState } from 'react';
+import { isNonEmptyArray, isNonEmptyString } from '@sniptt/guards';
+import {
+ type FocusEvent,
+ type KeyboardEvent,
+ useContext,
+ useId,
+ useRef,
+ useState,
+} from 'react';
+import { Key } from 'ts-key-enum';
import { isDefined } from 'twenty-shared/utils';
import { IconPlus } from 'twenty-ui/icon';
import { ThemeContext, themeCssVariables } from 'twenty-ui/theme-constants';
@@ -156,20 +164,52 @@ export const FormArrayFieldInput = ({
setNewItemDraftValue(value);
};
- const handleFirstItemInputEnter = () => {
- if (isLimitReached) {
- return;
+ const commitFirstItemDraft = () => {
+ if (
+ isLimitReached ||
+ draftValue.type !== 'static' ||
+ !isNonEmptyString(newItemDraftValue.trim())
+ ) {
+ return false;
}
+ const updatedItems = [...draftValue.value, newItemDraftValue.trim()];
+
setDraftValue({
type: 'static',
- value: [...draftValue.value, newItemDraftValue],
+ value: updatedItems,
});
- onChange([...draftValue.value, newItemDraftValue]);
+ onChange(updatedItems);
setNewItemDraftValue('');
+ return true;
+ };
+
+ const handleRowBlur = (event: FocusEvent<HTMLDivElement>) => {
+ const isFocusMovingOutsideRow =
+ event.relatedTarget instanceof Node &&
+ !event.currentTarget.contains(event.relatedTarget);
+
+ if (isFocusMovingOutsideRow) {
+ commitFirstItemDraft();
+ }
+ };
+
+ const handleFirstItemInputKeyDown = (
+ event: KeyboardEvent<HTMLInputElement>,
+ ) => {
+ if (event.key !== Key.Enter || event.nativeEvent.isComposing) {
+ return;
+ }
+
+ event.stopPropagation();
+
+ if (!commitFirstItemDraft()) {
+ return;
+ }
+
openDropdown({
dropdownComponentInstanceIdFromProps: dropdownId,
});
@@ -217,7 +257,18 @@ export const FormArrayFieldInput = ({
});
};
- const handleNewItemInputBlur = () => {
+ const handleNewItemInputBlur = (event: FocusEvent<HTMLInputElement>) => {
+ const isFocusMovingToAnotherElement = event.relatedTarget instanceof Node;
+
+ if (
+ isAddingNewItem &&
+ isFocusMovingToAnotherElement &&
+ isNonEmptyString(inputValue.trim())
+ ) {
+ handleNewItemInputSubmit();
+ return;
+ }
+
removeFocusItemFromFocusStackById({
focusId: newItemInputInstanceId,
});
@@ -313,7 +364,7 @@ export const FormArrayFieldInput = ({
<FormFieldInputContainer data-testid={testId}>
{label ? <Field.Label>{label}</Field.Label> : null}
- <FormFieldInputRowContainer>
+ <FormFieldInputRowContainer onBlur={handleRowBlur}>
<FormFieldInputInnerContainer
formFieldInputInstanceId={formFieldInputInstanceId}
preventFocusStackUpdate={preventContainerFocusStackUpdate}
@@ -332,14 +383,14 @@ export const FormArrayFieldInput = ({
</StyledDisplayModeReadonlyContainer>
) : draftValue.value.length === 0 ? (
<StyledInputContainer>
- <TextInput
- instanceId={formFieldInputInstanceId}
+ <StyledTextInput
+ autoComplete="off"
placeholder={t`Enter an item`}
value={newItemDraftValue}
- copyButton={false}
- onChange={handleFirstItemInputChange}
- onEnter={handleFirstItemInputEnter}
- shouldTrim={false}
+ onChange={(event) =>
+ handleFirstItemInputChange(event.target.value)
+ }
+ onKeyDown={handleFirstItemInputKeyDown}
/>
</StyledInputContainer>
) : (
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormNumberFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormNumberFieldInput.tsx
index 8c8b0422361..5f5c6a83e74 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormNumberFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormNumberFieldInput.tsx
@@ -3,7 +3,7 @@ import { FormFieldInputInnerContainer } from '@/object-record/record-field/ui/fo
import { FormFieldInputRowContainer } from '@/object-record/record-field/ui/form-types/components/FormFieldInputRowContainer';
import { VariableChipStandalone } from '@/object-record/record-field/ui/form-types/components/VariableChipStandalone';
import { type VariablePickerComponent } from '@/object-record/record-field/ui/form-types/types/VariablePickerComponent';
-import { TextInput } from '@/ui/field/input/components/TextInput';
+import { StyledTextInput } from '@/ui/field/input/components/TextInput';
import { Field } from 'twenty-ui/primitives/input';
import { isStandaloneVariableString } from 'twenty-shared/workflow';
import { t } from '@lingui/core/macro';
@@ -67,6 +67,22 @@ export const FormNumberFieldInput = ({
value: isDefined(defaultValue) ? String(defaultValue) : '',
};
+ const [inputText, setInputText] = useState(draftValue.value);
+ const [previousDraftText, setPreviousDraftText] = useState(draftValue.value);
+
+ const trimmedInputText = inputText.trim();
+ const isInputTextShowingDraftValue =
+ canBeCastAsNumberOrNull(trimmedInputText) &&
+ String(castAsNumberOrNull(trimmedInputText) ?? '') === draftValue.value;
+
+ if (previousDraftText !== draftValue.value) {
+ setPreviousDraftText(draftValue.value);
+
+ if (!isInputTextShowingDraftValue) {
+ setInputText(draftValue.value);
+ }
+ }
+
const persistNumber = (newValue: string) => {
if (!canBeCastAsNumberOrNull(newValue)) {
setErrorMessage(t`Invalid number`);
@@ -83,6 +99,7 @@ export const FormNumberFieldInput = ({
};
const handleChange = (newText: string) => {
+ setInputText(newText);
persistNumber(newText.trim());
};
@@ -107,16 +124,16 @@ export const FormNumberFieldInput = ({
onBlur={onBlur}
>
{draftValue.type === 'static' ? (
- <TextInput
- instanceId={instanceId}
+ <StyledTextInput
+ id={instanceId}
+ autoComplete="off"
placeholder={
isDefined(placeholder) && !isEmpty(placeholder)
? placeholder
: t`Enter a number`
}
- value={draftValue.value}
- copyButton={false}
- onChange={handleChange}
+ value={inputText}
+ onChange={(event) => handleChange(event.target.value)}
disabled={readonly}
/>
) : (
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormUuidFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormUuidFieldInput.tsx
index 22e11f4bf40..31910d346f5 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormUuidFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormUuidFieldInput.tsx
@@ -3,7 +3,7 @@ import { FormFieldInputInnerContainer } from '@/object-record/record-field/ui/fo
import { FormFieldInputRowContainer } from '@/object-record/record-field/ui/form-types/components/FormFieldInputRowContainer';
import { VariableChipStandalone } from '@/object-record/record-field/ui/form-types/components/VariableChipStandalone';
import { type VariablePickerComponent } from '@/object-record/record-field/ui/form-types/types/VariablePickerComponent';
-import { TextInput } from '@/ui/field/input/components/TextInput';
+import { StyledTextInput } from '@/ui/field/input/components/TextInput';
import { Field } from 'twenty-ui/primitives/input';
import { isStandaloneVariableString } from 'twenty-shared/workflow';
import { t } from '@lingui/core/macro';
@@ -95,13 +95,13 @@ export const FormUuidFieldInput = ({
hasRightElement={isDefined(VariablePicker) && !readonly}
>
{draftValue.type === 'static' ? (
- <TextInput
- instanceId={instanceId}
+ <StyledTextInput
+ id={instanceId}
+ autoComplete="off"
placeholder={placeholder ?? t`Enter a UUID`}
value={draftValue.value}
- copyButton={false}
disabled={readonly}
- onChange={handleChange}
+ onChange={(event) => handleChange(event.target.value)}
/>
) : (
<VariableChipStandalone
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__tests__/FormArrayFieldInput.test.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__tests__/FormArrayFieldInput.test.tsx
new file mode 100644
index 00000000000..d26d09e4bd3
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__tests__/FormArrayFieldInput.test.tsx
@@ -0,0 +1,152 @@
+import { FormArrayFieldInput } from '@/object-record/record-field/ui/form-types/components/FormArrayFieldInput';
+import { FormLinksFieldInput } from '@/object-record/record-field/ui/form-types/components/FormLinksFieldInput';
+import { i18n } from '@lingui/core';
+import { I18nProvider } from '@lingui/react';
+import { render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { createStore, Provider } from 'jotai';
+import { type ReactNode } from 'react';
+
+const renderWithProviders = (children: ReactNode) => {
+ const store = createStore();
+
+ return render(children, {
+ wrapper: ({ children: wrappedChildren }) => (
+ <I18nProvider i18n={i18n}>
+ <Provider store={store}>{wrappedChildren}</Provider>
+ </I18nProvider>
+ ),
+ });
+};
+
+const renderArrayField = ({
+ defaultValue = [],
+}: { defaultValue?: string[] } = {}) => {
+ const onChange = jest.fn();
+ renderWithProviders(
+ <>
+ <button>Before</button>
+ <FormArrayFieldInput defaultValue={defaultValue} onChange={onChange} />
+ <button>After</button>
+ </>,
+ );
+
+ return { onChange };
+};
+
+it.each([
+ ['Tab', { shift: false }, 'After'],
+ ['Shift+Tab', { shift: true }, 'Before'],
+])(
+ 'adds the typed first item when %s leaves the array field',
+ async (_key, tabOptions, nextButtonName) => {
+ const user = userEvent.setup();
+ const { onChange } = renderArrayField();
+ const itemInput = screen.getByPlaceholderText('Enter an item');
+ await user.click(itemInput);
+ await user.type(itemInput, ' Draft item ');
+
+ await user.tab(tabOptions);
+
+ expect(screen.getByRole('button', { name: nextButtonName })).toHaveFocus();
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith(['Draft item']);
+ expect(screen.getByText('Draft item')).toBeInTheDocument();
+ },
+);
+
+it('trims the first item added with Enter', async () => {
+ const user = userEvent.setup();
+ const { onChange } = renderArrayField();
+ const itemInput = screen.getByPlaceholderText('Enter an item');
+ await user.click(itemInput);
+ await user.type(itemInput, ' Draft item ');
+
+ await user.keyboard('{Enter}');
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith(['Draft item']);
+});
+
+it('adds the typed first item when focus moves to another element', async () => {
+ const user = userEvent.setup();
+ const { onChange } = renderArrayField();
+ const itemInput = screen.getByPlaceholderText('Enter an item');
+ await user.click(itemInput);
+ await user.type(itemInput, 'Draft item');
+
+ await user.click(screen.getByRole('button', { name: 'After' }));
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith(['Draft item']);
+});
+
+it('keeps the typed first item in the input when focus leaves to nothing focusable', async () => {
+ const user = userEvent.setup();
+ const { onChange } = renderArrayField();
+ const itemInput = screen.getByPlaceholderText('Enter an item');
+ await user.click(itemInput);
+ await user.type(itemInput, 'Draft item');
+
+ await user.click(document.body);
+
+ expect(onChange).not.toHaveBeenCalled();
+ expect(itemInput).toHaveValue('Draft item');
+});
+
+it('adds the typed new item when focus moves to another element', async () => {
+ const user = userEvent.setup();
+ const { onChange } = renderArrayField({ defaultValue: ['First item'] });
+ await user.click(screen.getByText('First item'));
+ await user.click(await screen.findByText('Add item'));
+ await user.type(screen.getByRole('textbox'), 'Second item');
+
+ await user.click(screen.getByRole('button', { name: 'After' }));
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith(['First item', 'Second item']);
+});
+
+it('keeps a blank first item draft in place without adding it when Tab leaves the field', async () => {
+ const user = userEvent.setup();
+ const { onChange } = renderArrayField();
+ const itemInput = screen.getByPlaceholderText('Enter an item');
+ await user.click(itemInput);
+ await user.type(itemInput, ' ');
+
+ await user.tab();
+ expect(screen.getByRole('button', { name: 'After' })).toHaveFocus();
+ await user.tab({ shift: true });
+
+ expect(onChange).not.toHaveBeenCalled();
+ expect(itemInput).toHaveFocus();
+ expect(itemInput).toHaveValue(' ');
+});
+
+it('adds the typed secondary link when Tab leaves the links field', async () => {
+ const user = userEvent.setup();
+ const onChange = jest.fn();
+ renderWithProviders(
+ <>
+ <FormLinksFieldInput
+ label="Links"
+ defaultValue={undefined}
+ onChange={onChange}
+ />
+ <button>Next field</button>
+ </>,
+ );
+ const secondaryLinksInput = screen.getByPlaceholderText('Enter an item');
+ await user.click(secondaryLinksInput);
+ await user.type(secondaryLinksInput, 'twenty.com');
+
+ await user.tab();
+
+ expect(screen.getByRole('button', { name: 'Next field' })).toHaveFocus();
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith({
+ primaryLinkLabel: '',
+ primaryLinkUrl: '',
+ secondaryLinks: [{ url: 'twenty.com', label: null }],
+ });
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__tests__/FormFieldInputTabNavigation.test.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__tests__/FormFieldInputTabNavigation.test.tsx
new file mode 100644
index 00000000000..863b09fd180
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__tests__/FormFieldInputTabNavigation.test.tsx
@@ -0,0 +1,76 @@
+import { FormArrayFieldInput } from '@/object-record/record-field/ui/form-types/components/FormArrayFieldInput';
+import { FormNumberFieldInput } from '@/object-record/record-field/ui/form-types/components/FormNumberFieldInput';
+import { FormLinksFieldInput } from '@/object-record/record-field/ui/form-types/components/FormLinksFieldInput';
+import { i18n } from '@lingui/core';
+import { I18nProvider } from '@lingui/react';
+import { render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { type ReactNode } from 'react';
+
+const I18nWrapper = ({ children }: { children: ReactNode }) => (
+ <I18nProvider i18n={i18n}>{children}</I18nProvider>
+);
+
+it('tabs between number inputs used in workflow nodes in both directions', async () => {
+ const user = userEvent.setup();
+ render(
+ <>
+ <FormNumberFieldInput defaultValue={1} onChange={() => {}} />
+ <FormNumberFieldInput defaultValue={2} onChange={() => {}} />
+ <button>Save</button>
+ </>,
+ );
+ const first = screen.getByDisplayValue('1');
+ const second = screen.getByDisplayValue('2');
+ await user.click(first);
+ await user.tab();
+ expect(second).toHaveFocus();
+ await user.tab({ shift: true });
+ expect(first).toHaveFocus();
+ await user.tab();
+ await user.tab();
+ expect(screen.getByRole('button', { name: 'Save' })).toHaveFocus();
+});
+
+it('tabs out of an empty array field in both directions', async () => {
+ const user = userEvent.setup();
+ render(
+ <>
+ <button>Before</button>
+ <FormArrayFieldInput defaultValue={[]} onChange={() => {}} />
+ <button>After</button>
+ </>,
+ { wrapper: I18nWrapper },
+ );
+ const itemInput = screen.getByPlaceholderText('Enter an item');
+ await user.click(itemInput);
+ await user.tab();
+ expect(screen.getByRole('button', { name: 'After' })).toHaveFocus();
+ await user.tab({ shift: true });
+ expect(itemInput).toHaveFocus();
+ await user.tab({ shift: true });
+ expect(screen.getByRole('button', { name: 'Before' })).toHaveFocus();
+});
+
+it('tabs through a links field and past its empty secondary links', async () => {
+ const user = userEvent.setup();
+ render(
+ <>
+ <FormLinksFieldInput
+ label="Links"
+ defaultValue={undefined}
+ onChange={() => {}}
+ />
+ <button>Next field</button>
+ </>,
+ { wrapper: I18nWrapper },
+ );
+ const secondaryLinksInput = screen.getByPlaceholderText('Enter an item');
+ await user.click(secondaryLinksInput);
+ await user.tab();
+ expect(screen.getByRole('button', { name: 'Next field' })).toHaveFocus();
+ await user.tab({ shift: true });
+ expect(secondaryLinksInput).toHaveFocus();
+ await user.tab({ shift: true });
+ expect(document.activeElement).toHaveAttribute('contenteditable', 'true');
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__tests__/FormNumberFieldInput.test.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__tests__/FormNumberFieldInput.test.tsx
new file mode 100644
index 00000000000..0699392ebeb
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__tests__/FormNumberFieldInput.test.tsx
@@ -0,0 +1,57 @@
+import { FormNumberFieldInput } from '@/object-record/record-field/ui/form-types/components/FormNumberFieldInput';
+import { render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { useState } from 'react';
+
+const NumberFieldWithParentState = ({
+ initialValue,
+}: {
+ initialValue?: number;
+}) => {
+ const [value, setValue] = useState<number | string | undefined>(initialValue);
+
+ return (
+ <>
+ <FormNumberFieldInput
+ defaultValue={value}
+ onChange={(newValue) => setValue(newValue ?? undefined)}
+ />
+ <button onClick={() => setValue(1)}>Reset to 1</button>
+ </>
+ );
+};
+
+it('keeps the decimal separator while a number is being typed', async () => {
+ const user = userEvent.setup();
+ render(<NumberFieldWithParentState />);
+ const input = screen.getByPlaceholderText('Enter a number');
+
+ await user.type(input, '1.5');
+
+ expect(input).toHaveValue('1.5');
+});
+
+it('keeps the decimal separator when it replaces part of an existing number', async () => {
+ const user = userEvent.setup();
+ render(<NumberFieldWithParentState initialValue={15} />);
+ const input = screen.getByDisplayValue('15');
+
+ await user.type(input, '.', {
+ initialSelectionStart: 1,
+ initialSelectionEnd: 2,
+ });
+
+ expect(input).toHaveValue('1.');
+});
+
+it('shows the value set by the parent after the user typed', async () => {
+ const user = userEvent.setup();
+ render(<NumberFieldWithParentState initialValue={5} />);
+ const input = screen.getByDisplayValue('5');
+ await user.type(input, '2');
+ expect(input).toHaveValue('52');
+
+ await user.click(screen.getByRole('button', { name: 'Reset to 1' }));
+
+ expect(input).toHaveValue('1');
+});