95 lines
2.3 KiB
Plaintext
95 lines
2.3 KiB
Plaintext
|
package component
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"forge.cadoles.com/wpetit/clearcase/internal/http/form"
|
||
|
)
|
||
|
|
||
|
templ FormField(form *form.Form, id string, name string, label string) {
|
||
|
{{ field := form.Field(name) }}
|
||
|
if field != nil {
|
||
|
<div class="field">
|
||
|
{{ err, hasErr := form.Error(name) }}
|
||
|
<label class={ "label", templ.KV("has-text-danger", hasErr) } for={ id }>{ label }</label>
|
||
|
<div class="control">
|
||
|
<input class={ "input", templ.KV("is-danger", hasErr) } id={ id } name={ field.Name() } { field.Attrs()... }/>
|
||
|
if hasErr {
|
||
|
<p class="help is-danger">{ err.Message() }</p>
|
||
|
}
|
||
|
</div>
|
||
|
</div>
|
||
|
}
|
||
|
}
|
||
|
|
||
|
templ FormTextarea(form *form.Form, id string, name string, label string) {
|
||
|
{{ field := form.Field(name) }}
|
||
|
if field != nil {
|
||
|
<div class="field">
|
||
|
<label class="label" for={ id }>{ label }</label>
|
||
|
<div class="control">
|
||
|
{{ err, hasErr := form.Error(name) }}
|
||
|
{{ value, hasValue := field.Get("value") }}
|
||
|
<textarea class={ "textarea", templ.KV("is-danger", hasErr) } id={ id } name={ field.Name() } { field.Attrs()... }>
|
||
|
if hasValue {
|
||
|
{ value.(string) }
|
||
|
}
|
||
|
</textarea>
|
||
|
if hasErr {
|
||
|
<p class="help is-danger">{ err.Message() }</p>
|
||
|
}
|
||
|
</div>
|
||
|
</div>
|
||
|
}
|
||
|
}
|
||
|
|
||
|
templ FormSelect(form *form.Form, id string, name string, label string, kvOptions ...string) {
|
||
|
{{ field := form.Field(name) }}
|
||
|
if field != nil {
|
||
|
<div class="field">
|
||
|
<label class="label" for={ id }>{ label }</label>
|
||
|
<div class="control">
|
||
|
{{ err, hasErr := form.Error(name) }}
|
||
|
<div class="select is-fullwidth">
|
||
|
<select id={ id } name={ field.Name() } { field.Attrs()... }>
|
||
|
{{ options := keyValuesToOptions(kvOptions) }}
|
||
|
for _, o := range options {
|
||
|
<option value={ o.Value }>{ o.Label }</option>
|
||
|
}
|
||
|
</select>
|
||
|
</div>
|
||
|
if hasErr {
|
||
|
<p class="help is-danger">{ err.Message() }</p>
|
||
|
}
|
||
|
</div>
|
||
|
</div>
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type SelectOption struct {
|
||
|
Value string
|
||
|
Label string
|
||
|
}
|
||
|
|
||
|
func keyValuesToOptions(kv []string) []SelectOption {
|
||
|
if len(kv)%2 != 0 {
|
||
|
panic(errors.New("expected pair number of key/values"))
|
||
|
}
|
||
|
|
||
|
options := make([]SelectOption, 0)
|
||
|
|
||
|
var key string
|
||
|
for idx := range kv {
|
||
|
if idx%2 == 0 {
|
||
|
key = kv[idx]
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
options = append(options, SelectOption{
|
||
|
Value: kv[idx],
|
||
|
Label: key,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return options
|
||
|
}
|