2024-09-11 18:09:55 +00:00
|
|
|
package ui
|
|
|
|
|
|
2024-09-17 15:41:29 +00:00
|
|
|
import (
|
|
|
|
|
"github.com/maddalax/htmgo/framework/h"
|
|
|
|
|
)
|
2024-09-11 18:09:55 +00:00
|
|
|
|
|
|
|
|
type InputProps struct {
|
2024-09-12 18:13:15 +00:00
|
|
|
Id string
|
|
|
|
|
Label string
|
|
|
|
|
Name string
|
|
|
|
|
Type string
|
|
|
|
|
DefaultValue string
|
|
|
|
|
ValidationPath string
|
2024-09-20 01:24:44 +00:00
|
|
|
Children []h.Ren
|
2024-09-11 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-20 01:24:44 +00:00
|
|
|
func Input(props InputProps) h.Ren {
|
2024-09-12 18:13:15 +00:00
|
|
|
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)),
|
2024-09-19 03:32:09 +00:00
|
|
|
h.If(props.Children != nil, h.Children(props.Children...)),
|
2024-09-12 02:06:34 +00:00
|
|
|
h.If(props.DefaultValue != "", h.Attribute("value", props.DefaultValue)),
|
2024-09-12 18:13:15 +00:00
|
|
|
validation,
|
2024-09-11 18:09:55 +00:00
|
|
|
)
|
2024-09-12 18:13:15 +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
|
|
|
}
|