Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Show to If #24

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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