feat: initial commit

This commit is contained in:
2025-02-22 09:42:15 +01:00
parent ee4a65b345
commit e6e5c9b04d
43 changed files with 1191 additions and 247 deletions

View File

@ -42,18 +42,99 @@ templ FormTextarea(form *form.Form, id string, name string, label string) {
}
}
templ FormSelect(form *form.Form, id string, name string, label string, kvOptions ...string) {
type FormSelectOptions struct {
Options []formSelectOption
Attrs map[string]any
}
type FormSelectOptionFunc func(opts *FormSelectOptions)
func WithOptions(kvOptions ...string) FormSelectOptionFunc {
return func(opts *FormSelectOptions) {
opts.Options = keyValuesToOptions(kvOptions)
}
}
func WithAttrs(kvAttrs ...any) FormSelectOptionFunc {
return func(opts *FormSelectOptions) {
opts.Attrs = keyValuesToAttrs(kvAttrs)
}
}
func keyValuesToAttrs(kv []any) map[string]any {
if len(kv)%2 != 0 {
panic(errors.New("expected pair number of key/values"))
}
attrs := make(map[string]any, 0)
var key string
for idx := range kv {
if idx%2 == 0 {
key = kv[idx].(string)
continue
}
attrs[key] = kv[idx]
}
return attrs
}
type formSelectOption struct {
Value string
Label string
}
func keyValuesToOptions(kv []string) []formSelectOption {
if len(kv)%2 != 0 {
panic(errors.New("expected pair number of key/values"))
}
options := make([]formSelectOption, 0)
var key string
for idx := range kv {
if idx%2 == 0 {
key = kv[idx]
continue
}
options = append(options, formSelectOption{
Value: kv[idx],
Label: key,
})
}
return options
}
func newFormSelectOptions(funcs ...FormSelectOptionFunc) *FormSelectOptions {
opts := &FormSelectOptions{}
for _, fn := range funcs {
fn(opts)
}
return opts
}
templ FormSelect(form *form.Form, id string, name string, label string, funcs ...FormSelectOptionFunc) {
{{ opts := newFormSelectOptions(funcs...) }}
{{ 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") }}
<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 id={ id } name={ field.Name() } { mergeAttrs(field.Attrs(), opts.Attrs)... }>
for _, o := range opts.Options {
<option
if hasValue && value == o.Value {
selected
}
value={ o.Value }
>{ o.Label }</option>
}
</select>
</div>
@ -65,30 +146,12 @@ templ FormSelect(form *form.Form, id string, name string, label string, kvOption
}
}
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
func mergeAttrs(attrs ...map[string]any) map[string]any {
merged := make(form.Attrs)
for _, a := range attrs {
for k, v := range a {
merged[k] = v
}
options = append(options, SelectOption{
Value: kv[idx],
Label: key,
})
}
return options
return merged
}