Skip to content

Commit

Permalink
Allow plain strings in Schema.defaultEventNames
Browse files Browse the repository at this point in the history
  • Loading branch information
grncdr committed Jun 22, 2023
1 parent e26e13d commit 0af60e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
13 changes: 12 additions & 1 deletion 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 Down Expand Up @@ -86,6 +86,17 @@ export class Action {
}
}

function getDefaultEventNameForElement(element: Element, schema: Schema): string | undefined {
let eventName = schema.defaultEventNames[element.localName]
if (typeof eventName === 'function') {
return eventName(element)
}
if (typeof eventName === 'string') {
return eventName
}
}


function error(message: string): never {
throw new Error(message)
}
Expand Down
21 changes: 7 additions & 14 deletions src/core/schema.ts
Expand Up @@ -5,7 +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 }
defaultEventNames: { [tagName: string]: string | ((element: Element) => string) }
}

export const defaultSchema: Schema = {
Expand All @@ -31,20 +31,13 @@ export const defaultSchema: Schema = {
...objectFromEntries("0123456789".split("").map((n) => [n, n])),
},
defaultEventNames: {
a: () => "click",
button: () => "click",
form: () => "submit",
details: () => "toggle",
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)
select: "change",
textarea: "input",
}
}

Expand Down

0 comments on commit 0af60e5

Please sign in to comment.