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(scenes): keep tags when escape is pressed in the tag picker (#127148)

Michael Reichenbachbebca86d080b
diff --git a/frontend/src/lib/components/Scenes/TagsCombobox.test.tsx b/frontend/src/lib/components/Scenes/TagsCombobox.test.tsx
new file mode 100644
index 000000000000..00e241dfbad1
--- /dev/null
+++ b/frontend/src/lib/components/Scenes/TagsCombobox.test.tsx
@@ -0,0 +1,55 @@
+import '@testing-library/jest-dom'
+
+import { act, cleanup, render, screen, waitFor } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { KeyboardEvent, useState } from 'react'
+
+import { TagsCombobox } from './TagsCombobox'
+
+function StatefulTags({
+ initialTags,
+ onParentKeyDown,
+}: {
+ initialTags: string[]
+ onParentKeyDown: (event: KeyboardEvent) => void
+}): JSX.Element {
+ const [tags, setTags] = useState(initialTags)
+ return (
+ <div onKeyDown={onParentKeyDown}>
+ <TagsCombobox value={tags} onChange={setTags} placeholder="Add tags..." />
+ </div>
+ )
+}
+
+function chipTitles(): string[] {
+ return Array.from(document.querySelectorAll('[data-slot="combobox-chip"]')).map(
+ (chip) => chip.getAttribute('title') ?? ''
+ )
+}
+
+describe('TagsCombobox', () => {
+ afterEach(() => {
+ cleanup()
+ })
+
+ it.each([
+ ['after adding a tag', 'new-tag{Enter}', ['existing', 'new-tag']],
+ ['after closing the list', '{Escape}', ['existing']],
+ ])(
+ 'keeps the tags and lets the parent close when Escape is pressed %s',
+ async (_, firstKeystrokes, expectedTags) => {
+ const onParentKeyDown = jest.fn()
+ render(<StatefulTags initialTags={['existing']} onParentKeyDown={onParentKeyDown} />)
+ await userEvent.click(screen.getByPlaceholderText('Add tags...'))
+ await userEvent.keyboard(firstKeystrokes)
+ await waitFor(() => expect(screen.queryByRole('listbox')).not.toBeInTheDocument())
+ onParentKeyDown.mockClear()
+
+ await userEvent.keyboard('{Escape}')
+ await act(async () => {})
+
+ expect(chipTitles()).toEqual(expectedTags)
+ expect(onParentKeyDown).toHaveBeenCalledWith(expect.objectContaining({ key: 'Escape' }))
+ }
+ )
+})
diff --git a/frontend/src/lib/components/Scenes/TagsCombobox.tsx b/frontend/src/lib/components/Scenes/TagsCombobox.tsx
index f4534a8cdec8..00d0666a9256 100644
--- a/frontend/src/lib/components/Scenes/TagsCombobox.tsx
+++ b/frontend/src/lib/components/Scenes/TagsCombobox.tsx
@@ -73,7 +73,11 @@ function TagsComboboxInner({
autoHighlight
items={items}
value={value}
- onValueChange={(next: string[]) => {
+ onValueChange={(next: string[], eventDetails) => {
+ if (eventDetails.reason === 'escape-key') {
+ eventDetails.allowPropagation()
+ return
+ }
// Convert any sentinel selection into the real typed value, dedupe.
const cleaned = (next ?? []).map((v) =>
v.startsWith('__create__:') ? v.slice('__create__:'.length) : v