Skip to content

Commit 62cd51e

Browse files
authored
fix(hub-ui): dont throw on browser autocomplete (#471)
1 parent 0e4a49c commit 62cd51e

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎packages/hub-ui/src/client/state/keybindings.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { DevframeCommandEntry, DevframeCommandKeybinding } from '@devframes/hub'
22
import type { WhenContext } from 'devframe/utils/when'
33
import { describe, expect, it } from 'vitest'
4-
import { collectAllKeybindings, filterCommandsByWhen, findCommandDeep, findKeybindingConflict, getShortcutRows, walkCommands } from './keybindings'
4+
import { collectAllKeybindings, filterCommandsByWhen, findCommandDeep, findKeybindingConflict, getShortcutRows, normalizeKeyEvent, walkCommands } from './keybindings'
55

66
describe('findKeybindingConflict', () => {
77
const commands: DevframeCommandEntry[] = [
@@ -259,3 +259,17 @@ describe('filterCommandsByWhen', () => {
259259
expect(filterCommandsByWhen(withWhen, whenContext)).toEqual([])
260260
})
261261
})
262+
263+
describe('normalizeKeyEvent', () => {
264+
// Chrome's native autofill dispatches `keydown` events without `key`
265+
it('ignores key events without a key, like Chrome autofill', () => {
266+
const event = { ctrlKey: false, metaKey: false, altKey: false, shiftKey: false } as KeyboardEvent
267+
expect(() => normalizeKeyEvent(event)).not.toThrow()
268+
expect(normalizeKeyEvent(event)).toBe('')
269+
})
270+
271+
it('normalizes a regular shortcut', () => {
272+
const event = { key: 'k', ctrlKey: false, metaKey: false, altKey: false, shiftKey: true } as KeyboardEvent
273+
expect(normalizeKeyEvent(event)).toBe('Shift+K')
274+
})
275+
})

‎packages/hub-ui/src/client/state/keybindings.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function normalizeKeyEvent(e: KeyboardEvent): string {
3030
parts.push('Shift')
3131

3232
// Normalize key name
33-
let key = e.key
33+
let key = e.key ?? ''
3434
if (key.length === 1)
3535
key = key.toUpperCase()
3636

0 commit comments

Comments
 (0)