fix tests
This commit is contained in:
parent
e618ad99f4
commit
9e44fb5d52
4 changed files with 24 additions and 12 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/maddalax/htmgo/framework/hx"
|
"github.com/maddalax/htmgo/framework/hx"
|
||||||
"github.com/maddalax/htmgo/framework/internal/util"
|
"github.com/maddalax/htmgo/framework/internal/util"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LifeCycle struct {
|
type LifeCycle struct {
|
||||||
|
|
@ -37,6 +38,11 @@ func validateCommands(cmds []Command) {
|
||||||
func (l *LifeCycle) OnEvent(event hx.Event, cmd ...Command) *LifeCycle {
|
func (l *LifeCycle) OnEvent(event hx.Event, cmd ...Command) *LifeCycle {
|
||||||
validateCommands(cmd)
|
validateCommands(cmd)
|
||||||
|
|
||||||
|
if strings.HasPrefix(event, "htmx:") {
|
||||||
|
event = event[5:]
|
||||||
|
event = util.ConvertCamelToDash(fmt.Sprintf("hx-on::%s", event))
|
||||||
|
}
|
||||||
|
|
||||||
if l.handlers[event] == nil {
|
if l.handlers[event] == nil {
|
||||||
l.handlers[event] = []Command{}
|
l.handlers[event] = []Command{}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -142,12 +142,10 @@ func (l *LifeCycle) Render(context *RenderContext) {
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
switch c := command.(type) {
|
switch c := command.(type) {
|
||||||
case SimpleJsCommand:
|
case SimpleJsCommand:
|
||||||
eventName := hx.FormatEventName(event, true)
|
m[event] += fmt.Sprintf("%s;", c.Command)
|
||||||
m[eventName] += fmt.Sprintf("%s;", c.Command)
|
|
||||||
case ComplexJsCommand:
|
case ComplexJsCommand:
|
||||||
eventName := hx.FormatEventName(event, true)
|
|
||||||
context.AddScript(c.TempFuncName, c.Command)
|
context.AddScript(c.TempFuncName, c.Command)
|
||||||
m[eventName] += fmt.Sprintf("%s();", c.TempFuncName)
|
m[event] += fmt.Sprintf("%s();", c.TempFuncName)
|
||||||
case *AttributeMap:
|
case *AttributeMap:
|
||||||
for k, v := range c.ToMap() {
|
for k, v := range c.ToMap() {
|
||||||
l.fromAttributeMap(event, k, v, context)
|
l.fromAttributeMap(event, k, v, context)
|
||||||
|
|
|
||||||
|
|
@ -23,14 +23,6 @@ func ToHtmxTriggerName(event string) string {
|
||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormatEventName(event string, isDomEvent bool) string {
|
|
||||||
raw := ToHtmxTriggerName(event)
|
|
||||||
if isDomEvent {
|
|
||||||
return "on" + raw
|
|
||||||
}
|
|
||||||
return event
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTrigger(opts ...TriggerEvent) *Trigger {
|
func NewTrigger(opts ...TriggerEvent) *Trigger {
|
||||||
t := Trigger{
|
t := Trigger{
|
||||||
events: make([]TriggerEvent, 0),
|
events: make([]TriggerEvent, 0),
|
||||||
|
|
|
||||||
16
framework/internal/util/casing.go
Normal file
16
framework/internal/util/casing.go
Normal 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)
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue