From 8562f9e85e0aa818124f714632070f97ef741df0 Mon Sep 17 00:00:00 2001 From: maddalax Date: Sun, 22 Sep 2024 11:25:18 -0500 Subject: [PATCH] remove ternary --- framework/h/attribute.go | 2 +- framework/h/conditionals.go | 9 +++------ framework/h/lifecycle.go | 1 + framework/h/render_test.go | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/framework/h/attribute.go b/framework/h/attribute.go index 9ea9341..507f66f 100644 --- a/framework/h/attribute.go +++ b/framework/h/attribute.go @@ -129,7 +129,7 @@ func Hidden() Ren { return Attribute("style", "display:none") } -func Class(value ...string) Ren { +func Class(value ...string) *AttributeMap { return Attribute("class", MergeClasses(value...)) } diff --git a/framework/h/conditionals.go b/framework/h/conditionals.go index 70b04df..379f519 100644 --- a/framework/h/conditionals.go +++ b/framework/h/conditionals.go @@ -9,13 +9,10 @@ func If(condition bool, node Ren) Ren { } func Ternary[T any](value bool, a T, b T) T { - if value { - return a - } - return b + return IfElse(value, a, b) } -func IfElse(condition bool, node Ren, node2 Ren) Ren { +func IfElse[T any](condition bool, node T, node2 T) T { if condition { return node } else { @@ -23,7 +20,7 @@ func IfElse(condition bool, node Ren, node2 Ren) Ren { } } -func IfElseLazy(condition bool, cb1 func() Ren, cb2 func() Ren) Ren { +func IfElseLazy[T any](condition bool, cb1 func() T, cb2 func() T) T { if condition { return cb1() } else { diff --git a/framework/h/lifecycle.go b/framework/h/lifecycle.go index 14e71b2..94a7256 100644 --- a/framework/h/lifecycle.go +++ b/framework/h/lifecycle.go @@ -161,6 +161,7 @@ func ToggleClassOnElement(selector, class string) ComplexJsCommand { return EvalJs(fmt.Sprintf(` var el = document.querySelector('%s'); if(el) { el.classList.toggle('%s'); }`, + selector, class, )) } diff --git a/framework/h/render_test.go b/framework/h/render_test.go index 1439bb2..0e78d03 100644 --- a/framework/h/render_test.go +++ b/framework/h/render_test.go @@ -43,7 +43,7 @@ func TestRawContent(t *testing.T) { func TestConditional(t *testing.T) { result := Render( Div( - IfElse(true, Text("true"), Text("false")), + Ternary(true, Text("true"), Text("false")), ), ) assert.Equal(t, "
true
", result)