Skip to content

Commit

Permalink
Adds new Action Options, namely :self and :!self (#546)
Browse files Browse the repository at this point in the history
Co-authored-by: David Heinemeier Hansson <david@basecamp.com>
  • Loading branch information
radiantshaw and dhh committed Jul 17, 2022
1 parent 5a60cde commit 910f2fd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/reference/actions.md
Expand Up @@ -107,6 +107,7 @@ Custom action option | Description
-------------------- | -----------
`:stop` | calls `.stopPropagation()` on the event before invoking the method
`:prevent` | calls `.preventDefault()` on the event before invoking the method
`:self` | only invokes the method if the event was fired by the element itself

## Event Objects

Expand Down
10 changes: 9 additions & 1 deletion src/core/binding.ts
Expand Up @@ -31,7 +31,7 @@ export class Binding {
}

handleEvent(event: Event) {
if (this.willBeInvokedByEvent(event)) {
if (this.willBeInvokedByEvent(event) && this.shouldBeInvokedPerSelf(event)) {
this.processStopPropagation(event);
this.processPreventDefault(event);

Expand Down Expand Up @@ -77,6 +77,14 @@ export class Binding {
}
}

private shouldBeInvokedPerSelf(event: Event): boolean {
if (this.action.eventOptions.self === true) {
return this.action.element === event.target
} else {
return true
}
}

private willBeInvokedByEvent(event: Event): boolean {
const eventTarget = event.target
if (this.element === eventTarget) {
Expand Down
1 change: 1 addition & 0 deletions src/core/event_modifiers.ts
@@ -1,4 +1,5 @@
export interface EventModifiers extends AddEventListenerOptions {
stop?: boolean;
prevent?: boolean;
self?: boolean;
}
20 changes: 20 additions & 0 deletions src/tests/modules/core/event_options_tests.ts
Expand Up @@ -190,6 +190,26 @@ export default class EventOptionsTests extends LogControllerTestCase {
)
}

async "test self option"() {
this.setAction(this.buttonElement, "click->c#log:self")
await this.nextFrame

await this.triggerEvent(this.buttonElement, "click")

this.assertActions(
{ name: "log", eventType: "click" }
)
}

async "test self option on parent"() {
this.setAction(this.element, "click->c#log:self")
await this.nextFrame

await this.triggerEvent(this.buttonElement, "click")

this.assertNoActions()
}

setAction(element: Element, value: string) {
element.setAttribute("data-action", value)
}
Expand Down

0 comments on commit 910f2fd

Please sign in to comment.