From 1b2211f8727b1e50f542afffb1f89736f7c0b747 Mon Sep 17 00:00:00 2001
From: Markus Wüstenberg
Date: Thu, 26 Sep 2024 11:13:11 +0200
Subject: Add ExampleMap_index

For getting the slice index using a counter outside the closure.
---
 gomponents_test.go | 12 ++++++++++++
 1 file changed, 12 insertions(+)

(limited to 'gomponents_test.go')

diff --git a/gomponents_test.go b/gomponents_test.go
index c8dab1b..026cde4 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -255,6 +255,18 @@ func ExampleMap() {
 	// Output: <ul><li>party hat</li><li>super hat</li></ul>
 }
 
+func ExampleMap_index() {
+	items := []string{"party hat", "super hat"}
+	var index int
+	e := g.El("ul", g.Map(items, func(i string) g.Node {
+		e := g.El("li", g.Textf("%v: %v", index, i))
+		index++
+		return e
+	}))
+	_ = e.Render(os.Stdout)
+	// Output: <ul><li>0: party hat</li><li>1: super hat</li></ul>
+}
+
 func TestGroup(t *testing.T) {
 	t.Run("groups multiple nodes into one", func(t *testing.T) {
 		children := []g.Node{g.El("br", g.Attr("id", "hat")), g.El("hr")}
-- 
cgit 1.4.1