Skip to content

Commit

Permalink
fix bug with example validation
Browse files Browse the repository at this point in the history
  • Loading branch information
will-wow committed Feb 26, 2024
1 parent e470e59 commit f0f8342
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 45 deletions.
4 changes: 0 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,5 @@ tasks:
tools:
desc: Install tools
cmds:
# Install goimports for formatting
- go install golang.org/x/tools/cmd/goimports@latest
# Install golangci for linting
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
# Install prettier for formatting non-go files
- npm install
7 changes: 7 additions & 0 deletions examples/templ/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ tasks:
generates:
- "**/*_templ.go"

ready:
desc: Format, lint, and test code. If this passes, then the code is ready to be committed.
cmds:
- task: fmt
- task: lint
- task: test

tools:
desc: Install tools
cmds:
Expand Down
10 changes: 9 additions & 1 deletion examples/templ/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ require (
github.com/will-wow/typed-htmx-go v0.0.3
)

require github.com/lithammer/dedent v1.1.0
require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/lithammer/dedent v1.1.0
)

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
golang.org/x/net v0.17.0 // indirect
)
36 changes: 36 additions & 0 deletions examples/templ/go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/a-h/templ v0.2.543 h1:8YyLvyUtf0/IE2nIwZ62Z/m2o2NqwhnMynzOL78Lzbk=
github.com/a-h/templ v0.2.543/go.mod h1:jP908DQCwI08IrnTalhzSEH9WJqG/Q94+EODQcJGFUA=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/will-wow/typed-htmx-go v0.0.3 h1:zF0ESFMm2Ry2OEe+mQ+2zcULM1aEqWaDwdyGEZWC/1w=
github.com/will-wow/typed-htmx-go v0.0.3/go.mod h1:vBV9acu4/cjaeLc1Z2z4ZruJYpF1KdCoFebp3CWNaPA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
14 changes: 7 additions & 7 deletions examples/templ/web/clicktoedit/clicktoedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type form struct {
Email string
}

func (f *form) Validate() (ok bool) {
func (f *form) validate() (ok bool) {
if f.FirstName == "" {
f.SetRequiredError("FirstName")
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func Handler() http.Handler {
func demo(w http.ResponseWriter, r *http.Request) {
form := newForm()

component := Page(form)
component := page(form)

w.WriteHeader(http.StatusOK)
_ = component.Render(r.Context(), w)
Expand All @@ -62,7 +62,7 @@ func demo(w http.ResponseWriter, r *http.Request) {
func view(w http.ResponseWriter, r *http.Request) {
form := newForm()

component := ViewForm(form)
component := viewForm(form)
w.WriteHeader(http.StatusOK)
_ = component.Render(r.Context(), w)
}
Expand All @@ -76,7 +76,7 @@ func edit(w http.ResponseWriter, r *http.Request) {

form := newForm()

component := EditForm(form)
component := editForm(form)
w.WriteHeader(http.StatusOK)
_ = component.Render(r.Context(), w)
}
Expand All @@ -94,16 +94,16 @@ func post(w http.ResponseWriter, r *http.Request) {
Email: r.Form.Get("email"),
Form: ui.NewForm(),
}
ok := form.Validate()
ok := form.validate()

if !ok {
component := EditForm(form)
component := editForm(form)
w.WriteHeader(http.StatusUnprocessableEntity)
_ = component.Render(r.Context(), w)
return
}

component := ViewForm(form)
component := viewForm(form)
w.WriteHeader(http.StatusOK)
_ = component.Render(r.Context(), w)
}
24 changes: 15 additions & 9 deletions examples/templ/web/clicktoedit/clicktoedit.templ
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/will-wow/typed-htmx-go/hx/swap"
)

templ Page(form *form) {
templ page(form *form) {
@layout.Base("Click to edit") {
<h1>Click To Edit</h1>
<p>
Expand All @@ -22,7 +22,7 @@ templ Page(form *form) {
</ul>
<pre>
{ dedent.Dedent(`
templ ViewForm(form *form) {
templ viewForm(form *form) {
<div { hx.New().Target(hx.TargetThis).Swap(swap.OuterHTML).Build()... }>
<dl>
<dt>First Name</dt>
Expand All @@ -48,7 +48,7 @@ templ Page(form *form) {
</ul>
<pre>
{ dedent.Dedent(`
templ EditForm(form *form) {
templ editForm(form *form) {
<form
method="POST"
action="/examples/click-to-edit/edit"
Expand Down Expand Up @@ -123,11 +123,11 @@ templ Page(form *form) {
</li>
</ul>
<h2>Demo</h2>
@ViewForm(form)
@viewForm(form)
}
}

templ ViewForm(form *form) {
templ viewForm(form *form) {
<div { hx.New().Target(hx.TargetThis).Swap(swap.OuterHTML).Build()... }>
<dl>
<dt>First Name</dt>
Expand All @@ -145,7 +145,7 @@ templ ViewForm(form *form) {
</div>
}

templ EditForm(form *form) {
templ editForm(form *form) {
<form
method="POST"
action="/examples/click-to-edit/edit"
Expand All @@ -161,7 +161,9 @@ templ EditForm(form *form) {
type="text"
name="firstName"
value={ form.FirstName }
aria-invalid={ form.AriaInvalid("LastName") }
if form.HasError("FirstName") {
aria-invalid="true"
}
/>
if form.HasError("FirstName") {
<small>
Expand All @@ -175,7 +177,9 @@ templ EditForm(form *form) {
type="text"
name="lastName"
value={ form.LastName }
aria-invalid={ form.AriaInvalid("LastName") }
if form.HasError("LastName") {
aria-invalid="true"
}
/>
if form.HasError("LastName") {
<small>
Expand All @@ -189,7 +193,9 @@ templ EditForm(form *form) {
type="text"
name="email"
value={ form.Email }
aria-invalid={ form.AriaInvalid("LastName") }
if form.HasError("Email") {
aria-invalid="true"
}
/>
if form.HasError("Email") {
<small>
Expand Down
54 changes: 30 additions & 24 deletions examples/templ/web/clicktoedit/clicktoedit_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f0f8342

Please sign in to comment.