Skip to content

Commit

Permalink
Switch Show to If
Browse files Browse the repository at this point in the history
  • Loading branch information
chasefleming committed Oct 24, 2023
1 parent 59bf4b0 commit acca1c0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ ulElement := elem.Ul(nil, liElements)
```
In this example, we transformed a slice of strings into a list of `li` elements and then wrapped them in a `ul` element.

### Conditional Rendering with Show
### Conditional Rendering with `If`

`elem` provides a utility function `Show` for conditional rendering of elements.
`elem` provides a utility function `If` for conditional rendering of elements.

```go
isAdmin := true
Expand All @@ -80,7 +80,7 @@ guestLink := elem.A(elem.Attrs{attrs.Href: "/login"}, elem.Text("Login"))

content := elem.Div(nil,
elem.H1(nil, elem.Text("Dashboard")),
elem.Show(isAdmin, adminLink, guestLink),
elem.If(isAdmin, adminLink, guestLink),
)
```

Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func ApplyStyle(s Style) string {
}

// Show conditionally renders one of the provided elements based on the condition
func Show(condition bool, ifTrue, ifFalse Node) Node {
func If[T any](condition bool, ifTrue, ifFalse T) T {
if condition {
return ifTrue
}
Expand Down
6 changes: 3 additions & 3 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func TestApplyStyle(t *testing.T) {
assert.Equal(t, expected, actual)
}

func TestShow(t *testing.T) {
func TestIf(t *testing.T) {
trueElement := Div(nil, Text("True Condition"))
falseElement := Div(nil, Text("False Condition"))

resultTrue := Show(true, trueElement, falseElement)
resultTrue := If(true, trueElement, falseElement)
assert.Equal(t, trueElement.Render(), resultTrue.Render())

resultFalse := Show(false, trueElement, falseElement)
resultFalse := If(false, trueElement, falseElement)
assert.Equal(t, falseElement.Render(), resultFalse.Render())
}

Expand Down

0 comments on commit acca1c0

Please sign in to comment.