1
0

Delete low-value helper components (#115)

I'd rather reserve the package for components that have proven
repeatedly useful, like `Classes` and `HTML5`.
This commit is contained in:
Markus Wüstenberg 2022-11-03 11:32:16 +01:00 committed by GitHub
parent 3bb4e3efea
commit b12942fb32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 124 deletions

View File

@ -1,37 +0,0 @@
package components
import (
"io"
"sort"
"strings"
g "github.com/maragudk/gomponents"
"github.com/maragudk/gomponents/html"
)
// Classes is a map of strings to booleans, which Renders to an attribute with name "class".
// The attribute value is a sorted, space-separated string of all the map keys,
// for which the corresponding map value is true.
type Classes map[string]bool
func (c Classes) Render(w io.Writer) error {
var included []string
for c, include := range c {
if include {
included = append(included, c)
}
}
sort.Strings(included)
return html.Class(strings.Join(included, " ")).Render(w)
}
func (c Classes) Type() g.NodeType {
return g.AttributeType
}
// String satisfies fmt.Stringer.
func (c Classes) String() string {
var b strings.Builder
_ = c.Render(&b)
return b.String()
}

View File

@ -1,39 +0,0 @@
package components_test
import (
"os"
"testing"
g "github.com/maragudk/gomponents"
c "github.com/maragudk/gomponents/components"
"github.com/maragudk/gomponents/internal/assert"
)
func TestClasses(t *testing.T) {
t.Run("given a map, returns sorted keys from the map with value true", func(t *testing.T) {
assert.Equal(t, ` class="boheme-hat hat partyhat"`, c.Classes{
"boheme-hat": true,
"hat": true,
"partyhat": true,
"turtlehat": false,
})
})
t.Run("renders as attribute in an element", func(t *testing.T) {
e := g.El("div", c.Classes{"hat": true})
assert.Equal(t, `<div class="hat"></div>`, e)
})
t.Run("also works with fmt", func(t *testing.T) {
a := c.Classes{"hat": true}
if a.String() != ` class="hat"` {
t.FailNow()
}
})
}
func ExampleClasses() {
e := g.El("div", c.Classes{"party-hat": true, "boring-hat": false})
_ = e.Render(os.Stdout)
// Output: <div class="party-hat"></div>
}

View File

@ -2,6 +2,10 @@
package components package components
import ( import (
"io"
"sort"
"strings"
g "github.com/maragudk/gomponents" g "github.com/maragudk/gomponents"
. "github.com/maragudk/gomponents/html" . "github.com/maragudk/gomponents/html"
) )
@ -31,3 +35,30 @@ func HTML5(p HTML5Props) g.Node {
), ),
) )
} }
// Classes is a map of strings to booleans, which Renders to an attribute with name "class".
// The attribute value is a sorted, space-separated string of all the map keys,
// for which the corresponding map value is true.
type Classes map[string]bool
func (c Classes) Render(w io.Writer) error {
var included []string
for c, include := range c {
if include {
included = append(included, c)
}
}
sort.Strings(included)
return Class(strings.Join(included, " ")).Render(w)
}
func (c Classes) Type() g.NodeType {
return g.AttributeType
}
// String satisfies fmt.Stringer.
func (c Classes) String() string {
var b strings.Builder
_ = c.Render(&b)
return b.String()
}

View File

@ -1,6 +1,7 @@
package components_test package components_test
import ( import (
"os"
"testing" "testing"
g "github.com/maragudk/gomponents" g "github.com/maragudk/gomponents"
@ -30,3 +31,32 @@ func TestHTML5(t *testing.T) {
assert.Equal(t, `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Hat</title></head><body></body></html>`, e) assert.Equal(t, `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Hat</title></head><body></body></html>`, e)
}) })
} }
func TestClasses(t *testing.T) {
t.Run("given a map, returns sorted keys from the map with value true", func(t *testing.T) {
assert.Equal(t, ` class="boheme-hat hat partyhat"`, Classes{
"boheme-hat": true,
"hat": true,
"partyhat": true,
"turtlehat": false,
})
})
t.Run("renders as attribute in an element", func(t *testing.T) {
e := g.El("div", Classes{"hat": true})
assert.Equal(t, `<div class="hat"></div>`, e)
})
t.Run("also works with fmt", func(t *testing.T) {
a := Classes{"hat": true}
if a.String() != ` class="hat"` {
t.FailNow()
}
})
}
func ExampleClasses() {
e := g.El("div", Classes{"party-hat": true, "boring-hat": false})
_ = e.Render(os.Stdout)
// Output: <div class="party-hat"></div>
}

View File

@ -1,18 +0,0 @@
package components
import (
g "github.com/maragudk/gomponents"
. "github.com/maragudk/gomponents/html"
)
func InputHidden(name, value string, children ...g.Node) g.Node {
return Input(Type("hidden"), Name(name), Value(value), g.Group(children))
}
func LinkStylesheet(href string, children ...g.Node) g.Node {
return Link(Rel("stylesheet"), Href(href), g.Group(children))
}
func LinkPreload(href, as string, children ...g.Node) g.Node {
return Link(Rel("preload"), Href(href), As(as), g.Group(children))
}

View File

@ -1,30 +0,0 @@
package components_test
import (
"testing"
g "github.com/maragudk/gomponents"
c "github.com/maragudk/gomponents/components"
"github.com/maragudk/gomponents/internal/assert"
)
func TestInputHidden(t *testing.T) {
t.Run("returns an input element with type hidden, and the given name and value", func(t *testing.T) {
n := c.InputHidden("id", "partyhat", g.Attr("class", "hat"))
assert.Equal(t, `<input type="hidden" name="id" value="partyhat" class="hat">`, n)
})
}
func TestLinkStylesheet(t *testing.T) {
t.Run("returns a link element with rel stylesheet and the given href", func(t *testing.T) {
n := c.LinkStylesheet("style.css", g.Attr("media", "print"))
assert.Equal(t, `<link rel="stylesheet" href="style.css" media="print">`, n)
})
}
func TestLinkPreload(t *testing.T) {
t.Run("returns a link element with rel preload and the given href and as", func(t *testing.T) {
n := c.LinkPreload("party.woff2", "font", g.Attr("type", "font/woff2"))
assert.Equal(t, `<link rel="preload" href="party.woff2" as="font" type="font/woff2">`, n)
})
}