htmgo/framework-ui/ui/input.go

51 lines
1.1 KiB
Go
Raw Normal View History

2024-09-11 18:09:55 +00:00
package ui
import (
"github.com/maddalax/htmgo/framework/h"
2024-09-21 03:59:07 +00:00
"github.com/maddalax/htmgo/framework/hx"
)
2024-09-11 18:09:55 +00:00
type InputProps struct {
Id string
Label string
Name string
Type string
DefaultValue string
2024-10-01 01:32:42 +00:00
Placeholder string
Required bool
ValidationPath string
Children []h.Ren
2024-09-11 18:09:55 +00:00
}
func Input(props InputProps) h.Ren {
validation := h.If(props.ValidationPath != "", h.Children(
2024-09-21 03:59:07 +00:00
h.Post(props.ValidationPath, hx.ChangeEvent),
h.Attribute("hx-swap", "innerHTML transition:true"),
h.Attribute("hx-target", "next div"),
))
2024-10-01 01:32:42 +00:00
if props.Type == "" {
props.Type = "text"
}
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.Children != nil, h.Children(props.Children...)),
2024-10-01 01:32:42 +00:00
h.If(props.Required, h.Required()),
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"),
2024-10-01 01:32:42 +00:00
h.If(props.Label != "", h.Label(h.Text(props.Label))),
input,
h.Div(h.Class("text-red-500")),
)
return wrapped
2024-09-11 18:09:55 +00:00
}