-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): add a form system (Experimental)
- Loading branch information
Showing
17 changed files
with
754 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
[Test_Form_Rendering - 1] | ||
<form action="POST" action="" encoding=""> | ||
<label class="" style="" for="name">name</label> | ||
<input name="name" type="text" value="John Doe" id="name" placeholder="Enter the name" required readonly autofocus size="10" maxlength="100" minlength="10" max="100" min="10" step="10" pattern="^[a-z]+$" autocomplete="on"> | ||
<label class="" style="" for="email">email</label> | ||
<input name="email" type="email" value="john.doe@gmail.com" id="email"> | ||
</form> | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package form |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package form | ||
|
||
import ( | ||
"github.com/flosch/pongo2" | ||
"github.com/rande/goapp" | ||
"github.com/rande/gonode/core/config" | ||
"github.com/rande/gonode/core/embed" | ||
) | ||
|
||
func Configure(l *goapp.Lifecycle, conf *config.Config) { | ||
|
||
l.Prepare(func(app *goapp.App) error { | ||
app.Get("gonode.embeds").(*embed.Embeds).Add("form", GetEmbedFS()) | ||
|
||
pongo := app.Get("gonode.pongo").(*pongo2.TemplateSet) | ||
|
||
pongo.Globals["form_field"] = createPongoField(pongo) | ||
pongo.Globals["form_label"] = createPongoLabel(pongo) | ||
pongo.Globals["form_input"] = createPongoInput(pongo) | ||
|
||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package form | ||
|
||
import ( | ||
"embed" | ||
) | ||
|
||
//go:embed all:templates | ||
var content embed.FS | ||
|
||
func GetEmbedFS() embed.FS { | ||
return content | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package form | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/flosch/pongo2" | ||
"github.com/rande/gonode/core/helper" | ||
) | ||
|
||
type AttributOption struct { | ||
Name string | ||
Value interface{} | ||
} | ||
|
||
func createPongoField(pongo *pongo2.TemplateSet) func(field *FormField, form *Form) *pongo2.Value { | ||
|
||
return func(field *FormField, form *Form) *pongo2.Value { | ||
tpl, err := pongo.FromFile("form:field.tpl") | ||
|
||
helper.PanicOnError(err) | ||
|
||
data, err := tpl.Execute(pongo2.Context{ | ||
"form": form, | ||
"field": field, | ||
"input": field.Input, | ||
"label": field.Label, | ||
}) | ||
|
||
helper.PanicOnError(err) | ||
|
||
return pongo2.AsSafeValue(data) | ||
} | ||
} | ||
|
||
func createPongoLabel(pongo *pongo2.TemplateSet) func(field *FormField, form *Form) *pongo2.Value { | ||
|
||
return func(field *FormField, form *Form) *pongo2.Value { | ||
|
||
tpl, err := pongo.FromFile(field.Label.Template) | ||
|
||
helper.PanicOnError(err) | ||
|
||
data, err := tpl.Execute(pongo2.Context{ | ||
"form": form, | ||
"field": field, | ||
"input": field.Input, | ||
"label": field.Label, | ||
}) | ||
|
||
helper.PanicOnError(err) | ||
|
||
return pongo2.AsSafeValue(data) | ||
} | ||
} | ||
|
||
func createPongoInput(pongo *pongo2.TemplateSet) func(field *FormField, form *Form) *pongo2.Value { | ||
|
||
return func(field *FormField, form *Form) *pongo2.Value { | ||
|
||
tpl, err := pongo.FromFile(fmt.Sprintf("%s:fields/input.%s.tpl", field.Module, field.Input.Type)) | ||
|
||
helper.PanicOnError(err) | ||
|
||
data, err := tpl.Execute(pongo2.Context{ | ||
"form": form, | ||
"field": field, | ||
"input": field.Input, | ||
"label": field.Label, | ||
}) | ||
|
||
helper.PanicOnError(err) | ||
|
||
return pongo2.AsSafeValue(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package form | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/flosch/pongo2" | ||
"github.com/gkampitakis/go-snaps/snaps" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/rande/gonode/core/embed" | ||
) | ||
|
||
func TestMain(t *testing.M) { | ||
|
||
fmt.Println("Setup") | ||
v := t.Run() | ||
|
||
// After all tests have run `go-snaps` can check for not used snapshots | ||
snaps.Clean(t) | ||
|
||
os.Exit(v) | ||
} | ||
|
||
func GetPongo() *pongo2.TemplateSet { | ||
embeds := embed.NewEmbeds() | ||
embeds.Add("form", GetEmbedFS()) | ||
|
||
pongo := pongo2.NewSet("gonode.embeds", &embed.PongoTemplateLoader{ | ||
Embeds: embeds, | ||
BasePath: "", | ||
}) | ||
pongo.Options = &pongo2.Options{ | ||
TrimBlocks: true, | ||
LStripBlocks: true, | ||
} | ||
|
||
pongo.Globals["form_field"] = createPongoField(pongo) | ||
pongo.Globals["form_label"] = createPongoLabel(pongo) | ||
pongo.Globals["form_input"] = createPongoInput(pongo) | ||
|
||
return pongo | ||
} | ||
func Test_Form_Rendering(t *testing.T) { | ||
|
||
form := &Form{} | ||
form.Add("name", "text", "John Doe") | ||
form.Add("email", "email", "john.doe@gmail.com") | ||
|
||
PrepareForm(form) | ||
|
||
assert.False(t, form.HasErrors) | ||
|
||
pongo := GetPongo() | ||
template, err := pongo.FromFile("form:form.tpl") | ||
|
||
assert.Equal(t, "John Doe", form.Get("name").InitialValue) | ||
assert.Equal(t, "John Doe", form.Get("name").Input.Value) | ||
|
||
form.Get("name").Input.Pattern = "^[a-z]+$" | ||
form.Get("name").Input.Placeholder = "Enter the name" | ||
form.Get("name").Input.Placeholder = "Enter the name" | ||
form.Get("name").Input.Readonly = true | ||
form.Get("name").Input.Required = true | ||
form.Get("name").Input.Size = 10 | ||
form.Get("name").Input.Autofocus = true | ||
form.Get("name").Input.Autocomplete = "on" | ||
form.Get("name").Input.Min = 10 | ||
form.Get("name").Input.Max = 100 | ||
form.Get("name").Input.Step = 10 | ||
|
||
form.Get("name").Input.MinLength = 10 | ||
form.Get("name").Input.MaxLength = 100 | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, template) | ||
|
||
html, err := template.Execute(pongo2.Context{ | ||
"form": form, | ||
}) | ||
|
||
// fmt.Printf("%s", err.Error()) | ||
assert.Nil(t, err) | ||
|
||
snaps.MatchSnapshot(t, html) | ||
} |
Oops, something went wrong.