Skip to content

Commit 9552629

Browse files
Reworking react
1 parent 4117f6a commit 9552629

File tree

3 files changed

+43
-59
lines changed

3 files changed

+43
-59
lines changed

playground/internal/react/codeBox.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,20 @@ import (
66
"github.com/gopherjs/gopherjs/js"
77
)
88

9-
func CodeBox(code string, setCode func(string), onCodeChanged func()) *Element {
9+
func CodeBox(code string, setCode func(string)) *Element {
1010
return CreateElement(codeBoxComponent, Props{
11-
`code`: code,
12-
`setCode`: setCode,
13-
`onCodeChanged`: onCodeChanged,
11+
`code`: code,
12+
`setCode`: setCode,
1413
})
1514
}
1615

1716
func codeBoxComponent(prop Props) *Element {
1817
cba := &codeBoxAssistant{
19-
code: prop[`code`].(string),
20-
setCode: prop.AsFunc(`setCode`),
21-
onCodeChanged: prop.AsFunc(`onCodeChanged`),
22-
textAreaRef: UseRef(),
18+
code: prop[`code`].(string),
19+
setCode: prop.AsFunc(`setCode`),
20+
textAreaRef: UseRef(),
2321
}
2422

25-
println("> code:", cba.code) // TODO(grantnelson-wf): REMOVE
26-
2723
return Div(Props{
2824
`className`: `box`,
2925
`id`: `content`,
@@ -48,24 +44,18 @@ func codeBoxComponent(prop Props) *Element {
4844
}
4945

5046
type codeBoxAssistant struct {
51-
code string
52-
setCode Func
53-
onCodeChanged Func
54-
textAreaRef *Ref
47+
code string
48+
setCode Func
49+
textAreaRef *Ref
5550
}
5651

5752
func (cba *codeBoxAssistant) onInput(e *js.Object) {
58-
if cba.onCodeChanged != nil {
59-
cba.onCodeChanged()
60-
}
53+
cba.setCode(e.Get(`target`).Get(`value`).String())
6154
}
6255

6356
func (cba *codeBoxAssistant) onKeyDown(e *js.Object) {
6457
if cba.handleKeyDown(e.Get(`keyCode`).Int()) {
6558
e.Call(`preventDefault`)
66-
if cba.onCodeChanged != nil {
67-
cba.onCodeChanged()
68-
}
6959
}
7060
}
7161

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package react
22

3+
import "fmt"
4+
35
func Playground() *Element {
46
return CreateElement(playgroundComponent, nil)
57
}
68

79
func playgroundComponent() *Element {
810
code, setCode := UseState("Hello World")
911

10-
onCodeChanged := func() {
11-
println("Code changed!") // TODO(grantnelson-wf): REMOVE
12-
}
12+
UseEffect(func() {
13+
println(fmt.Sprintf(`Code: %q`, code))
14+
}, code)
1315

1416
return Div(Props{
1517
`id`: `playground`,
1618
},
17-
CodeBox(code, setCode, onCodeChanged),
19+
CodeBox(code, setCode),
1820
)
1921
}

playground/playground.js

Lines changed: 27 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)