Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(lemon-ui): let a field's Enter commit before a form dialog submits (#127108)

Michael Reichenbach283ec0ae6a4f
diff --git a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.test.tsx b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.test.tsx
index f0947e93abcb..1768e5d6ff61 100644
--- a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.test.tsx
+++ b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.test.tsx
@@ -1,6 +1,6 @@
import '@testing-library/jest-dom'
-import { act, cleanup, render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react'
+import { act, cleanup, fireEvent, render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { useValues } from 'kea'
import posthog from 'posthog-js'
@@ -8,6 +8,7 @@ import posthog from 'posthog-js'
import { ApiError } from 'lib/api-error'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { LemonInput } from 'lib/lemon-ui/LemonInput/LemonInput'
+import { LemonInputSelect } from 'lib/lemon-ui/LemonInputSelect/LemonInputSelect'
import { initKeaTests } from '~/test/init'
@@ -95,6 +96,43 @@ describe('LemonFormDialog', () => {
}
})
+ it('lets a field that handles Enter commit its value instead of submitting', async () => {
+ const onSubmit = jest.fn().mockResolvedValue(undefined)
+ render(
+ <LemonFormDialog
+ dialogKey="test-dialog"
+ title="Test dialog"
+ shouldAwaitSubmit
+ initialValues={{ tags: ['growth'] }}
+ content={
+ <LemonField name="tags">
+ <LemonInputSelect mode="multiple" allowCustomValues options={[]} />
+ </LemonField>
+ }
+ onSubmit={onSubmit}
+ />
+ )
+
+ await userEvent.type(screen.getByRole('textbox'), 'billing{Enter}')
+
+ expect(onSubmit).not.toHaveBeenCalled()
+ await submitViaButton()
+ await waitFor(() => expect(onSubmit).toHaveBeenCalledWith({ tags: ['growth', 'billing'] }))
+ })
+
+ it.each([
+ ['an IME composition confirm', 0, { isComposing: true }],
+ ['a Safari IME composition confirm', 0, { keyCode: 229 }],
+ ['a plain key press', 1, {}],
+ ])('Enter from %s in a text field submits %i times', async (_case, submits, eventInit) => {
+ const onSubmit = jest.fn().mockResolvedValue(undefined)
+
+ renderDialog(onSubmit)
+ fireEvent.keyDown(screen.getByRole('textbox'), { key: 'Enter', ...eventInit })
+
+ await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(submits))
+ })
+
it('closes the dialog and does not capture when the submit resolves', async () => {
const onSubmit = jest.fn().mockResolvedValue(undefined)
diff --git a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx
index 6ad031b2fabf..d28bac44a706 100644
--- a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx
+++ b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx
@@ -185,6 +185,14 @@ function isEnterOnField(e: React.KeyboardEvent<HTMLFormElement>): boolean {
return e.key === 'Enter' && !(e.target as HTMLElement).closest('button')
}
+function isImeComposing(e: React.KeyboardEvent<HTMLFormElement>): boolean {
+ return e.nativeEvent.isComposing || e.keyCode === 229
+}
+
+function isEnterHandledByField(e: React.KeyboardEvent<HTMLFormElement>): boolean {
+ return e.defaultPrevented || isImeComposing(e)
+}
+
export const LemonFormDialog = ({
initialValues = {},
onSubmit,
@@ -230,6 +238,9 @@ export const LemonFormDialog = ({
const ref = useRef<LemonDialogRef>(null)
+ const isSubmitKey = (e: React.KeyboardEvent<HTMLFormElement>): boolean =>
+ isEnterOnField(e) && !isEnterHandledByField(e) && primaryButton?.htmlType === 'submit' && isFormValid
+
return (
<Form
logic={lemonDialogLogic}
@@ -238,7 +249,7 @@ export const LemonFormDialog = ({
onKeyDown={
props.shouldAwaitSubmit
? async (e: React.KeyboardEvent<HTMLFormElement>): Promise<void> => {
- if (isEnterOnField(e) && primaryButton?.htmlType === 'submit' && isFormValid) {
+ if (isSubmitKey(e)) {
try {
await onSubmit(form)
} catch (error) {
@@ -252,7 +263,7 @@ export const LemonFormDialog = ({
}
}
: (e: React.KeyboardEvent<HTMLFormElement>): void => {
- if (isEnterOnField(e) && primaryButton?.htmlType === 'submit' && isFormValid) {
+ if (isSubmitKey(e)) {
void onSubmit(form)
ref?.current?.closeDialog()
}