Skip to content

Commit

Permalink
Expose default event name logic as a method on Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
grncdr committed Feb 18, 2023
1 parent e26e13d commit 2ddac5f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/core/action.ts
@@ -1,6 +1,6 @@
import { ActionDescriptor, parseActionDescriptorString, stringifyEventTarget } from "./action_descriptor"
import { Token } from "../mutation-observers"
import { getDefaultEventNameForElement, Schema } from "./schema"
import { 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, schema) || error("missing event name")
this.eventName = descriptor.eventName || schema.defaultEventNameForElement(element) || 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
30 changes: 15 additions & 15 deletions src/core/schema.ts
Expand Up @@ -4,8 +4,8 @@ export interface Schema {
targetAttribute: string
targetAttributeForScope(identifier: string): string
outletAttributeForScope(identifier: string, outlet: string): string
defaultEventNameForElement(element: Element): string | undefined
keyMappings: { [key: string]: string }
defaultEventNames: { [tagName: string]: (element: Element) => string }
}

export const defaultSchema: Schema = {
Expand All @@ -14,6 +14,12 @@ export const defaultSchema: Schema = {
targetAttribute: "data-target",
targetAttributeForScope: (identifier) => `data-${identifier}-target`,
outletAttributeForScope: (identifier, outlet) => `data-${identifier}-${outlet}-outlet`,
defaultEventNameForElement: (element: Element) => {
const tagName = element.tagName.toLowerCase()
if (tagName in defaultEventNames) {
return defaultEventNames[tagName](element)
}
},
keyMappings: {
enter: "Enter",
tab: "Tab",
Expand All @@ -29,23 +35,17 @@ export const defaultSchema: Schema = {
...objectFromEntries("abcdefghijklmnopqrstuvwxyz".split("").map((c) => [c, c])),
// [0-9]
...objectFromEntries("0123456789".split("").map((n) => [n, n])),
},
defaultEventNames: {
a: () => "click",
button: () => "click",
form: () => "submit",
details: () => "toggle",
input: (element) => (element.getAttribute("type") == "submit" ? "click" : "input"),
select: () => "change",
textarea: () => "input",
}
}

export function getDefaultEventNameForElement(element: Element, schema = defaultSchema): string | undefined {
const tagName = element.tagName.toLowerCase()
if (tagName in schema.defaultEventNames) {
return schema.defaultEventNames[tagName](element)
}
const defaultEventNames: { [tag: string]: (element: Element) => string } = {
a: () => "click",
button: () => "click",
form: () => "submit",
details: () => "toggle",
input: (element) => (element.getAttribute("type") == "submit" ? "click" : "input"),
select: () => "change",
textarea: () => "input",
}

function objectFromEntries(array: [string, any][]): object {
Expand Down
7 changes: 6 additions & 1 deletion src/tests/modules/core/action_custom_default_event_tests.ts
Expand Up @@ -6,7 +6,12 @@ import { Application } from "../../../core/application"
export default class ActionKeyboardFilterTests extends LogControllerTestCase {
schema: Schema = {
...defaultSchema,
defaultEventNames: { ...defaultSchema.defaultEventNames, "some-element": () => "click" },
defaultEventNameForElement(element) {
if (element.tagName === 'SOME-ELEMENT') {
return 'click'
}
return defaultSchema.defaultEventNameForElement(element)
}
}
application: Application = new TestApplication(this.fixtureElement, this.schema)

Expand Down

0 comments on commit 2ddac5f

Please sign in to comment.