-
Notifications
You must be signed in to change notification settings - Fork 425
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
Allow action params for global events #495
Conversation
Thank you for opening this PR! I tried this locally, and I was able to pass the tests you've written with a smaller set of changes: diff --git a/src/core/action.ts b/src/core/action.ts
index 5c69dc9..6a69d1f 100644
--- a/src/core/action.ts
+++ b/src/core/action.ts
@@ -34,7 +34,7 @@ export class Action {
if (this.eventTarget instanceof Element) {
return this.getParamsFromEventTargetAttributes(this.eventTarget)
} else {
- return {}
+ return this.getParamsFromEventTargetAttributes(this.element)
}
}
diff --git a/src/tests/modules/core/action_params_tests.ts b/src/tests/modules/core/action_params_tests.ts
index afdbab4..982c03c 100644
--- a/src/tests/modules/core/action_params_tests.ts
+++ b/src/tests/modules/core/action_params_tests.ts
@@ -16,6 +16,7 @@ export default class ActionParamsTests extends LogControllerTestCase {
<div id="nested"></div>
</button>
</div>
+ <div id="outside"></div>
`
expectedParamsForC = {
id: 123,
@@ -38,6 +39,16 @@ export default class ActionParamsTests extends LogControllerTestCase {
)
}
+ async "test global event return element params where the action is defined"() {
+ this.actionValue = "keydown@window->c#log"
+ await this.nextFrame
+ await this.triggerEvent("#outside", "keydown")
+
+ this.assertActions(
+ { identifier: "c", params: this.expectedParamsForC },
+ )
+ }
+
async "test passing params to namespaced controller"() {
this.actionValue = "click->c#log click->d#log2"
await this.nextFrame Is there something I'm misunderstanding that makes the rest of the implementation change necessary? |
I was trying to cleanup after making the code work. The smallest change I initially made was: get params(): object {
return this.getParamsFromEventTargetAttributes(this.element)
} But then For the more subjective changes, they are not needed at all. That would be moving from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @rik, it corrects a significant bug for action params and global events. I think I was confused by this.element
in the action context vs this.element
in the typical Stimulus context.
This makes the solution even simpler
@dhh Is there a nightly build somewhere to start using this fix? Thanks! |
Just came across this issue, too - a stable release with this fix would be great :) |
Ping @adrienpoly since you implemented the original version and you might have some insight why you used the event target rather than the element.