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

Move defaultEventNames into Schema #662

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 2 additions & 19 deletions src/core/action.ts
@@ -1,6 +1,6 @@
import { ActionDescriptor, parseActionDescriptorString, stringifyEventTarget } from "./action_descriptor"
import { Token } from "../mutation-observers"
import { Schema } from "./schema"
import { getDefaultEventNameForElement, Schema } from "./schema"
import { camelize } from "./string_helpers"
import { hasProperty } from "./utils"

Expand All @@ -23,7 +23,7 @@ export class Action {
this.element = element
this.index = index
this.eventTarget = descriptor.eventTarget || element
this.eventName = descriptor.eventName || getDefaultEventNameForElement(element) || error("missing event name")
this.eventName = descriptor.eventName || getDefaultEventNameForElement(element, schema) || error("missing event name")
this.eventOptions = descriptor.eventOptions || {}
this.identifier = descriptor.identifier || error("missing identifier")
this.methodName = descriptor.methodName || error("missing method name")
Expand Down Expand Up @@ -86,23 +86,6 @@ export class Action {
}
}

const defaultEventNames: { [tagName: string]: (element: Element) => string } = {
a: () => "click",
button: () => "click",
form: () => "submit",
details: () => "toggle",
input: (e) => (e.getAttribute("type") == "submit" ? "click" : "input"),
select: () => "change",
textarea: () => "input",
}

export function getDefaultEventNameForElement(element: Element): string | undefined {
const tagName = element.tagName.toLowerCase()
if (tagName in defaultEventNames) {
return defaultEventNames[tagName](element)
}
}

function error(message: string): never {
throw new Error(message)
}
Expand Down
17 changes: 17 additions & 0 deletions src/core/schema.ts
Expand Up @@ -5,6 +5,7 @@ export interface Schema {
targetAttributeForScope(identifier: string): string
outletAttributeForScope(identifier: string, outlet: string): string
keyMappings: { [key: string]: string }
defaultEventNames: { [tagName: string]: (element: Element) => string }
}

export const defaultSchema: Schema = {
Expand All @@ -29,6 +30,22 @@ export const defaultSchema: Schema = {
// [0-9]
...objectFromEntries("0123456789".split("").map((n) => [n, n])),
},
defaultEventNames: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defaultEventNames: {
defaultEventMappings: {

Since we use the term mappings already, might be nice to align with that naming?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsoleted by 2ddac5f

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up dropping 2ddac5f so this is relevant again. I'm open to renaming it.

a: () => "click",
button: () => "click",
form: () => "submit",
details: () => "toggle",
input: (e) => (e.getAttribute("type") == "submit" ? "click" : "input"),
grncdr marked this conversation as resolved.
Show resolved Hide resolved
select: () => "change",
textarea: () => "input",
}
}

export function getDefaultEventNameForElement(element: Element, schema = defaultSchema): string | undefined {
grncdr marked this conversation as resolved.
Show resolved Hide resolved
const tagName = element.tagName.toLowerCase()
if (tagName in schema.defaultEventNames) {
return schema.defaultEventNames[tagName](element)
}
}

function objectFromEntries(array: [string, any][]): object {
Expand Down
20 changes: 20 additions & 0 deletions src/tests/modules/core/action_custom_default_event_tests.ts
@@ -0,0 +1,20 @@
import { TestApplication } from "../../cases/application_test_case"
import { LogControllerTestCase } from "../../cases/log_controller_test_case"
import { Schema, defaultSchema } from "../../../core/schema"
import { Application } from "../../../core/application"

export default class ActionKeyboardFilterTests extends LogControllerTestCase {
schema: Schema = {
...defaultSchema,
defaultEventNames: { ...defaultSchema.defaultEventNames, "some-element": () => "click" },
}
application: Application = new TestApplication(this.fixtureElement, this.schema)

identifier = "c"
fixtureHTML = `<some-element data-controller="c" data-action="c#log"></some-element>`

async "test default event"() {
await this.triggerEvent("some-element", "click")
this.assertActions({ name: "log", eventType: "click" })
}
}