diff options
author | Markus Wüstenberg | 2020-12-22 10:53:22 +0100 |
---|---|---|
committer | GitHub | 2020-12-22 10:53:22 +0100 |
commit | f22ce3fb68ca6702a683b7759410ae50a9fddabd (patch) | |
tree | aace6553bb5c48b99d7205fe56d6b7d630b7799a /gomponents.go | |
parent | 428a2519eaa8e16767f86b417abd5af007f24fc6 (diff) | |
download | gomponents-f22ce3fb68ca6702a683b7759410ae50a9fddabd.tar.lz gomponents-f22ce3fb68ca6702a683b7759410ae50a9fddabd.tar.zst gomponents-f22ce3fb68ca6702a683b7759410ae50a9fddabd.zip |
Add If helper function (#57)
Used to inline conditional nodes.
Diffstat (limited to 'gomponents.go')
-rw-r--r-- | gomponents.go | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/gomponents.go b/gomponents.go index de46b7c..1986a1e 100644 --- a/gomponents.go +++ b/gomponents.go @@ -222,11 +222,11 @@ func Group(children []Node) Node { // Example: // items := []string{"hat", "partyhat"} // -// lis := g.Map(len(items), func(i int) g.Node { -// return g.El("li", g.Text(items[i])) +// lis := Map(len(items), func(i int) Node { +// return El("li", Text(items[i])) // }) // -// list := g.El("ul", lis...) +// list := El("ul", lis...) func Map(length int, cb func(i int) Node) []Node { var nodes []Node for i := 0; i < length; i++ { @@ -234,3 +234,16 @@ func Map(length int, cb func(i int) Node) []Node { } return nodes } + +// If condition is true, return the given Node. Otherwise, return nil. +// This helper function is good for inlining elements conditionally. +// Example: +// El("div", +// If(showMessage, El("span", "You lost your hat.")), +// ) +func If(condition bool, n Node) Node { + if condition { + return n + } + return nil +} |