htmgo/examples/ws-example/pages/index.go

58 lines
1.4 KiB
Go
Raw Normal View History

2024-10-08 17:48:28 +00:00
package pages
import (
"fmt"
2024-10-16 20:29:01 +00:00
"github.com/maddalax/htmgo/extensions/websocket/ws"
2024-10-08 17:48:28 +00:00
"github.com/maddalax/htmgo/framework/h"
2024-10-16 20:43:34 +00:00
"github.com/maddalax/htmgo/framework/session"
2024-10-17 14:53:30 +00:00
"ws-example/partials"
2024-10-08 17:48:28 +00:00
)
func IndexPage(ctx *h.RequestContext) *h.Page {
2024-10-16 20:43:34 +00:00
sessionId := session.GetSessionId(ctx)
2024-10-08 17:48:28 +00:00
return h.NewPage(
RootPage(
2024-10-09 14:57:51 +00:00
ctx,
2024-10-08 17:48:28 +00:00
h.Div(
2024-10-16 20:43:34 +00:00
h.Attribute("ws-connect", fmt.Sprintf("/ws?sessionId=%s", sessionId)),
2024-10-08 17:48:28 +00:00
h.Class("flex flex-col gap-4 items-center pt-24 min-h-screen bg-neutral-100"),
h.H3(
h.Id("intro-text"),
h.Text("Repeater Example"),
h.Class("text-2xl"),
),
h.Div(
2024-11-04 16:37:41 +00:00
h.Id("ws-metrics"),
),
2024-10-08 17:48:28 +00:00
partials.CounterForm(ctx, partials.CounterProps{Id: "counter-1"}),
partials.Repeater(ctx, partials.RepeaterProps{
Id: "repeater-1",
2024-10-16 20:23:36 +00:00
OnAdd: func(data ws.HandlerData) {
ws.BroadcastServerSideEvent("increment", map[string]any{})
},
2024-10-16 20:23:36 +00:00
OnRemove: func(data ws.HandlerData, index int) {
ws.BroadcastServerSideEvent("decrement", map[string]any{})
},
2024-10-08 17:48:28 +00:00
AddButton: h.Button(
h.Text("+ Add Item"),
),
RemoveButton: func(index int, children ...h.Ren) *h.Element {
return h.Button(
h.Text("Remove"),
h.Children(children...),
)
},
Item: func(index int) *h.Element {
return h.Input(
"text",
2024-10-08 17:48:28 +00:00
h.Class("border border-gray-300 rounded p-2"),
h.Value(fmt.Sprintf("item %d", index)),
)
2024-10-08 17:48:28 +00:00
},
}),
),
),
)
}