diff --git a/framework/h/lifecycle.go b/framework/h/lifecycle.go index 94a72567..dfbb086e 100644 --- a/framework/h/lifecycle.go +++ b/framework/h/lifecycle.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/maddalax/htmgo/framework/hx" "github.com/maddalax/htmgo/framework/internal/util" + "strings" ) type LifeCycle struct { @@ -37,6 +38,11 @@ func validateCommands(cmds []Command) { func (l *LifeCycle) OnEvent(event hx.Event, cmd ...Command) *LifeCycle { validateCommands(cmd) + if strings.HasPrefix(event, "htmx:") { + event = event[5:] + event = util.ConvertCamelToDash(fmt.Sprintf("hx-on::%s", event)) + } + if l.handlers[event] == nil { l.handlers[event] = []Command{} } diff --git a/framework/h/renderer.go b/framework/h/renderer.go index 6ae9769c..926e66c1 100644 --- a/framework/h/renderer.go +++ b/framework/h/renderer.go @@ -142,12 +142,10 @@ func (l *LifeCycle) Render(context *RenderContext) { for _, command := range commands { switch c := command.(type) { case SimpleJsCommand: - eventName := hx.FormatEventName(event, true) - m[eventName] += fmt.Sprintf("%s;", c.Command) + m[event] += fmt.Sprintf("%s;", c.Command) case ComplexJsCommand: - eventName := hx.FormatEventName(event, true) context.AddScript(c.TempFuncName, c.Command) - m[eventName] += fmt.Sprintf("%s();", c.TempFuncName) + m[event] += fmt.Sprintf("%s();", c.TempFuncName) case *AttributeMap: for k, v := range c.ToMap() { l.fromAttributeMap(event, k, v, context) diff --git a/framework/hx/trigger.go b/framework/hx/trigger.go index 89fd46ac..2875bf3b 100644 --- a/framework/hx/trigger.go +++ b/framework/hx/trigger.go @@ -23,14 +23,6 @@ func ToHtmxTriggerName(event string) string { return event } -func FormatEventName(event string, isDomEvent bool) string { - raw := ToHtmxTriggerName(event) - if isDomEvent { - return "on" + raw - } - return event -} - func NewTrigger(opts ...TriggerEvent) *Trigger { t := Trigger{ events: make([]TriggerEvent, 0), diff --git a/framework/internal/util/casing.go b/framework/internal/util/casing.go new file mode 100644 index 00000000..1a7b54cc --- /dev/null +++ b/framework/internal/util/casing.go @@ -0,0 +1,16 @@ +package util + +import ( + "regexp" + "strings" +) + +var re = regexp.MustCompile("([a-z])([A-Z])") + +// ConvertCamelToDash converts a camelCase string to dash-case +func ConvertCamelToDash(s string) string { + // Find uppercase letters and prepend a dash + s = re.ReplaceAllString(s, "$1-$2") + // Convert the string to lower case + return strings.ToLower(s) +}