about summary refs log tree commit diff stats
path: root/el/elements.go
diff options
context:
space:
mode:
authorMarkus Wüstenberg2020-10-28 16:16:18 +0100
committerGitHub2020-10-28 16:16:18 +0100
commit18e90339fcac48806a5777766aeb256be2b8c4bc (patch)
tree7b5bd103e15db8a4ed81981e6b83b0d547d34540 /el/elements.go
parent8f17dba6f1268ab8c8810d7940d1484315cdb825 (diff)
downloadgomponents-18e90339fcac48806a5777766aeb256be2b8c4bc.tar.lz
gomponents-18e90339fcac48806a5777766aeb256be2b8c4bc.tar.zst
gomponents-18e90339fcac48806a5777766aeb256be2b8c4bc.zip
Add element helpers and refactor (#34)
This change adds a lot of element helpers, and refactors:
- helpers into simple, text, and other helpers
- most tests into table-driven tests, so they're easier to read

Thanks to @oderwat for pushing me to improve the tests. 😉
Diffstat (limited to 'el/elements.go')
-rw-r--r--el/elements.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/el/elements.go b/el/elements.go
new file mode 100644
index 0000000..4ca001f
--- /dev/null
+++ b/el/elements.go
@@ -0,0 +1,70 @@
+// Package el provides shortcuts and helpers to common HTML elements.
+// See https://developer.mozilla.org/en-US/docs/Web/HTML/Element for a list of elements.
+package el
+
+import (
+	"fmt"
+	"strings"
+
+	g "github.com/maragudk/gomponents"
+)
+
+func A(href string, children ...g.Node) g.NodeFunc {
+	return g.El("a", g.Attr("href", href), g.Group(children))
+}
+
+// Document returns an special kind of Node that prefixes its children with the string "<!doctype html>".
+func Document(children ...g.Node) g.NodeFunc {
+	return func() string {
+		var b strings.Builder
+		b.WriteString("<!doctype html>")
+		for _, c := range children {
+			b.WriteString(c.Render())
+		}
+		return b.String()
+	}
+}
+
+// Form returns an element with name "form", the given action and method attributes, and the given children.
+func Form(action, method string, children ...g.Node) g.NodeFunc {
+	return g.El("form", g.Attr("action", action), g.Attr("method", method), g.Group(children))
+}
+
+func Img(src, alt string, children ...g.Node) g.NodeFunc {
+	return g.El("img", g.Attr("src", src), g.Attr("alt", alt), g.Group(children))
+}
+
+// Input returns an element with name "input", the given type and name attributes, and the given children.
+// Note that "type" is a keyword in Go, so the parameter is called typ.
+func Input(typ, name string, children ...g.Node) g.NodeFunc {
+	return g.El("input", g.Attr("type", typ), g.Attr("name", name), g.Group(children))
+}
+
+// Label returns an element with name "label", the given for attribute, and the given children.
+// Note that "for" is a keyword in Go, so the parameter is called forr.
+func Label(forr string, children ...g.Node) g.NodeFunc {
+	return g.El("label", g.Attr("for", forr), g.Group(children))
+}
+
+// Option returns an element with name "option", the given text content and value attribute, and the given children.
+func Option(text, value string, children ...g.Node) g.NodeFunc {
+	return g.El("option", g.Attr("value", value), g.Text(text), g.Group(children))
+}
+
+// Progress returns an element with name "progress", the given value and max attributes, and the given children.
+func Progress(value, max float64, children ...g.Node) g.NodeFunc {
+	return g.El("progress",
+		g.Attr("value", fmt.Sprintf("%v", value)),
+		g.Attr("max", fmt.Sprintf("%v", max)),
+		g.Group(children))
+}
+
+// Select returns an element with name "select", the given name attribute, and the given children.
+func Select(name string, children ...g.Node) g.NodeFunc {
+	return g.El("select", g.Attr("name", name), g.Group(children))
+}
+
+// Textarea returns an element with name "textarea", the given name attribute, and the given children.
+func Textarea(name string, children ...g.Node) g.NodeFunc {
+	return g.El("textarea", g.Attr("name", name), g.Group(children))
+}