fix tests

This commit is contained in:
maddalax 2024-09-22 11:53:41 -05:00
parent e618ad99f4
commit 9e44fb5d52
4 changed files with 24 additions and 12 deletions

View file

@ -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{}
}

View file

@ -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)

View file

@ -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),

View file

@ -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)
}