: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(lemon-dialog): isolate nested form dialogs with unique kea logic keys (#62113)
okxintc097d9d0c9f2
diff --git a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsxindex 357a4e451b26..89ac7989ebc0 100644--- a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx+++ b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx@@ -6,6 +6,7 @@ import { Root, createRoot } from 'react-dom/client'import { LemonButton, LemonButtonProps } from 'lib/lemon-ui/LemonButton'import { LemonModal, LemonModalProps } from 'lib/lemon-ui/LemonModal'+import { uuid } from 'lib/utils/dom'import { LemonDialogFormPropsType, lemonDialogLogic } from './lemonDialogLogic'@@ -167,9 +168,11 @@ export const LemonFormDialog = ({errors,content,primaryButtonProps,+ dialogKey,...props}: LemonFormDialogProps): JSX.Element => {- const logic = lemonDialogLogic({ errors })+ const logicProps = { errors, dialogKey }+ const logic = lemonDialogLogic(logicProps)const { form, isFormValid, formValidationErrors } = useValues(logic)const { setFormValues } = useActions(logic)const [isLoading, setIsLoading] = useState(false)@@ -206,6 +209,7 @@ export const LemonFormDialog = ({return (<Formlogic={lemonDialogLogic}+ props={logicProps}formKey="form"onKeyDown={props.shouldAwaitSubmit@@ -261,5 +265,7 @@ LemonDialog.open = (props: LemonDialogProps) => {LemonDialog.openForm = (props: LemonFormDialogProps) => {const { root, onDestroy } = createAndInsertRoot()- root.render(<LemonFormDialog {...props} onAfterClose={onDestroy} />)+ // Each dialog gets a unique key so nested dialogs don't share the same+ // lemonDialogLogic instance and corrupt each other's form state.+ root.render(<LemonFormDialog {...props} dialogKey={uuid()} onAfterClose={onDestroy} />)}diff --git a/frontend/src/lib/lemon-ui/LemonDialog/lemonDialogLogic.test.ts b/frontend/src/lib/lemon-ui/LemonDialog/lemonDialogLogic.test.tsnew file mode 100644index 000000000000..1f25a3ba3026--- /dev/null+++ b/frontend/src/lib/lemon-ui/LemonDialog/lemonDialogLogic.test.ts@@ -0,0 +1,47 @@+import { initKeaTests } from '~/test/init'++import { lemonDialogLogic } from './lemonDialogLogic'++describe('lemonDialogLogic', () => {+ beforeEach(() => {+ initKeaTests()+ })++ it('isolates form state and validation between dialogs with different keys', () => {+ const saveView = lemonDialogLogic({+ dialogKey: 'save-view',+ errors: { viewName: (name: string) => (/\s/.test(name ?? '') ? 'Spaces are not allowed' : undefined) },+ })+ saveView.mount()+ saveView.actions.setFormValues({ viewName: 'invalid name' })+ expect(saveView.values.isFormValid).toBe(false)++ const newFolder = lemonDialogLogic({+ dialogKey: 'new-folder',+ errors: { folderName: (name: string) => (!name?.trim() ? 'You must enter a folder name' : undefined) },+ })+ newFolder.mount()+ newFolder.actions.setFormValues({ folderName: 'docs' })+ expect(newFolder.values.isFormValid).toBe(true)++ // The nested dialog's valid form must not leak into the parent's validation.+ expect(saveView.values.isFormValid).toBe(false)+ expect(saveView.values.form).toEqual({ viewName: 'invalid name' })++ newFolder.unmount()+ saveView.unmount()+ })++ it('shares state when no key is provided, falling back to a single default instance', () => {+ const first = lemonDialogLogic({ errors: {} })+ first.mount()+ const second = lemonDialogLogic({ errors: {} })+ second.mount()++ first.actions.setFormValues({ name: 'shared' })+ expect(second.values.form).toEqual({ name: 'shared' })++ second.unmount()+ first.unmount()+ })+})diff --git a/frontend/src/lib/lemon-ui/LemonDialog/lemonDialogLogic.ts b/frontend/src/lib/lemon-ui/LemonDialog/lemonDialogLogic.tsindex 664fcf14c2d6..77ec4655b56f 100644--- a/frontend/src/lib/lemon-ui/LemonDialog/lemonDialogLogic.ts+++ b/frontend/src/lib/lemon-ui/LemonDialog/lemonDialogLogic.ts@@ -1,15 +1,18 @@-import { kea, path, props } from 'kea'+import { kea, key, path, props } from 'kea'import { forms } from 'kea-forms'import type { lemonDialogLogicType } from './lemonDialogLogicType'export type LemonDialogFormPropsType = {errors?: Record<string, (value: string) => string | undefined>+ /** Unique key that isolates this dialog's form state from other open dialogs. */+ dialogKey?: string}export const lemonDialogLogic = kea<lemonDialogLogicType>([- path(['components', 'lemon-dialog', 'lemonDialogLogic']),props({} as LemonDialogFormPropsType),+ key(({ dialogKey }) => dialogKey ?? 'default'),+ path((k) => ['components', 'lemon-dialog', 'lemonDialogLogic', k]),forms(({ props }) => ({form: {defaults: {} as Record<string, string>,