Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maddalax committed Sep 22, 2024
1 parent e618ad9 commit 9e44fb5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
6 changes: 6 additions & 0 deletions framework/h/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/maddalax/htmgo/framework/hx"
"github.com/maddalax/htmgo/framework/internal/util"
"strings"
)

type LifeCycle struct {
Expand Down Expand Up @@ -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{}
}
Expand Down
6 changes: 2 additions & 4 deletions framework/h/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions framework/hx/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
16 changes: 16 additions & 0 deletions framework/internal/util/casing.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9e44fb5

Please sign in to comment.