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

fix: handle additional args in components' with event modifiers #11746

Closed
wants to merge 1 commit into from
Closed

fix: handle additional args in components' with event modifiers #11746

wants to merge 1 commit into from

Conversation

mshahbazi
Copy link

Additional args are not passed to components events with modifiers.

with a component template like this
<input @keyup="$emit('my-keyup', $event, var1, var2)"/>
triggering the event, e.g.
<my-component @my-keyup.enter="onMyKeyup"/>
the onMyKeyup receives the $event, but not var1 and var2

hope this PR resolves the issue.

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

Additional args are not passed to the component's events with modifiers.
with a component template like this
`<input @keyup="$emit('my-keyup', $event, var1, var2)"/>`
triggering the event, e.g.
`<my-component @my-keyup.enter="onMyKeyup"/>`
the `onMyKeyup` receives the `$event`, but not `var1` and `var2`

hope this PR resolves the issue.
@xunita
Copy link

xunita commented Oct 31, 2020

No need of additional args, you can achieve it using an object instead.
<input @keyup="$emit('my-keyup', object)"/>
Where your object looks like :
{ event: $event, var1: var1, var2: var2, }

@mshahbazi
Copy link
Author

@sopegue,
Thank you for your comment,
Digging into the code revealed that .enter modifier is checked by something like if (aruments[0].keyCode == 13) so { event: $event, var1: var1, var2: var2, } will not work.
Simply put, to use event modifier in a component, e.g., @my-keyup.enter, $event need to be passed as the first argument, and surprisingly, the rest is ignored.

@xunita
Copy link

xunita commented Oct 31, 2020

I tried your previous attempt and my solution, both work properly locally and in production.
The event sill fire even if not enter key pressed.
However, tried it on codesandbox but did not work, wondering why...

@mshahbazi
Copy link
Author

The codesandbox with your solution raises Cannot read property 'indexOf' of undefined" which is probably rooted in getKeyFilter where checks the existence of key in $event, i.e., if(!$event.type.indexOf('key') ... return null. In your code, $event is set as { event: $event, var1, var2 } that does not include the key.
Although it does not resolve the issue, to get rid of the error, you may add key, keyCode, target to the object. Please see codesandbox

@posva
Copy link
Member

posva commented Nov 1, 2020

Duplicate of #10958

@posva posva marked this as a duplicate of #10958 Nov 1, 2020
@posva posva closed this Nov 1, 2020
@mshahbazi mshahbazi deleted the patch-1 branch November 1, 2020 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants