feat: improve component support

This commit is contained in:
Joao Goncalves
2025-11-03 00:41:05 -01:00
parent c0e050b0f8
commit 23aebf73f2
3 changed files with 117 additions and 104 deletions

View File

@@ -9,13 +9,15 @@ func main() {
v := via.New()
v.Page("/", func(c *via.Context) {
counter1 := c.Component(counterComponent)
counter2 := c.Component(counterComponent)
counterComp1 := c.Component(counterCompFn)
counterComp2 := c.Component(counterCompFn)
c.View(func() h.H {
return h.Div(
counter1(),
counter2(),
h.H1(h.Text("Counter 1")),
counterComp1(),
h.H1(h.Text("Counter 2")),
counterComp2(),
)
})
})
@@ -23,22 +25,18 @@ func main() {
v.Start(":3000")
}
type Counter struct{ Count int }
func counterComponent(c *via.Context) {
s := Counter{Count: 0}
func counterCompFn(c *via.Context) {
count := 0
step := c.Signal(1)
increment := c.Action(func() {
s.Count += step.Int()
count += step.Int()
c.Sync()
})
c.View(func() h.H {
return h.Div(
h.P(h.Textf("Count: %d", s.Count)),
h.P(h.Textf("Count: %d", count)),
h.P(h.Span(h.Text("Step: ")), h.Span(step.Text())),
h.Label(
h.Text("Update Step: "),
@@ -48,48 +46,3 @@ func counterComponent(c *via.Context) {
)
})
}
//
// c.View(func() h.H {
// return Layout(
// h.Div(
// h.Meta(h.Data("init", "@get('/_sse')")),
// h.P(h.Data("text", "$via-ctx")),
// h.Div(
// counter(),
// h.Data("signals:step", "1"),
// h.Label(h.Text("Step")),
// h.Input(h.Data("bind", "step")),
// h.Button(
// h.Text("Trigger foo"),
// h.Data("on:click", "@get('/_action/foo')"),
// ),
// ),
// ),
// )
// })
// conterComponent := c.Component("counter1", CounterComponent)
//
// in c.View of page add CounterComponent
//
// func CounterComponent(c *via.Context){
// s := CounterState{ Count: 1 }
// step := c.Signal(1)
//
// c.View(func() h.H {
// return h.Div(
// h.P(h.Textf("Count: %d", s.Count)),
// h.Label(
// h.Text("Step"),
// h.Input(h.Type("number"), step.Bind()),
// ),
// h.Button(h.Text("Increment"), h.OnClick("inc")),
// )
// })
//
// c.Action("inc", func() {
// s.Count += step
// c.Sync()
// })
// }