htmgo/framework-ui/ui/input.go

43 lines
990 B
Go
Raw Normal View History

2024-09-11 18:09:55 +00:00
package ui
2024-09-13 17:28:32 +00:00
import "github.com/maddalax/mhtml/framework/h"
2024-09-11 18:09:55 +00:00
type InputProps struct {
Id string
Label string
Name string
Type string
DefaultValue string
ValidationPath string
Childen []h.Renderable
2024-09-11 18:09:55 +00:00
}
func Input(props InputProps) h.Renderable {
validation := h.If(props.ValidationPath != "", h.Children(
h.Post(props.ValidationPath),
h.Trigger("change"),
h.Attribute("hx-swap", "innerHTML transition:true"),
h.Attribute("hx-target", "next div"),
))
2024-09-11 18:09:55 +00:00
input := h.Input(
props.Type,
h.Class("border p-2 rounded"),
h.If(props.Id != "", h.Id(props.Id)),
h.If(props.Name != "", h.Name(props.Name)),
h.If(props.Childen != nil, h.Children(props.Childen...)),
2024-09-12 02:06:34 +00:00
h.If(props.DefaultValue != "", h.Attribute("value", props.DefaultValue)),
validation,
2024-09-11 18:09:55 +00:00
)
wrapped := h.Div(
h.Class("flex flex-col gap-1"),
h.If(props.Label != "", h.Label(props.Label)),
input,
h.Div(h.Class("text-red-500")),
)
return wrapped
2024-09-11 18:09:55 +00:00
}