diff --git a/examples/todo-list/partials/task/task.go b/examples/todo-list/partials/task/task.go index e7e19b4..2fa37ab 100644 --- a/examples/todo-list/partials/task/task.go +++ b/examples/todo-list/partials/task/task.go @@ -51,11 +51,12 @@ func Input(list []*ent.Task) *h.Element { h.Class("border border-b-slate-100 relative"), h.Input( "text", - h.Attribute("required", "true"), - h.Attribute("maxlength", "150"), - h.Attribute("autocomplete", "off"), - h.Attribute("autofocus", "true"), - h.Attribute("name", "name"), + h.Required(), + h.Disabled(), + h.MaxLength(150), + h.AutoComplete("off"), + h.AutoFocus(), + h.Name("name"), h.Class("pl-12 text-xl p-4 w-full outline-none focus:outline-2 focus:outline-rose-400"), h.Placeholder("What needs to be done?"), h.Post(h.GetPartialPath(Create)), @@ -145,8 +146,8 @@ func Task(task *ent.Task, editing bool) *h.Element { h.Form( h.Class("h-full"), h.Input("text", - h.Attribute("name", "task"), - h.Attribute("value", task.ID.String()), + h.Name("task"), + h.Value(task.ID.String()), h.Class("hidden"), ), h.Input( diff --git a/framework/h/attribute.go b/framework/h/attribute.go index 2d6f2ec..92e3907 100644 --- a/framework/h/attribute.go +++ b/framework/h/attribute.go @@ -58,7 +58,7 @@ func AttributePairs(pairs ...string) *AttributeMap { } func Checked() Ren { - return Attribute("checked", "true") + return Attribute("checked", "") } func Id(value string) Ren { @@ -68,7 +68,7 @@ func Id(value string) Ren { return Attribute("id", value) } -func Disabled() Ren { +func Disabled() *AttributeR { return Attribute("disabled", "") } @@ -182,3 +182,115 @@ func Boost() Ren { func IfQueryParam(key string, node *Element) Ren { return Fragment(Attribute("hx-if-qp:"+key, "true"), node) } + +func ReadOnly() *AttributeR { + return Attribute("readonly", "") +} + +func Required() *AttributeR { + return Attribute("required", "") +} + +func Multiple() *AttributeR { + return Attribute("multiple", "") +} + +func Selected() *AttributeR { + return Attribute("selected", "") +} + +func MaxLength(value int) *AttributeR { + return Attribute("maxlength", fmt.Sprintf("%d", value)) +} + +func MinLength(value int) *AttributeR { + return Attribute("minlength", fmt.Sprintf("%d", value)) +} + +func Size(value int) *AttributeR { + return Attribute("size", fmt.Sprintf("%d", value)) +} + +func Width(value int) *AttributeR { + return Attribute("width", fmt.Sprintf("%d", value)) +} + +func Height(value int) *AttributeR { + return Attribute("height", fmt.Sprintf("%d", value)) +} + +func Download(value bool) *AttributeR { + return Attribute("download", fmt.Sprintf("%t", value)) +} + +func Rel(value string) *AttributeR { + return Attribute("rel", value) +} + +func Pattern(value string) *AttributeR { + return Attribute("pattern", value) +} + +func Action(value string) *AttributeR { + return Attribute("action", value) +} + +func Method(value string) *AttributeR { + return Attribute("method", value) +} + +func Enctype(value string) *AttributeR { + return Attribute("enctype", value) +} + +func AutoComplete(value string) *AttributeR { + return Attribute("autocomplete", value) +} + +func AutoFocus() *AttributeR { + return Attribute("autofocus", "") +} + +func NoValidate() *AttributeR { + return Attribute("novalidate", "") +} + +func Step(value string) *AttributeR { + return Attribute("step", value) +} + +func Max(value string) *AttributeR { + return Attribute("max", value) +} + +func Min(value string) *AttributeR { + return Attribute("min", value) +} + +func Cols(value int) *AttributeR { + return Attribute("cols", fmt.Sprintf("%d", value)) +} + +func Rows(value int) *AttributeR { + return Attribute("rows", fmt.Sprintf("%d", value)) +} + +func Wrap(value string) *AttributeR { + return Attribute("wrap", value) +} + +func Role(value string) *AttributeR { + return Attribute("role", value) +} + +func AriaLabel(value string) *AttributeR { + return Attribute("aria-label", value) +} + +func AriaHidden(value bool) *AttributeR { + return Attribute("aria-hidden", fmt.Sprintf("%t", value)) +} + +func TabIndex(value int) *AttributeR { + return Attribute("tabindex", fmt.Sprintf("%d", value)) +} diff --git a/framework/h/renderer.go b/framework/h/renderer.go index 3122da1..426c6cc 100644 --- a/framework/h/renderer.go +++ b/framework/h/renderer.go @@ -3,7 +3,7 @@ package h import ( "fmt" "github.com/maddalax/htmgo/framework/hx" - "golang.org/x/net/html" + "html" "html/template" "strings" ) @@ -161,10 +161,14 @@ func renderScripts(context *RenderContext) { func (a *AttributeR) Render(context *RenderContext) { context.builder.WriteString(a.Name) - context.builder.WriteString(`=`) - context.builder.WriteString(`"`) - context.builder.WriteString(html.EscapeString(a.Value)) - context.builder.WriteString(`"`) + if a.Value != "" { + context.builder.WriteString(`=`) + context.builder.WriteString(`"`) + context.builder.WriteString(html.EscapeString(a.Value)) + context.builder.WriteString(`"`) + } else { + context.builder.WriteString(" ") + } } func (t *TextContent) Render(context *RenderContext) {