Skip to content

Commit

Permalink
Delete low-value helper components (#115)
Browse files Browse the repository at this point in the history
I'd rather reserve the package for components that have proven
repeatedly useful, like `Classes` and `HTML5`.
  • Loading branch information
markuswustenberg authored Nov 3, 2022
1 parent 3bb4e3e commit b12942f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 124 deletions.
37 changes: 0 additions & 37 deletions components/attributes.go

This file was deleted.

39 changes: 0 additions & 39 deletions components/attributes_test.go

This file was deleted.

31 changes: 31 additions & 0 deletions components/documents.go → components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
package components

import (
"io"
"sort"
"strings"

g "github.com/maragudk/gomponents"
. "github.com/maragudk/gomponents/html"
)
Expand Down Expand Up @@ -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()
}
30 changes: 30 additions & 0 deletions components/documents_test.go → components/components_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package components_test

import (
"os"
"testing"

g "github.com/maragudk/gomponents"
Expand Down Expand Up @@ -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)
})
}

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>
}
18 changes: 0 additions & 18 deletions components/elements.go

This file was deleted.

30 changes: 0 additions & 30 deletions components/elements_test.go

This file was deleted.

0 comments on commit b12942f

Please sign in to comment.