Skip to content

Commit

Permalink
Add Boost, Get, and Post attributes (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswustenberg committed Feb 24, 2023
2 parents f6c2489 + 75dd2af commit 4c86ded
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# template
# gomponents-htmx

[![GoDoc](https://pkg.go.dev/badge/github.com/maragudk/gomponents-htmx)](https://pkg.go.dev/github.com/maragudk/gomponents-htmx)
[![CI](https://github.com/maragudk/gomponents-htmx/actions/workflows/ci.yml/badge.svg)](https://github.com/maragudk/gomponents-htmx/actions/workflows/ci.yml)

[HTMX](https://htmx.org) attributes and helpers for [gomponents](https://www.gomponents.com).

Made in 🇩🇰 by [maragu](https://www.maragu.dk/), maker of [online Go courses](https://www.golang.dk/).
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module template
module github.com/maragudk/gomponents-htmx

go 1.19
go 1.20

require github.com/maragudk/gomponents v0.20.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/maragudk/gomponents v0.20.1 h1:TeJY1fXEcfUvzmvjeUgxol42dvkYMggK1c0V67crWWs=
github.com/maragudk/gomponents v0.20.1/go.mod h1:nHkNnZL6ODgMBeJhrZjkMHVvNdoYsfmpKB2/hjdQ0Hg=
29 changes: 29 additions & 0 deletions htmx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package htmx provides HTMX attributes and helpers for gomponents.
// See https://htmx.org/
package htmx

import (
g "github.com/maragudk/gomponents"
)

// Boost for links and forms.
// See https://htmx.org/attributes/hx-boost
func Boost(v string) g.Node {
return attr("boost", v)
}

// Get the url.
// See https://htmx.org/attributes/hx-get
func Get(url string) g.Node {
return attr("get", url)
}

// Post to the url.
// See https://htmx.org/attributes/hx-post
func Post(url string) g.Node {
return attr("post", url)
}

func attr(name, value string) g.Node {
return g.Attr("hx-"+name, value)
}
26 changes: 26 additions & 0 deletions htmx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package htmx_test

import (
"fmt"
"testing"

g "github.com/maragudk/gomponents"

. "github.com/maragudk/gomponents-htmx"
"github.com/maragudk/gomponents-htmx/internal/assert"
)

func TestAttributes(t *testing.T) {
cases := map[string]func(string) g.Node{
"boost": Boost,
"get": Get,
"post": Post,
}

for name, fn := range cases {
t.Run(fmt.Sprintf(`should output hx-%v="hat"`, name), func(t *testing.T) {
n := g.El("div", fn("hat"))
assert.Equal(t, fmt.Sprintf(`<div hx-%v="hat"></div>`, name), n)
})
}
}
29 changes: 29 additions & 0 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package assert provides testing helpers.
package assert

import (
"strings"
"testing"

g "github.com/maragudk/gomponents"
)

// Equal checks for equality between the given expected string and the rendered Node string.
func Equal(t *testing.T, expected string, actual g.Node) {
t.Helper()

var b strings.Builder
_ = actual.Render(&b)
if expected != b.String() {
t.Fatalf(`expected "%v" but got "%v"`, expected, b.String())
}
}

// Error checks for a non-nil error.
func Error(t *testing.T, err error) {
t.Helper()

if err == nil {
t.Fatal("error is nil")
}
}
1 change: 0 additions & 1 deletion template.go

This file was deleted.

0 comments on commit 4c86ded

Please sign in to comment.