Repository: maragudk/gomponents
Pull request #57 (2020-12-22 09:50:44)

Add If helper function
Used to inline conditional nodes.

codecov[bot]: # [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/57?src=pr&el=h1) Report
> Merging [#57](https://codecov.io/gh/maragudk/gomponents/pull/57?src=pr&el=desc) (1430f8c) into [master](https://codecov.io/gh/maragudk/gomponents/commit/428a2519eaa8e16767f86b417abd5af007f24fc6?el=desc) (428a251) will **decrease** coverage by `0.41%`.
> The diff coverage is `75.00%`.

[![Impacted file tree graph](https://codecov.io/gh/maragudk/gomponents/pull/57/graphs/tree.svg?width=650&height=150&src=pr&token=JYN032TYAC)](https://codecov.io/gh/maragudk/gomponents/pull/57?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##            master      #57      +/-   ##
===========================================
- Coverage   100.00%   99.58%   -0.42%     
===========================================
  Files            5        6       +1     
  Lines          239      243       +4     
===========================================
+ Hits           239      242       +3     
- Misses           0        1       +1     
```


| [Impacted Files](https://codecov.io/gh/maragudk/gomponents/pull/57?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [components/elements.go](https://codecov.io/gh/maragudk/gomponents/pull/57/diff?src=pr&el=tree#diff-Y29tcG9uZW50cy9lbGVtZW50cy5nbw==) | `0.00% <0.00%> (ø)` | |
| [gomponents.go](https://codecov.io/gh/maragudk/gomponents/pull/57/diff?src=pr&el=tree#diff-Z29tcG9uZW50cy5nbw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/maragudk/gomponents/pull/57?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/57?src=pr&el=footer). Last update [428a251...fc5710d](https://codecov.io/gh/maragudk/gomponents/pull/57?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/components/elements.go b/components/elements.go
new file mode 100644
index 0000000..baffba6
--- /dev/null
+++ b/components/elements.go
@@ -0,0 +1,10 @@
+package components
+
+import (
+	g "github.com/maragudk/gomponents"
+	. "github.com/maragudk/gomponents/html"
+)
+
+func InputHidden(name, value string, children ...g.Node) g.NodeFunc {
+	return Input(Type("hidden"), Name(name), Value(value), g.Group(children))
+}
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
+}


Test patch:
diff --git a/gomponents_test.go b/gomponents_test.go
index 26199c3..c88a796 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -207,3 +207,15 @@ func TestMap(t *testing.T) {
 		assert.Equal(t, `<ul><li>hat</li><li>partyhat</li><li>turtlehat</li></ul>`, list)
 	})
 }
+
+func TestIf(t *testing.T) {
+	t.Run("returns node if condition is true", func(t *testing.T) {
+		n := g.El("div", g.If(true, g.El("span")))
+		assert.Equal(t, "<div><span></span></div>", n)
+	})
+
+	t.Run("returns nil if condition is false", func(t *testing.T) {
+		n := g.El("div", g.If(false, g.El("span")))
+		assert.Equal(t, "<div></div>", n)
+	})
+}