Skip to content

Commit f1697dc

Browse files
zernoniaclaude
andauthored
fix(Tree): leave keyboard events alone when they come from a text field (#2903)
* fix(Tree): leave keyboard events alone when they come from a text field An input rendered inside a tree item — an inline rename, a per-row filter — could not be typed into. Two handlers claimed keys that were never theirs: - `TreeItem` bound `@keydown.left.prevent` / `@keydown.right.prevent` with no `.self`, so arrows from a descendant input were prevented and walked the tree instead of moving the caret. `.self` has to precede `.prevent`: Vue runs modifier guards in source order, so `.prevent.self` would still cancel the event before bailing out. The enter/space binding above already had it. - `TreeRoot.handleKeydown` ran typeahead on every keystroke, so typing a name moved focus to a matching row and unmounted the input mid-edit. `handleKeydownNavigation` had the same gap for Shift+Arrow, which is text selection inside a field. `MenuContentImpl` already guards typeahead this way; Tree now matches, and also covers `contenteditable`. `RovingFocusItem` needed no change — it already returns early when `event.target !== event.currentTarget`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(Tree): include select in the form-field keyboard guard A native select consumes character keys for its own option typeahead and Shift+Arrow to extend a multiple selection, so a select rendered in a tree item was still losing those keys to the tree. Renamed the helper to `isKeyDownInFormField` since `select` is not a text field. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent e5e8dc1 commit f1697dc

3 files changed

Lines changed: 123 additions & 3 deletions

File tree

‎packages/core/src/Tree/Tree.test.ts‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,102 @@ describe('given a Tree with disabled items', () => {
406406
expect(items()[0].attributes('aria-selected')).toBe('false')
407407
})
408408
})
409+
410+
describe('given a Tree with a text field inside an item', () => {
411+
let wrapper: VueWrapper<any>
412+
let input: DOMWrapper<HTMLInputElement>
413+
414+
// An inline rename is the common shape: the row stays a tree item, but while
415+
// it is being edited the keys belong to the input.
416+
const Fixture = defineComponent({
417+
setup() {
418+
const items = [
419+
{ title: 'components', children: [{ title: 'Card.vue' }, { title: 'Button.vue' }] },
420+
{ title: 'app.vue' },
421+
]
422+
423+
return () => h(
424+
TreeRoot,
425+
{ items, getKey: (item: any) => item.title, defaultExpanded: ['components'] },
426+
{
427+
default: ({ flattenItems }: any) => flattenItems.map((item: any) =>
428+
h(
429+
TreeItem,
430+
{ key: item._id, ...item.bind },
431+
() => {
432+
if (item.value.title === 'app.vue')
433+
return h('input', { 'data-testid': 'rename' })
434+
if (item.value.title === 'Card.vue')
435+
return h('select', { 'data-testid': 'picker' }, [h('option', 'components'), h('option', 'other')])
436+
return h('span', item.value.title)
437+
},
438+
),
439+
),
440+
},
441+
)
442+
},
443+
})
444+
445+
function pressIn(target: HTMLElement, key: string) {
446+
const event = new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true })
447+
target.dispatchEvent(event)
448+
return event
449+
}
450+
451+
beforeEach(async () => {
452+
document.body.innerHTML = ''
453+
wrapper = mount(Fixture, { attachTo: document.body })
454+
input = wrapper.find('[data-testid=rename]') as DOMWrapper<HTMLInputElement>
455+
input.element.focus()
456+
await nextTick()
457+
})
458+
459+
it('leaves ArrowRight to the caret instead of walking the tree', async () => {
460+
const rowsBefore = wrapper.findAll('[role=treeitem]').length
461+
const event = pressIn(input.element, kbd.ARROW_RIGHT)
462+
await nextTick()
463+
464+
expect(event.defaultPrevented).toBe(false)
465+
expect(document.activeElement).toBe(input.element)
466+
expect(wrapper.findAll('[role=treeitem]')).toHaveLength(rowsBefore)
467+
})
468+
469+
it('leaves ArrowLeft to the caret instead of jumping to the parent row', async () => {
470+
const rowsBefore = wrapper.findAll('[role=treeitem]').length
471+
const event = pressIn(input.element, kbd.ARROW_LEFT)
472+
await nextTick()
473+
474+
expect(event.defaultPrevented).toBe(false)
475+
expect(document.activeElement).toBe(input.element)
476+
expect(wrapper.findAll('[role=treeitem]')).toHaveLength(rowsBefore)
477+
})
478+
479+
it('does not run typeahead while typing', async () => {
480+
// 'c' matches both "components" and "Card.vue"; typeahead would focus one.
481+
pressIn(input.element, 'c')
482+
await nextTick()
483+
484+
expect(document.activeElement).toBe(input.element)
485+
})
486+
487+
it('does not run typeahead while a select inside an item is focused', async () => {
488+
// A native select owns character keys for its own option typeahead.
489+
const picker = wrapper.find('[data-testid=picker]').element as HTMLSelectElement
490+
picker.focus()
491+
await nextTick()
492+
493+
pressIn(picker, 'c')
494+
await nextTick()
495+
496+
expect(document.activeElement).toBe(picker)
497+
})
498+
499+
it('still walks the tree when the key lands on the item itself', async () => {
500+
const items = wrapper.findAll('[role=treeitem]')
501+
const folder = items[0]
502+
await folder.trigger('keydown', { key: kbd.ARROW_LEFT })
503+
504+
// "components" was expanded, so ArrowLeft on the row collapses it.
505+
expect(wrapper.findAll('[role=treeitem]').length).toBeLessThan(items.length)
506+
})
507+
})

