summaryrefslogtreecommitdiff
path: root/form/field.templ
blob: e9ce33ecef72e5e78e136c3c40d1cb365ed37ed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package form

import "fmt"

func (f *field) isCheckbox() bool {
    return f.Type == "checkbox"
}

func (f *field) optionAttributes() templ.Attributes {
    attrs := make(map[string]any)
    for _, o := range f.Options {
        attrs[o] = true
    }
    return templ.Attributes(attrs)
}

templ (f *field) item(errors []string) {
    <div class={"mb-3", 
                templ.KV("form-floating", !f.isCheckbox()),
                templ.KV("form-check form-switch", f.isCheckbox())}>
        <input
            if f.ID != "" {id={f.ID}}
            type={f.Type}
            name={f.Name}
            placeholder={f.Placeholder}
            if f.Value != nil {value={fmt.Sprint(f.Value)}}
            if f.isCheckbox() {
                class="form-check-input" 
            } else {
                class="form-control"
            } 
            {f.optionAttributes()...}    
        >
        <label if f.ID != "" {for={f.ID}}
               if f.isCheckbox() {class="form-check-label"}>
               {f.Label}
        </label>
        for _, e := range errors {
            <p style="color:red">{e}</p>
        }
    </div>
}