Skip to content

Commit

Permalink
Merge branch 'main' into chasefleming/todo-example
Browse files Browse the repository at this point in the history
  • Loading branch information
chasefleming committed Oct 24, 2023
2 parents 8dd4dc3 + d47eef6 commit 5cc22e7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 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
26 changes: 23 additions & 3 deletions htmx/htmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,46 @@ const (
HXDelete = "hx-delete"
HXPatch = "hx-patch"

// Request Headers and Content-Type
HXHeaders = "hx-headers"
HXContent = "hx-content"

// Request Parameters
HXParams = "hx-params"
HXValues = "hx-values"

// Request Timeout and Retries
HXTimeout = "hx-timeout"
HXRetry = "hx-retry"
HXRetryTimeout = "hx-retry-timeout"

// Response Processing
HXSwap = "hx-swap"
HXTarget = "hx-target"
HXSwapOOB = "hx-swap-oob"
HXSelect = "hx-select"
HXExt = "hx-ext"
HXVals = "hx-vals"

// Events
HXTrigger = "hx-trigger"
HXConfirm = "hx-confirm"
HXTrigger = "hx-trigger"
HXConfirm = "hx-confirm"
HXOn = "hx-on"
HXTriggeringElement = "hx-triggering-element"
HXTriggeringEvent = "hx-triggering-event"

// Indicators
HXIndicator = "hx-indicator"

// History
HXPushURL = "hx-push-url"
HXPushURL = "hx-push-url"
HXHistoryElt = "hx-history-elt"
HXHistoryAttr = "hx-history-attr"

// Error Handling
HXBoost = "hx-boost"
HXError = "hx-error"

// Caching
HXCache = "hx-cache"
)
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 5cc22e7

Please sign in to comment.