-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.templ
72 lines (64 loc) · 1.3 KB
/
form.templ
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package gong
import "net/http"
templ Form(opts ...FormOption) {
{{
config := formConfig{
method: http.MethodPost,
swap: SwapNone,
}
for _, opt := range opts {
config = opt(config)
}
path := getContext(ctx).request.RequestURI
}}
<form
if config.method == http.MethodPost {
hx-post={ path }
}
if config.method == http.MethodPatch {
hx-patch={ path }
}
if config.method == http.MethodDelete {
hx-delete={ path }
}
hx-swap={ config.swap }
if config.swap != SwapNone {
hx-target={ "#" + buildComponentID(ctx, config.id) }
}
hx-headers={ buildHeaders(ctx, GongRequestTypeAction) }
class={ config.cssClass }
>
{ children... }
</form>
}
type formConfig struct {
method string
id string
cssClass templ.CSSClass
swap string
}
type FormOption func(c formConfig) formConfig
func FormWithMethod(method string) FormOption {
return func(c formConfig) formConfig {
c.method = method
return c
}
}
func FormWithCSSClass(cssClass templ.CSSClass) FormOption {
return func(c formConfig) formConfig {
c.cssClass = cssClass
return c
}
}
func FormWithID(id string) FormOption {
return func(c formConfig) formConfig {
c.id = id
return c
}
}
func FormWithSwap(swap string) FormOption {
return func(c formConfig) formConfig {
c.swap = swap
return c
}
}