Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(withModifiers): explicit modifiers type #10856

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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} />
})

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-dom/__tests__/directives/vOn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
16 changes: 14 additions & 2 deletions packages/runtime-dom/src/directives/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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('.')
Expand Down