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

Allow for default method for controller actions #756

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 src/core/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Action {
this.eventName = descriptor.eventName || getDefaultEventNameForElement(element) || error("missing event name")
this.eventOptions = descriptor.eventOptions || {}
this.identifier = descriptor.identifier || error("missing identifier")
this.methodName = descriptor.methodName || error("missing method name")
this.methodName = descriptor.methodName || "_"
this.keyFilter = descriptor.keyFilter || ""
this.schema = schema
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/action_descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface ActionDescriptor {
}

// capture nos.: 1 1 2 2 3 3 4 4 5 5 6 6 7 7
const descriptorPattern = /^(?:(?:([^.]+?)\+)?(.+?)(?:\.(.+?))?(?:@(window|document))?->)?(.+?)(?:#([^:]+?))(?::(.+))?$/
const descriptorPattern = /^(?:(?:([^.]+?)\+)?(.+?)(?:\.(.+?))?(?:@(window|document))?->)?(.+?)?(?:#([^:]+?))?(?::(.+))?$/

export function parseActionDescriptorString(descriptorString: string): Partial<ActionDescriptor> {
const source = descriptorString.trim()
Expand Down
4 changes: 4 additions & 0 deletions src/tests/controllers/log_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class LogController extends Controller {
this.disconnectCount++
}

_(event: ActionEvent) {
this.recordAction("default", event)
}

log(event: ActionEvent) {
this.recordAction("log", event)
}
Expand Down
6 changes: 6 additions & 0 deletions src/tests/modules/core/action_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default class ActionTests extends LogControllerTestCase {
fixtureHTML = `
<div data-controller="c" data-action="keydown@window->c#log">
<button data-action="c#log"><span>Log</span></button>
<button id="default" data-action="c"><span>Default</span></button>
<div id="outer" data-action="click->c#log">
<div id="inner" data-controller="c" data-action="click->c#log keyup@window->c#log"></div>
</div>
Expand All @@ -21,6 +22,11 @@ export default class ActionTests extends LogControllerTestCase {
this.assertActions({ name: "log", eventType: "click" })
}

async "test default method"() {
await this.triggerEvent("#default", "click")
this.assertActions({ name: "default", eventType: "click" })
}

async "test bubbling events"() {
await this.triggerEvent("span", "click")
this.assertActions({ eventType: "click", currentTarget: this.findElement("button") })
Expand Down