-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.go
58 lines (49 loc) · 1.03 KB
/
component.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package gong
import (
"context"
"io"
"github.com/a-h/templ"
)
type component struct {
kind string
view View
action bool
config componentConfig
}
func Component(kind string, view View, opts ...ComponentOption) templ.Component {
c := component{
kind: kind,
view: view,
}
if loader, ok := view.(Loader); ok {
c.config.loader = loader
}
for _, opt := range opts {
c.config = opt(c.config)
}
return c
}
func (c component) Render(ctx context.Context, w io.Writer) error {
gCtx := getContext(ctx)
gCtx.action = c.action
gCtx.loader = c.config.loader
if gCtx.kind == "" {
gCtx.kind = c.kind
} else {
gCtx.kind += "_" + c.kind
}
ctx = context.WithValue(ctx, contextKey, gCtx)
return c.view.View().Render(ctx, w)
}
type componentConfig struct {
loader Loader
}
type ComponentOption func(c componentConfig) componentConfig
func ComponentWithLoaderData(data any) ComponentOption {
return func(c componentConfig) componentConfig {
c.loader = LoaderFunc(func(ctx context.Context) any {
return data
})
return c
}
}