From 0af60e5e4cb181d889ae46cd35e8e17fb8d312bb Mon Sep 17 00:00:00 2001 From: Stephen Sugden Date: Fri, 23 Jun 2023 00:53:59 +0200 Subject: [PATCH] Allow plain strings in Schema.defaultEventNames --- src/core/action.ts | 13 ++++++++++++- src/core/schema.ts | 21 +++++++-------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/core/action.ts b/src/core/action.ts index 4774a782..d8414123 100644 --- a/src/core/action.ts +++ b/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" @@ -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) } diff --git a/src/core/schema.ts b/src/core/schema.ts index 09778552..4a93daa6 100644 --- a/src/core/schema.ts +++ b/src/core/schema.ts @@ -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 = { @@ -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", } }