Skip to content

The open alternative to Salesforce, designed for AI.

Let buttons inside field inputs handle Enter (#27150)

Raphaël Bosib372c98abf72
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/__stories__/DateTimeFieldInput.stories.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/__stories__/DateTimeFieldInput.stories.tsx
index bccbd441f46..ebfd14b352d 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/__stories__/DateTimeFieldInput.stories.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/__stories__/DateTimeFieldInput.stories.tsx
@@ -230,6 +230,7 @@ export const Enter: Story = {
).not.toBeInTheDocument();
});
await expect(handleEnterMocked).not.toHaveBeenCalled();
+ await userEvent.click(await canvas.findByPlaceholderText(/HH:mm/));
await userEvent.keyboard('{enter}');
await expect(handleEnterMocked).toHaveBeenCalledTimes(1);
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/hooks/__tests__/useRegisterInputEvents.test.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/hooks/__tests__/useRegisterInputEvents.test.tsx
new file mode 100644
index 00000000000..01855163f1f
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/hooks/__tests__/useRegisterInputEvents.test.tsx
@@ -0,0 +1,126 @@
+import { act, render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { useRef } from 'react';
+
+import { useRegisterInputEvents } from '@/object-record/record-field/ui/meta-types/input/hooks/useRegisterInputEvents';
+import { focusStackState } from '@/ui/utilities/focus/states/focusStackState';
+import { FocusComponentType } from '@/ui/utilities/focus/types/FocusComponentType';
+import { DEFAULT_GLOBAL_HOTKEYS_CONFIG } from '@/ui/utilities/hotkey/constants/DefaultGlobalHotkeysConfig';
+import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper';
+
+const FIELD_INPUT_FOCUS_ID = 'field-input';
+
+type FieldInputWithControlsProps = {
+ onEnter: (inputValue: string) => void;
+ onEscape: (inputValue: string) => void;
+ onCopyClick: () => void;
+};
+
+const FieldInputWithControls = ({
+ onEnter,
+ onEscape,
+ onCopyClick,
+}: FieldInputWithControlsProps) => {
+ const fieldInputRef = useRef<HTMLDivElement>(null);
+
+ useRegisterInputEvents({
+ focusId: FIELD_INPUT_FOCUS_ID,
+ inputRef: fieldInputRef,
+ inputValue: 'Acme',
+ onEnter,
+ onEscape,
+ });
+
+ return (
+ <div ref={fieldInputRef}>
+ <input aria-label="Name" defaultValue="Acme" />
+ <button type="button" onClick={onCopyClick}>
+ Copy
+ </button>
+ <div role="button" tabIndex={0}>
+ Country
+ </div>
+ <a href="#website">Website</a>
+ </div>
+ );
+};
+
+const renderFieldInputWithControls = () => {
+ const onEnter = jest.fn();
+ const onEscape = jest.fn();
+ const onCopyClick = jest.fn();
+ const BaseWrapper = getJestMetadataAndApolloMocksWrapper({
+ onInitializeJotaiStore: (store) => {
+ store.set(focusStackState.atom, [
+ {
+ focusId: FIELD_INPUT_FOCUS_ID,
+ componentInstance: {
+ componentType: FocusComponentType.OPENED_FIELD_INPUT,
+ componentInstanceId: FIELD_INPUT_FOCUS_ID,
+ },
+ globalHotkeysConfig: DEFAULT_GLOBAL_HOTKEYS_CONFIG,
+ },
+ ]);
+ },
+ });
+
+ render(
+ <FieldInputWithControls
+ onEnter={onEnter}
+ onEscape={onEscape}
+ onCopyClick={onCopyClick}
+ />,
+ { wrapper: BaseWrapper },
+ );
+
+ return { onEnter, onEscape, onCopyClick };
+};
+
+describe('useRegisterInputEvents', () => {
+ it('submits with Enter from the text input', async () => {
+ const user = userEvent.setup();
+ const { onEnter } = renderFieldInputWithControls();
+
+ act(() => screen.getByRole('textbox', { name: 'Name' }).focus());
+ await user.keyboard('{Enter}');
+
+ expect(onEnter).toHaveBeenCalledWith('Acme');
+ });
+
+ it('lets a native button handle Enter instead of submitting', async () => {
+ const user = userEvent.setup();
+ const { onEnter, onCopyClick } = renderFieldInputWithControls();
+
+ act(() => screen.getByRole('button', { name: 'Copy' }).focus());
+ await user.keyboard('{Enter}');
+
+ expect(onCopyClick).toHaveBeenCalledTimes(1);
+ expect(onEnter).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ [
+ 'a role="button" element',
+ () => screen.getByRole('button', { name: 'Country' }),
+ ],
+ ['a link', () => screen.getByRole('link', { name: 'Website' })],
+ ])('does not submit with Enter on %s', async (_, getFocusedControl) => {
+ const user = userEvent.setup();
+ const { onEnter } = renderFieldInputWithControls();
+
+ act(() => getFocusedControl().focus());
+ await user.keyboard('{Enter}');
+
+ expect(onEnter).not.toHaveBeenCalled();
+ });
+
+ it('still closes with Escape on a native button', async () => {
+ const user = userEvent.setup();
+ const { onEscape } = renderFieldInputWithControls();
+
+ act(() => screen.getByRole('button', { name: 'Copy' }).focus());
+ await user.keyboard('{Escape}');
+
+ expect(onEscape).toHaveBeenCalledWith('Acme');
+ });
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/hooks/useRegisterInputEvents.ts b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/hooks/useRegisterInputEvents.ts
index 972e8d7fc98..b57b6f0d47b 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/hooks/useRegisterInputEvents.ts
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/hooks/useRegisterInputEvents.ts
@@ -1,6 +1,7 @@
import { Key } from 'ts-key-enum';
import { useHotkeysOnFocusedElement } from '@/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement';
+import { isKeyboardEventTargetActivatedByEnter } from '@/ui/utilities/hotkey/utils/isKeyboardEventTargetActivatedByEnter';
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';
import { isDefined } from 'twenty-shared/utils';
@@ -41,6 +42,9 @@ export const useRegisterInputEvents = <T>({
},
focusId,
dependencies: [onEnter, inputValue],
+ options: {
+ ignoreEventWhen: isKeyboardEventTargetActivatedByEnter,
+ },
});
useHotkeysOnFocusedElement({
diff --git a/packages/twenty-front/src/modules/ui/field/input/components/__tests__/DateInput.test.tsx b/packages/twenty-front/src/modules/ui/field/input/components/__tests__/DateInput.test.tsx
new file mode 100644
index 00000000000..ada462a9dfc
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/field/input/components/__tests__/DateInput.test.tsx
@@ -0,0 +1,86 @@
+import { act, render, screen, within } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { i18n } from '@lingui/core';
+import { I18nProvider } from '@lingui/react';
+import { type ReactNode } from 'react';
+
+import { DateInput } from '@/ui/field/input/components/DateInput';
+import { focusStackState } from '@/ui/utilities/focus/states/focusStackState';
+import { FocusComponentType } from '@/ui/utilities/focus/types/FocusComponentType';
+import { DEFAULT_GLOBAL_HOTKEYS_CONFIG } from '@/ui/utilities/hotkey/constants/DefaultGlobalHotkeysConfig';
+import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper';
+
+const DATE_INPUT_INSTANCE_ID = 'date-input';
+
+const renderDateInput = () => {
+ const onEnter = jest.fn();
+ const onChange = jest.fn();
+ const BaseWrapper = getJestMetadataAndApolloMocksWrapper({
+ onInitializeJotaiStore: (store) => {
+ store.set(focusStackState.atom, [
+ {
+ focusId: DATE_INPUT_INSTANCE_ID,
+ componentInstance: {
+ componentType: FocusComponentType.OPENED_FIELD_INPUT,
+ componentInstanceId: DATE_INPUT_INSTANCE_ID,
+ },
+ globalHotkeysConfig: DEFAULT_GLOBAL_HOTKEYS_CONFIG,
+ },
+ ]);
+ },
+ });
+
+ const Wrapper = ({ children }: { children: ReactNode }) => (
+ <I18nProvider i18n={i18n}>
+ <BaseWrapper>{children}</BaseWrapper>
+ </I18nProvider>
+ );
+
+ render(
+ <DateInput
+ instanceId={DATE_INPUT_INSTANCE_ID}
+ value="2026-01-15"
+ onEnter={onEnter}
+ onEscape={jest.fn()}
+ onClickOutside={jest.fn()}
+ onChange={onChange}
+ />,
+ { wrapper: Wrapper },
+ );
+
+ return { onEnter, onChange };
+};
+
+describe('DateInput keyboard submission', () => {
+ it.each([
+ ['Next', '2026-02-15'],
+ ['Previous', '2025-12-15'],
+ ])(
+ 'pages the calendar with Enter on the %s button without submitting',
+ async (buttonName, expectedDate) => {
+ const user = userEvent.setup();
+ const { onEnter, onChange } = renderDateInput();
+ const pageButton = await screen.findByRole('button', {
+ name: buttonName,
+ });
+
+ act(() => pageButton.focus());
+ await user.keyboard('{Enter}');
+
+ expect(onChange).toHaveBeenCalledWith(expectedDate);
+ expect(onEnter).not.toHaveBeenCalled();
+ },
+ );
+
+ it('submits the date with Enter from the date text input', async () => {
+ const user = userEvent.setup();
+ const { onEnter } = renderDateInput();
+ const calendar = await screen.findByRole('dialog', { name: 'Choose Date' });
+ const dateTextInput = within(calendar).getByRole('textbox');
+
+ act(() => dateTextInput.focus());
+ await user.keyboard('{Enter}');
+
+ expect(onEnter).toHaveBeenCalledWith('2026-01-15');
+ });
+});
diff --git a/packages/twenty-front/src/modules/ui/field/input/components/__tests__/DateTimeInput.test.tsx b/packages/twenty-front/src/modules/ui/field/input/components/__tests__/DateTimeInput.test.tsx
new file mode 100644
index 00000000000..b31871339b6
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/field/input/components/__tests__/DateTimeInput.test.tsx
@@ -0,0 +1,101 @@
+import { act, render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { i18n } from '@lingui/core';
+import { I18nProvider } from '@lingui/react';
+import { type ReactNode } from 'react';
+import { Temporal } from 'temporal-polyfill';
+
+import { DateTimeInput } from '@/ui/field/input/components/DateTimeInput';
+import { focusStackState } from '@/ui/utilities/focus/states/focusStackState';
+import { FocusComponentType } from '@/ui/utilities/focus/types/FocusComponentType';
+import { DEFAULT_GLOBAL_HOTKEYS_CONFIG } from '@/ui/utilities/hotkey/constants/DefaultGlobalHotkeysConfig';
+import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper';
+
+const DATE_TIME_INPUT_INSTANCE_ID = 'date-time-input';
+
+const renderDateTimeInput = () => {
+ const onEnter = jest.fn();
+ const onChange = jest.fn();
+ const BaseWrapper = getJestMetadataAndApolloMocksWrapper({
+ onInitializeJotaiStore: (store) => {
+ store.set(focusStackState.atom, [
+ {
+ focusId: DATE_TIME_INPUT_INSTANCE_ID,
+ componentInstance: {
+ componentType: FocusComponentType.OPENED_FIELD_INPUT,
+ componentInstanceId: DATE_TIME_INPUT_INSTANCE_ID,
+ },
+ globalHotkeysConfig: DEFAULT_GLOBAL_HOTKEYS_CONFIG,
+ },
+ ]);
+ },
+ });
+
+ const Wrapper = ({ children }: { children: ReactNode }) => (
+ <I18nProvider i18n={i18n}>
+ <BaseWrapper>{children}</BaseWrapper>
+ </I18nProvider>
+ );
+
+ render(
+ <DateTimeInput
+ instanceId={DATE_TIME_INPUT_INSTANCE_ID}
+ value={Temporal.Instant.from('2026-01-15T10:00:00Z')}
+ onEnter={onEnter}
+ onEscape={jest.fn()}
+ onClickOutside={jest.fn()}
+ onChange={onChange}
+ />,
+ { wrapper: Wrapper },
+ );
+
+ return { onEnter, onChange };
+};
+
+describe('DateTimeInput keyboard submission', () => {
+ it.each([
+ ['Next', '2026-02-15T10:00:00Z'],
+ ['Previous', '2025-12-15T10:00:00Z'],
+ ])(
+ 'pages the calendar with Enter on the %s button without submitting',
+ async (buttonName, expectedInstant) => {
+ const user = userEvent.setup();
+ const { onEnter, onChange } = renderDateTimeInput();
+ const pageButton = await screen.findByRole('button', {
+ name: buttonName,
+ });
+
+ act(() => pageButton.focus());
+ await user.keyboard('{Enter}');
+
+ expect(onChange.mock.lastCall?.[0]?.toString()).toBe(expectedInstant);
+ expect(onEnter).not.toHaveBeenCalled();
+ },
+ );
+
+ it('opens the month and year panel with Enter without submitting', async () => {
+ const user = userEvent.setup();
+ const { onEnter } = renderDateTimeInput();
+ const monthYearTrigger = await screen.findByRole('button', {
+ name: 'Select month and year',
+ });
+
+ act(() => monthYearTrigger.focus());
+ await user.keyboard('{Enter}');
+
+ expect(monthYearTrigger).toHaveAttribute('aria-expanded', 'true');
+ expect(await screen.findByText('January')).toBeVisible();
+ expect(onEnter).not.toHaveBeenCalled();
+ });
+
+ it('submits the date with Enter from the time input', async () => {
+ const user = userEvent.setup();
+ const { onEnter } = renderDateTimeInput();
+ const timeInput = await screen.findByPlaceholderText(/HH:mm/);
+
+ act(() => timeInput.focus());
+ await user.keyboard('{Enter}');
+
+ expect(onEnter.mock.lastCall?.[0]?.toString()).toBe('2026-01-15T10:00:00Z');
+ });
+});
diff --git a/packages/twenty-front/src/modules/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement.ts b/packages/twenty-front/src/modules/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement.ts
index f8d0f0364a2..161fc85aa04 100644
--- a/packages/twenty-front/src/modules/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement.ts
+++ b/packages/twenty-front/src/modules/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement.ts
@@ -71,6 +71,7 @@ export const useHotkeysOnFocusedElement = ({
enableOnContentEditable,
enableOnFormTags,
ignoreModifiers,
+ ignoreEventWhen: options?.ignoreEventWhen,
},
dependencies,
);
diff --git a/packages/twenty-front/src/modules/ui/utilities/hotkey/utils/__tests__/isKeyboardEventTargetActivatedByEnter.test.ts b/packages/twenty-front/src/modules/ui/utilities/hotkey/utils/__tests__/isKeyboardEventTargetActivatedByEnter.test.ts
new file mode 100644
index 00000000000..c581aae6cc2
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/utilities/hotkey/utils/__tests__/isKeyboardEventTargetActivatedByEnter.test.ts
@@ -0,0 +1,69 @@
+import { isKeyboardEventTargetActivatedByEnter } from '@/ui/utilities/hotkey/utils/isKeyboardEventTargetActivatedByEnter';
+
+const pressEnterOn = (target: EventTarget) => {
+ const keyboardEvent = new KeyboardEvent('keydown', { key: 'Enter' });
+
+ target.dispatchEvent(keyboardEvent);
+
+ return keyboardEvent;
+};
+
+const createElementFromHtml = (html: string) => {
+ const container = document.createElement('div');
+
+ container.innerHTML = html;
+
+ return container.firstElementChild as Element;
+};
+
+describe('isKeyboardEventTargetActivatedByEnter', () => {
+ it.each([
+ ['a native button', '<button type="button">Next</button>'],
+ ['an input button', '<input type="button" value="Next" />'],
+ ['an input submit', '<input type="submit" value="Save" />'],
+ ['an input reset', '<input type="reset" value="Reset" />'],
+ ['an input image', '<input type="image" alt="Save" />'],
+ ['a role="button" element', '<div role="button" tabindex="0">Next</div>'],
+ ['a link', '<a href="/companies">Companies</a>'],
+ ['a role="link" element', '<span role="link" tabindex="0">Docs</span>'],
+ ])('is true for Enter pressed on %s', (_, html) => {
+ expect(
+ isKeyboardEventTargetActivatedByEnter(
+ pressEnterOn(createElementFromHtml(html)),
+ ),
+ ).toBe(true);
+ });
+
+ it.each([
+ ['a text input', '<input type="text" />'],
+ ['a checkbox', '<input type="checkbox" />'],
+ ['a textarea', '<textarea></textarea>'],
+ ['a contenteditable element', '<div contenteditable="true"></div>'],
+ ['an anchor without href', '<a>Placeholder</a>'],
+ ['a calendar day cell', '<div role="gridcell" tabindex="0">12</div>'],
+ ])('is false for Enter pressed on %s', (_, html) => {
+ expect(
+ isKeyboardEventTargetActivatedByEnter(
+ pressEnterOn(createElementFromHtml(html)),
+ ),
+ ).toBe(false);
+ });
+
+ it('is false for Enter pressed on a text input inside a role="button" element', () => {
+ const wrapper = createElementFromHtml(
+ '<div role="button"><input type="text" /></div>',
+ );
+
+ expect(
+ isKeyboardEventTargetActivatedByEnter(
+ pressEnterOn(wrapper.querySelector('input') as HTMLInputElement),
+ ),
+ ).toBe(false);
+ });
+
+ it('is false for Enter dispatched on the document', () => {
+ expect(isKeyboardEventTargetActivatedByEnter(pressEnterOn(document))).toBe(
+ false,
+ );
+ });
+});
diff --git a/packages/twenty-front/src/modules/ui/utilities/hotkey/utils/isKeyboardEventTargetActivatedByEnter.ts b/packages/twenty-front/src/modules/ui/utilities/hotkey/utils/isKeyboardEventTargetActivatedByEnter.ts
new file mode 100644
index 00000000000..7165b943cfe
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/utilities/hotkey/utils/isKeyboardEventTargetActivatedByEnter.ts
@@ -0,0 +1,16 @@
+const ENTER_ACTIVATED_TARGET_SELECTOR = [
+ 'button',
+ 'input[type="button"]',
+ 'input[type="submit"]',
+ 'input[type="reset"]',
+ 'input[type="image"]',
+ '[role="button"]',
+ 'a[href]',
+ '[role="link"]',
+].join(', ');
+
+export const isKeyboardEventTargetActivatedByEnter = (
+ keyboardEvent: KeyboardEvent,
+) =>
+ keyboardEvent.target instanceof Element &&
+ keyboardEvent.target.matches(ENTER_ACTIVATED_TARGET_SELECTOR);