diff --git a/example.go b/example.go index 2aa6727..91918d4 100644 --- a/example.go +++ b/example.go @@ -17,21 +17,24 @@ var templates embed.FS func handler(view, action string, formRenderer *theme.Renderer, w http.ResponseWriter, r *http.Request) { entity := example.ExampleData{} - form := example.CreateDataForm(action) + entityForm := example.CreateDataForm(action) + entityForm.Mount(entity) - form.Mount(entity) + style := example.NewTheme(action) + styleForm := example.CreateThemeSelectorForm() + styleForm.Mount(style) - if r.Method == form.Method { - form.HandleRequest(r) + if r.Method == entityForm.Method { + entityForm.HandleRequest(r) - if form.IsSubmitted() && form.IsValid() { - form.Bind(&entity) + if entityForm.IsSubmitted() && entityForm.IsValid() { + entityForm.Bind(&entity) } } content, _ := templates.ReadFile(view) - formAsJson, _ := json.MarshalIndent(form, " ", " ") + formAsJson, _ := json.MarshalIndent(entityForm, " ", " ") tpl, _ := template.New("page"). Funcs(formRenderer.FuncMap()). @@ -43,9 +46,10 @@ func handler(view, action string, formRenderer *theme.Renderer, w http.ResponseW dump.Theme = godump.Theme{} tpl.Execute(w, map[string]any{ - "isSubmitted": form.IsSubmitted(), - "isValid": form.IsValid(), - "form": form, + "isSubmitted": entityForm.IsSubmitted(), + "isValid": entityForm.IsValid(), + "form": entityForm, + "styleForm": styleForm, "json": string(formAsJson), "dump": template.HTML(dump.Sprint(entity)), }) @@ -72,5 +76,6 @@ func main() { ) }) + log.Println("Browse: http://localhost:1122") log.Fatal(http.ListenAndServe(":1122", nil)) } diff --git a/example/form.go b/example/form.go index 8c516ba..fc6e8bf 100644 --- a/example/form.go +++ b/example/form.go @@ -50,6 +50,10 @@ type CollectionItem struct { ValueB string } +type Theme struct { + Value string `field:"lowerCamel"` +} + func CreateDataForm(action string) *form.Form { items := []Item{ Item{Id: 1, Name: "Item 1"}, @@ -211,3 +215,37 @@ func CreateDataForm(action string) *form.Form { WithMethod(http.MethodPost). WithAction(action) } + +func NewTheme(value string) *Theme { + return &Theme{Value: value} +} + +func CreateThemeSelectorForm() *form.Form { + choices := form.NewChoices([]map[string]string{ + map[string]string{"value": "/", "label": "Html5"}, + map[string]string{"value": "/bootstrap", "label": "Bootstrap5"}, + }) + + choices.LabelBuilder = func(key int, item any) string { + return item.(map[string]string)["label"] + } + + choices.ValueBuilder = func(key int, item any) string { + return item.(map[string]string)["value"] + } + + return form.NewForm( + form.NewFieldChoice("value"). + WithOptions( + form.NewOption("choices", choices), + form.NewOption("label", "Select a theme"), + form.NewOption("required", true), + form.NewOption("attr", form.Attrs{ + "onchange": "document.location.href = this.value", + }), + ), + ). + End(). + WithName(""). + WithMethod(http.MethodGet) +} diff --git a/example/view/bootstrap.html b/example/view/bootstrap.html index 7b848f1..23543fd 100644 --- a/example/view/bootstrap.html +++ b/example/view/bootstrap.html @@ -2,11 +2,12 @@
-