about summary refs log tree commit diff stats
path: root/gomponents.go
diff options
context:
space:
mode:
authorMarkus Wüstenberg2024-09-24 09:46:45 +0200
committerGitHub2024-09-24 09:46:45 +0200
commitf58a0660823bc88c901eba4a130f1d4996729c73 (patch)
treefcf96b76fe265e332c7f442c8052014c77c0d720 /gomponents.go
parent3a3de2932c6ec72c23c63608b21df93f61d88df5 (diff)
downloadgomponents-f58a0660823bc88c901eba4a130f1d4996729c73.tar.lz
gomponents-f58a0660823bc88c901eba4a130f1d4996729c73.tar.zst
gomponents-f58a0660823bc88c901eba4a130f1d4996729c73.zip
Make `Group` a type (#202)
This changes `Group` to be a type instead of a function, which means it
can support both grouping `[]Node` as well as doing variadic-ish slice things
like `Group{n1, n2}`.

This also means that `Map` can just return a `Group`.

Special thanks to @deitrix for this simple and elegant solution that,
for some reason or another, has had me perplexed for a long time.

Fixes #201.
Diffstat (limited to 'gomponents.go')
-rw-r--r--gomponents.go28
1 files changed, 11 insertions, 17 deletions
diff --git a/gomponents.go b/gomponents.go
index 1a3b4ee..a17e1da 100644
--- a/gomponents.go
+++ b/gomponents.go
@@ -113,8 +113,8 @@ func renderChild(w *statefulWriter, c Node, t NodeType) {
 
 	// Rendering groups like this is still important even though a group can render itself,
 	// since otherwise attributes will sometimes be ignored.
-	if g, ok := c.(group); ok {
-		for _, groupC := range g.children {
+	if g, ok := c.(Group); ok {
+		for _, groupC := range g {
 			renderChild(w, groupC, t)
 		}
 		return
@@ -246,8 +246,8 @@ func Rawf(format string, a ...interface{}) Node {
 	})
 }
 
-// Map a slice of anything to a slice of Nodes.
-func Map[T any](ts []T, cb func(T) Node) []Node {
+// Map a slice of anything to a [Group] (which is just a slice of [Node]s).
+func Map[T any](ts []T, cb func(T) Node) Group {
 	var nodes []Node
 	for _, t := range ts {
 		nodes = append(nodes, cb(t))
@@ -255,27 +255,21 @@ func Map[T any](ts []T, cb func(T) Node) []Node {
 	return nodes
 }
 
-type group struct {
-	children []Node
-}
+// Group a slice of [Node]s into one Node, while still being usable like a regular slice of [Node]s.
+// A [Group] can render directly, but if any of the direct children are [AttributeType], they will be ignored,
+// to not produce invalid HTML.
+type Group []Node
 
 // String satisfies [fmt.Stringer].
-func (g group) String() string {
+func (g Group) String() string {
 	var b strings.Builder
 	_ = g.Render(&b)
 	return b.String()
 }
 
 // Render satisfies [Node].
-func (g group) Render(w io.Writer) error {
-	return render(w, nil, g.children...)
-}
-
-// Group a slice of Nodes into one Node. Useful for grouping the result of [Map] into one [Node].
-// A [Group] can render directly, but if any of the direct children are [AttributeType], they will be ignored,
-// to not produce invalid HTML.
-func Group(children []Node) Node {
-	return group{children: children}
+func (g Group) Render(w io.Writer) error {
+	return render(w, nil, g...)
 }
 
 // If condition is true, return the given [Node]. Otherwise, return nil.