feat(example): add theme picker
This commit is contained in:
parent
7373b19212
commit
f70f0a1f9d
4 changed files with 69 additions and 15 deletions
25
example.go
25
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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@
|
|||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Form with Bootstrap</title>
|
||||
<title>Demo of go-form with Bootstrap</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
|
||||
<style>
|
||||
fieldset {
|
||||
border: 1px solid #999;
|
||||
border-radius: var(--bs-border-radius);
|
||||
border: var(--bs-border-width) solid var(--bs-border-color);
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
|
|
@ -21,7 +22,12 @@
|
|||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="container pt-2">
|
||||
{{ form_widget (.styleForm.GetField "value") }}
|
||||
<hr>
|
||||
|
||||
<h1>Demo of go-form with Bootstrap</h1>
|
||||
|
||||
<details>
|
||||
<summary>Debug view</summary>
|
||||
<div class="py-2">
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Form HTML5 (with Pico)</title>
|
||||
<title>Demo of go-form (pure HTML5 and Pico)</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.colors.min.css">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
|
@ -41,7 +41,12 @@
|
|||
}
|
||||
</style>
|
||||
</head>
|
||||
<body class="container">
|
||||
<body class="container p10">
|
||||
{{ form_widget (.styleForm.GetField "value") }}
|
||||
<hr>
|
||||
|
||||
<h1>Demo of go-form (pure HTML5 and Pico)</h1>
|
||||
|
||||
<details class="p10">
|
||||
<summary>Debug view</summary>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue