Skip to content

Commit

Permalink
Keep the same type as result of If, to avoid types matchers to fail
Browse files Browse the repository at this point in the history
  • Loading branch information
willoma committed May 8, 2024
1 parent b244438 commit d054409
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
23 changes: 9 additions & 14 deletions class.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@ package gomplements
type Class string

// If allows to add a CSS class to an Element, only if a condition is met.
func (c Class) If(condition bool) ParentModifier {
return &conditionalClass{class: c, cond: condition}
}

type conditionalClass struct {
class Class
cond bool
}

func (c *conditionalClass) ModifyParent(p Element) {
if c.cond {
p.With(c.class)
func (c Class) If(condition bool) Class {
if condition {
return c
}
return ""
}

// Classes contains multiple CSS classes to be applied in the class attribute of an Element.
Expand All @@ -28,8 +20,11 @@ func (c Classes) Classes() []Class {
}

// If allows to add multiple CSS classes to an Element, only if a condition is met.
func (c Classes) If(condition bool) ParentModifier {
return &conditionalClasses{classes: c, cond: condition}
func (c Classes) If(cond bool) Classes {
if cond {
return c
}
return []Class{}
}

type conditionalClasses struct {
Expand Down
41 changes: 35 additions & 6 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,67 @@ func (e *elem) With(children ...any) Element {
for _, c := range children {
switch c := c.(type) {
case func(...gomponents.Node) gomponents.Node:
if c == nil {
continue
}
e.elemFn = c
case Class:
e.classes[c] = true
if c != "" {
e.classes[c] = true
}
case Classer:
e.classes[c.Class()] = true
cl := c.Class()
if cl != "" {
e.classes[cl] = true
}
case Classeser:
for _, cl := range c.Classes() {
e.classes[cl] = true
if cl != "" {
e.classes[cl] = true
}
}
case Styles:
for prop, val := range c {
e.styles[prop] = val
}
case ParentModifierAndNode:
if c == nil {
continue
}
c.ModifyParent(e)
e.elements = append(e.elements, c)
case ParentModifier:
if c == nil {
continue
}
c.ModifyParent(e)
case Element:
if c == nil {
continue
}
e.elements = append(e.elements, c)
case gomponents.Node:
if c == nil {
continue
}
if IsAttribute(c) {
e.attributes = append(e.attributes, c)
} else {
e.elements = append(e.elements, c)
}
case ID:
e.attributes = append(e.attributes, html.ID(string(c)))
if c != "" {
e.attributes = append(e.attributes, html.ID(string(c)))
}
case string:
e.elements = append(e.elements, gomponents.Text(c))
if c != "" {
e.elements = append(e.elements, gomponents.Text(c))
}
case fmt.Stringer:
e.elements = append(e.elements, gomponents.Text(c.String()))
s := c.String()
if s != "" {
e.elements = append(e.elements, gomponents.Text(s))
}
case []any:
e.With(c...)
}
Expand Down
16 changes: 4 additions & 12 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@ package gomplements
type Styles map[string]string

// If allows to apply CSS styles to an Element, only if a condition is met.
func (s Styles) If(cond bool) ParentModifier {
return &conditionalStyles{styles: s, cond: cond}
}

type conditionalStyles struct {
styles Styles
cond bool
}

func (c *conditionalStyles) ModifyParent(p Element) {
if c.cond {
p.With(c.styles)
func (s Styles) If(cond bool) Styles {
if cond {
return s
}
return Styles{}
}

0 comments on commit d054409

Please sign in to comment.