‎packages/core/src/Tree/TreeItem.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ defineExpose({
195195
:data-expanded="isExpanded ? '' : undefined"
196196
:data-disabled="isDisabled ? '' : undefined"
197197
@keydown.enter.space.self.prevent="handleSelectCustomEvent"
198-
@keydown.right.prevent="(ev: KeyboardEvent) => rootContext.dir.value === 'ltr' ? handleKeydownRight(ev) : handleKeydownLeft(ev)"
199-
@keydown.left.prevent="(ev: KeyboardEvent) => rootContext.dir.value === 'ltr' ? handleKeydownLeft(ev) : handleKeydownRight(ev)"
198+
@keydown.right.self.prevent="(ev: KeyboardEvent) => rootContext.dir.value === 'ltr' ? handleKeydownRight(ev) : handleKeydownLeft(ev)"
199+
@keydown.left.self.prevent="(ev: KeyboardEvent) => rootContext.dir.value === 'ltr' ? handleKeydownLeft(ev) : handleKeydownRight(ev)"
200200
@click.stop="(ev: PointerEvent) => {
201201
handleSelectCustomEvent(ev)
202202
handleToggleCustomEvent(ev)

‎packages/core/src/Tree/TreeRoot.vue‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,28 @@ const expandedItems = computed(() => {
167167
return flattenItems(items ?? [])
168168
})
169169
170+
/**
171+
* A form control rendered inside a tree item — an inline rename, a per-row
172+
* filter or picker — owns its own keys. Without this, typing into it drives
173+
* tree typeahead and selection, moving focus to another row mid-edit. `select`
174+
* counts too: it consumes character keys for its own option typeahead and
175+
* Shift+Arrow to extend a multiple selection.
176+
*
177+
* Same idea as the guard in `MenuContentImpl`, widened past `input`/`textarea`.
178+
*/
179+
const KEY_OWNING_TAGS = ['input', 'textarea', 'select']
180+
181+
function isKeyDownInFormField(event: KeyboardEvent) {
182+
const target = event.target as HTMLElement | null
183+
if (!target)
184+
return false
185+
return KEY_OWNING_TAGS.includes(target.tagName.toLowerCase()) || target.isContentEditable
186+
}
187+
170188
function handleKeydown(event: KeyboardEvent) {
189+
if (isKeyDownInFormField(event))
190+
return
191+
171192
if (isVirtual.value) {
172193
virtualKeydownHook.trigger(event)
173194
}
@@ -178,7 +199,7 @@ function handleKeydown(event: KeyboardEvent) {
178199
}
179200
180201
function handleKeydownNavigation(event: KeyboardEvent) {
181-
if (isVirtual.value)
202+
if (isVirtual.value || isKeyDownInFormField(event))
182203
return
183204
184205
const intent = MAP_KEY_TO_FOCUS_INTENT[event.key]

0 commit comments

Comments
 (0)