Skip to content

Commit fbab436

Browse files
updating props
1 parent 1ed139a commit fbab436

7 files changed

Lines changed: 118 additions & 112 deletions

File tree

playground/internal/react/bannerTitle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func BannerTitle(version string) *Element {
77
}
88

99
func bannerTitleComponent(props Props) *Element {
10-
version := As[string](props, `version`)
10+
version := props.GetString(`version`)
1111
return Span(Props{
1212
`id`: `banner-title`,
1313
},

playground/internal/react/bindings.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,37 @@ func (p Props) Affirm() Props {
101101
return p
102102
}
103103

104-
// As retrieves the property with the given name from the Props
105-
// and converts it to the specified type T.
106-
func As[T any](props Props, name string) T {
107-
if prop, ok := props[name]; ok {
108-
return prop.(T)
104+
func (p Props) Get(key string) any {
105+
if prop, ok := p[key]; ok {
106+
return prop
109107
}
110108
panic(ErrUndefinedPropKey)
111109
}
112110

113-
// AsFunc retrieves the property with the given name from the Props
111+
func (p Props) GetString(key string) string {
112+
return p.Get(key).(string)
113+
}
114+
115+
func (p Props) GetBool(key string) bool {
116+
return p.Get(key).(bool)
117+
}
118+
119+
func (p Props) GetFloat(key string) float64 {
120+
return p.Get(key).(float64)
121+
}
122+
123+
func (p Props) GetInt(key string) int {
124+
return int(p.GetFloat(key))
125+
}
126+
127+
// GetFunc retrieves the property with the given key from the Props
114128
// and converts it to a function of type Func.
115-
func AsFunc(props Props, name string) Func {
116-
if prop, ok := props[name]; ok {
117-
return Func(prop.(func(...any) *js.Object))
118-
}
119-
panic(ErrUndefinedPropKey)
129+
//
130+
// If a Setter is passed in as a prop, call this to read it out of the prop
131+
// as a Func since the externalizing of the Setter when passed through the
132+
// props will cause it to become a Func.
133+
func (p Props) GetFunc(key string) Func {
134+
return Func(p.Get(key).(func(...any) *js.Object))
120135
}
121136

122137
// CreateElement creates a React element of the given type with the given props and children.

playground/internal/react/codeBox.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ func CodeBox(code string, setCode Setter, onSave Setter, onEscape Setter) *Eleme
2121

2222
func codeBoxComponent(props Props) *Element {
2323
var (
24-
curCode = As[string](props, `curCode`)
25-
setCode = AsFunc(props, `setCode`)
26-
onSave = AsFunc(props, `onSave`)
27-
onEscape = AsFunc(props, `onEscape`)
24+
curCode = props.GetString(`curCode`)
25+
setCode = props.GetFunc(`setCode`)
26+
onSave = props.GetFunc(`onSave`)
27+
onEscape = props.GetFunc(`onEscape`)
2828
textAreaRef = UseRef()
2929
lineNumsRef = UseRef()
3030
)

playground/internal/react/outputBox.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func OutputBox(output []any) *Element {
5858

5959
func outputBoxComponent(props Props) *Element {
6060
var (
61-
output = As[[]any](props, `output`)
61+
output = props.Get(`output`).([]any)
6262
outputBoxRef = UseRef()
6363
)
6464

@@ -112,9 +112,9 @@ func outputLine(index int, isError bool, content string) *Element {
112112

113113
func outputLineComponent(props Props) *Element {
114114
var (
115-
index = int(As[float64](props, `index`))
116-
isError = As[bool](props, `isError`)
117-
content = As[string](props, `content`)
115+
index = props.GetInt(`index`)
116+
isError = props.GetBool(`isError`)
117+
content = props.GetString(`content`)
118118
)
119119

120120
classType := `output-text`

playground/internal/react/shareUrlControl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ func ShareUrlControl(shareUrl string, onShare Setter) *Element {
1111

1212
func shareUrlComponent(props Props) *Element {
1313
var (
14-
shareUrl = As[string](props, `shareUrl`)
15-
onShare = AsFunc(props, `onShare`)
14+
shareUrl = props.GetString(`shareUrl`)
15+
onShare = props.GetFunc(`onShare`)
1616
shareUrlRef = UseRef()
1717
)
1818

playground/internal/react/toggleBox.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ func ToggleBox(id, title, label string, checked bool, setChecked Setter) *Elemen
1414

1515
func toggleBoxComponent(props Props) *Element {
1616
var (
17-
id = As[string](props, `id`)
18-
title = As[string](props, `title`)
19-
label = As[string](props, `label`)
20-
checked = As[bool](props, `checked`)
21-
setChecked = AsFunc(props, `setChecked`)
17+
id = props.GetString(`id`)
18+
title = props.GetString(`title`)
19+
label = props.GetString(`label`)
20+
checked = props.GetBool(`checked`)
21+
setChecked = props.GetFunc(`setChecked`)
2222
)
2323

2424
onChange := UseCallback(func(e *js.Object) {

0 commit comments

Comments
 (0)