refactor(types): use explicit modifiers type (#10856)

This commit is contained in:
btea 2024-05-07 06:23:04 +08:00 committed by GitHub
parent 8373350ce5
commit 481b1b6f38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 4 deletions

View File

@ -1501,7 +1501,7 @@ describe('should work when props type is incompatible with setup returned type '
describe('withKeys and withModifiers as pro', () => {
const onKeydown = withKeys(e => {}, [''])
const onClick = withModifiers(e => {}, [''])
const onClick = withModifiers(e => {}, [])
;<input onKeydown={onKeydown} onClick={onClick} />
})

View File

@ -43,7 +43,7 @@ describe('runtime-dom: v-on directive', () => {
})
test('it should support key modifiers and system modifiers', () => {
const keyNames = ['ctrl', 'shift', 'meta', 'alt']
const keyNames = ['ctrl', 'shift', 'meta', 'alt'] as const
keyNames.forEach(keyName => {
const el = document.createElement('div')

View File

@ -10,9 +10,21 @@ import { hyphenate, isArray } from '@vue/shared'
const systemModifiers = ['ctrl', 'shift', 'alt', 'meta']
type KeyedEvent = KeyboardEvent | MouseEvent | TouchEvent
type ModifierGuardsKeys =
| 'stop'
| 'prevent'
| 'self'
| 'ctrl'
| 'shift'
| 'alt'
| 'meta'
| 'left'
| 'middle'
| 'right'
| 'exact'
const modifierGuards: Record<
string,
ModifierGuardsKeys,
(e: Event, modifiers: string[]) => void | boolean
> = {
stop: e => e.stopPropagation(),
@ -36,7 +48,7 @@ export const withModifiers = <
T extends (event: Event, ...args: unknown[]) => any,
>(
fn: T & { _withMods?: { [key: string]: T } },
modifiers: string[],
modifiers: ModifierGuardsKeys[],
) => {
const cache = fn._withMods || (fn._withMods = {})
const cacheKey = modifiers.join('.')