htmgo/htmgo-site/partials/snippets/form.go

77 lines
1.8 KiB
Go
Raw Permalink Normal View History

2024-10-28 15:32:50 +00:00
package snippets
2024-09-29 13:21:58 +00:00
import (
"github.com/maddalax/htmgo/framework/h"
2024-09-29 13:47:39 +00:00
"github.com/maddalax/htmgo/framework/hx"
2024-09-29 13:21:58 +00:00
"github.com/maddalax/htmgo/framework/js"
2024-10-28 15:32:50 +00:00
"time"
2024-09-29 13:21:58 +00:00
)
2024-10-28 15:32:50 +00:00
func FormExample(ctx *h.RequestContext) *h.Partial {
return h.NewPartial(
h.Form(
h.TriggerChildren(),
h.PostPartial(SubmitForm),
h.Class("flex flex-col gap-2 max-w-[300px] mx-auto"),
h.LabelFor("name", "Your Name"),
h.Input(
"text",
h.Required(),
h.Class("p-4 rounded-md border border-slate-200"),
h.Name("name"),
h.Placeholder("Name"),
h.OnEvent(
hx.KeyDownEvent,
js.SubmitFormOnEnter(),
2024-09-29 13:47:39 +00:00
),
2024-09-29 13:21:58 +00:00
),
2024-10-28 15:32:50 +00:00
SubmitButton(),
2024-09-29 13:21:58 +00:00
),
2024-10-28 15:32:50 +00:00
)
2024-09-29 13:21:58 +00:00
}
func SubmitButton() *h.Element {
buttonClasses := "rounded items-center px-3 py-2 bg-slate-800 text-white w-full text-center"
return h.Div(
h.HxBeforeRequest(
js.RemoveClassOnChildren(".loading", "hidden"),
js.SetClassOnChildren(".submit", "hidden"),
),
h.HxAfterRequest(
js.SetClassOnChildren(".loading", "hidden"),
js.RemoveClassOnChildren(".submit", "hidden"),
),
h.Class("flex gap-2 justify-center"),
h.Button(
h.Class("loading hidden relative text-center", buttonClasses),
Spinner(),
2024-09-29 13:28:15 +00:00
h.Disabled(),
2024-09-29 13:21:58 +00:00
h.Text("Submitting..."),
),
h.Button(
2024-09-29 13:47:39 +00:00
h.Type("submit"),
2024-09-29 13:21:58 +00:00
h.Class("submit", buttonClasses),
h.Text("Submit"),
),
)
}
2024-09-29 14:45:17 +00:00
func Spinner(children ...h.Ren) *h.Element {
2024-09-29 13:21:58 +00:00
return h.Div(
2024-09-29 14:45:17 +00:00
h.Children(children...),
2024-10-28 15:32:50 +00:00
h.Class("absolute left-1 spinner spinner-border animate-spin "+
"inline-block w-6 h-6 border-4 rounded-full border-slate-200 border-t-transparent"),
2024-09-29 13:21:58 +00:00
h.Attribute("role", "status"),
)
}
2024-10-28 15:32:50 +00:00
func SubmitForm(ctx *h.RequestContext) *h.Partial {
name := ctx.FormValue("name")
time.Sleep(time.Second * 2)
return h.NewPartial(
h.Div(
h.TextF("Form submitted with name: %s", name),
),
)
}