Skip to content

Commit 75dd2af

Browse files
Add Boost, Get, and Post attributes
1 parent f6c2489 commit 75dd2af

File tree

7 files changed

+96
-4
lines changed

7 files changed

+96
-4
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
# template
1+
# gomponents-htmx
2+
3+
[![GoDoc](https://pkg.go.dev/badge/github.com/maragudk/gomponents-htmx)](https://pkg.go.dev/github.com/maragudk/gomponents-htmx)
4+
[![CI](https://github.com/maragudk/gomponents-htmx/actions/workflows/ci.yml/badge.svg)](https://github.com/maragudk/gomponents-htmx/actions/workflows/ci.yml)
5+
6+
[HTMX](https://htmx.org) attributes and helpers for [gomponents](https://www.gomponents.com).
27

38
Made in 🇩🇰 by [maragu](https://www.maragu.dk/), maker of [online Go courses](https://www.golang.dk/).

go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
module template
1+
module github.com/maragudk/gomponents-htmx
22

3-
go 1.19
3+
go 1.20
4+
5+
require github.com/maragudk/gomponents v0.20.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/maragudk/gomponents v0.20.1 h1:TeJY1fXEcfUvzmvjeUgxol42dvkYMggK1c0V67crWWs=
2+
github.com/maragudk/gomponents v0.20.1/go.mod h1:nHkNnZL6ODgMBeJhrZjkMHVvNdoYsfmpKB2/hjdQ0Hg=

htmx.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Package htmx provides HTMX attributes and helpers for gomponents.
2+
// See https://htmx.org/
3+
package htmx
4+
5+
import (
6+
g "github.com/maragudk/gomponents"
7+
)
8+
9+
// Boost for links and forms.
10+
// See https://htmx.org/attributes/hx-boost
11+
func Boost(v string) g.Node {
12+
return attr("boost", v)
13+
}
14+
15+
// Get the url.
16+
// See https://htmx.org/attributes/hx-get
17+
func Get(url string) g.Node {
18+
return attr("get", url)
19+
}
20+
21+
// Post to the url.
22+
// See https://htmx.org/attributes/hx-post
23+
func Post(url string) g.Node {
24+
return attr("post", url)
25+
}
26+
27+
func attr(name, value string) g.Node {
28+
return g.Attr("hx-"+name, value)
29+
}

htmx_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package htmx_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
g "github.com/maragudk/gomponents"
8+
9+
. "github.com/maragudk/gomponents-htmx"
10+
"github.com/maragudk/gomponents-htmx/internal/assert"
11+
)
12+
13+
func TestAttributes(t *testing.T) {
14+
cases := map[string]func(string) g.Node{
15+
"boost": Boost,
16+
"get": Get,
17+
"post": Post,
18+
}
19+
20+
for name, fn := range cases {
21+
t.Run(fmt.Sprintf(`should output hx-%v="hat"`, name), func(t *testing.T) {
22+
n := g.El("div", fn("hat"))
23+
assert.Equal(t, fmt.Sprintf(`<div hx-%v="hat"></div>`, name), n)
24+
})
25+
}
26+
}

internal/assert/assert.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Package assert provides testing helpers.
2+
package assert
3+
4+
import (
5+
"strings"
6+
"testing"
7+
8+
g "github.com/maragudk/gomponents"
9+
)
10+
11+
// Equal checks for equality between the given expected string and the rendered Node string.
12+
func Equal(t *testing.T, expected string, actual g.Node) {
13+
t.Helper()
14+
15+
var b strings.Builder
16+
_ = actual.Render(&b)
17+
if expected != b.String() {
18+
t.Fatalf(`expected "%v" but got "%v"`, expected, b.String())
19+
}
20+
}
21+
22+
// Error checks for a non-nil error.
23+
func Error(t *testing.T, err error) {
24+
t.Helper()
25+
26+
if err == nil {
27+
t.Fatal("error is nil")
28+
}
29+
}

template.go

-1
This file was deleted.

0 commit comments

Comments
 (0)