diff options
author | Markus Wüstenberg | 2020-09-23 20:30:14 +0200 |
---|---|---|
committer | GitHub | 2020-09-23 20:30:14 +0200 |
commit | 05c31515c6e5d5a9633b3af4b58eb46456e0632c (patch) | |
tree | f7343b7af90620c066eafac48d73c8b72e4c6bb3 /attr/attributes.go | |
parent | c832941edb5e896a893c663e83aba7088ea40e6c (diff) | |
download | gomponents-05c31515c6e5d5a9633b3af4b58eb46456e0632c.tar.lz gomponents-05c31515c6e5d5a9633b3af4b58eb46456e0632c.tar.zst gomponents-05c31515c6e5d5a9633b3af4b58eb46456e0632c.zip |
Make attr.Classes a map type (#14)
This makes the usage syntax prettier. Instead of `attr.Classes(map[string]bool{})`, we can just use `attr.Classes{}`.
Diffstat (limited to 'attr/attributes.go')
-rw-r--r-- | attr/attributes.go | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/attr/attributes.go b/attr/attributes.go index 7ab27e4..56fcd74 100644 --- a/attr/attributes.go +++ b/attr/attributes.go @@ -19,15 +19,23 @@ func Class(v string) g.Node { return g.Attr("class", v) } -// Classes returns an attribute with name "class" and the value being a sorted, space-separated string of all the keys, -// for which the corresponding value is true. -func Classes(cs map[string]bool) 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() string { var included []string - for c, include := range cs { + for c, include := range c { if include { included = append(included, c) } } sort.Strings(included) - return g.Attr("class", strings.Join(included, " ")) + return g.Attr("class", strings.Join(included, " ")).Render() +} + +// String satisfies fmt.Stringer. +func (c Classes) String() string { + return c.Render() } |