2024-07-18 00:24:43 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2024-07-23 22:16:53 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/vdom"
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wshutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Page(ctx context.Context, props map[string]any) any {
|
|
|
|
clicked, setClicked := vdom.UseState(ctx, false)
|
|
|
|
var clickedDiv *vdom.Elem
|
|
|
|
if clicked {
|
|
|
|
clickedDiv = vdom.Bind(`<div>clicked</div>`, nil)
|
|
|
|
}
|
|
|
|
clickFn := func() {
|
|
|
|
log.Printf("run clickFn\n")
|
|
|
|
setClicked(true)
|
|
|
|
}
|
|
|
|
return vdom.Bind(
|
|
|
|
`
|
|
|
|
<div>
|
|
|
|
<h1>hello world</h1>
|
|
|
|
<Button onClick="#bind:clickFn">hello</Button>
|
|
|
|
<bind key="clickedDiv"/>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
map[string]any{"clickFn": clickFn, "clickedDiv": clickedDiv},
|
|
|
|
)
|
2024-07-18 00:24:43 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:16:53 +02:00
|
|
|
func Button(ctx context.Context, props map[string]any) any {
|
|
|
|
ref := vdom.UseRef(ctx, nil)
|
|
|
|
clName, setClName := vdom.UseState(ctx, "button")
|
|
|
|
vdom.UseEffect(ctx, func() func() {
|
|
|
|
fmt.Printf("Button useEffect\n")
|
|
|
|
setClName("button mounted")
|
|
|
|
return nil
|
|
|
|
}, nil)
|
|
|
|
return vdom.Bind(`
|
|
|
|
<div className="#bind:clName" ref="#bind:ref" onClick="#bind:onClick">
|
|
|
|
<bind key="children"/>
|
|
|
|
</div>
|
|
|
|
`, map[string]any{"clName": clName, "ref": ref, "onClick": props["onClick"], "children": props["children"]})
|
2024-07-18 00:24:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-07-23 22:16:53 +02:00
|
|
|
wshutil.SetTermRawModeAndInstallShutdownHandlers(true)
|
|
|
|
defer wshutil.RestoreTermState()
|
2024-07-18 00:24:43 +02:00
|
|
|
}
|