Added enum field support in the ui.

This commit is contained in:
mikestefanello 2025-04-14 20:34:31 -04:00
parent aacb3d358f
commit 53113101c9
4 changed files with 58 additions and 12 deletions

View file

@ -129,7 +129,7 @@
{{- else if $f.Nillable }} {{- else if $f.Nillable }}
op.SetNillable{{ fieldName $f.Name }}(payload.{{ fieldName $f.Name }}) op.SetNillable{{ fieldName $f.Name }}(payload.{{ fieldName $f.Name }})
if payload.{{ fieldName $f.Name }} != nil { if payload.{{ fieldName $f.Name }} != nil {
op.Clear{{ fieldName $f.Name }}() // TODO this is not available op.Clear{{ fieldName $f.Name }}()
} }
{{- else if (fieldIsPointer $f) }} {{- else if (fieldIsPointer $f) }}
if payload.{{ fieldName $f.Name }} == nil { if payload.{{ fieldName $f.Name }} == nil {

View file

@ -19,16 +19,16 @@ type (
Help string Help string
} }
RadiosParams struct { OptionsParams struct {
Form form.Form Form form.Form
FormField string FormField string
Name string Name string
Label string Label string
Value string Value string
Options []Radio Options []Choice
} }
Radio struct { Choice struct {
Value string Value string
Label string Label string
} }
@ -88,7 +88,7 @@ func TextareaField(el TextareaFieldParams) Node {
) )
} }
func Radios(el RadiosParams) Node { func Radios(el OptionsParams) Node {
buttons := make(Group, len(el.Options)) buttons := make(Group, len(el.Options))
for i, opt := range el.Options { for i, opt := range el.Options {
buttons[i] = Label( buttons[i] = Label(
@ -114,6 +114,30 @@ func Radios(el RadiosParams) Node {
) )
} }
func SelectList(el OptionsParams) Node {
buttons := make(Group, len(el.Options))
for i, opt := range el.Options {
buttons[i] = Option(
Text(opt.Label),
Value(opt.Value),
If(opt.Value == el.Value, Attr("selected")),
)
}
return Div(
Class("control field"),
Label(Class("label"), Text(el.Label)),
Div(
Class("select"),
Select(
Name(el.Name),
buttons,
),
),
formFieldErrors(el.Form, el.FormField),
)
}
func Checkbox(el CheckboxParams) Node { func Checkbox(el CheckboxParams) Node {
return Div( return Div(
Class("field"), Class("field"),

View file

@ -31,13 +31,13 @@ func (f *Contact) Render(r *ui.Request) Node {
Label: "Email address", Label: "Email address",
Value: f.Email, Value: f.Email,
}), }),
Radios(RadiosParams{ Radios(OptionsParams{
Form: f, Form: f,
FormField: "Department", FormField: "Department",
Name: "department", Name: "department",
Label: "Department", Label: "Department",
Value: f.Department, Value: f.Department,
Options: []Radio{ Options: []Choice{
{Value: "sales", Label: "Sales"}, {Value: "sales", Label: "Sales"},
{Value: "marketing", Label: "Marketing"}, {Value: "marketing", Label: "Marketing"},
{Value: "hr", Label: "HR"}, {Value: "hr", Label: "HR"},

View file

@ -29,7 +29,11 @@ func AdminEntityDelete(ctx echo.Context, entityTypeName string) error {
H2(Textf("Are you sure you want to delete this %s?", entityTypeName)), H2(Textf("Are you sure you want to delete this %s?", entityTypeName)),
ControlGroup( ControlGroup(
FormButton("is-link", "Delete"), FormButton("is-link", "Delete"),
ButtonLink(r.Path(routenames.AdminEntityList(entityTypeName)), "is-secondary", "Cancel"), ButtonLink(
r.Path(routenames.AdminEntityList(entityTypeName)),
"is-secondary",
"Cancel",
),
), ),
CSRF(r), CSRF(r),
) )
@ -44,7 +48,7 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u
} else { } else {
r.Title = fmt.Sprintf("Edit %s", schema.Name) r.Title = fmt.Sprintf("Edit %s", schema.Name)
} }
// TODO inline validation?
nodes := make(Group, 0) nodes := make(Group, 0)
getValue := func(name string) string { getValue := func(name string) string {
@ -64,7 +68,7 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u
if !isNew && f.Immutable { if !isNew && f.Immutable {
continue continue
} }
// TODO sensitive edits
switch f.Info.Type { switch f.Info.Type {
case field.TypeString: case field.TypeString:
p := InputFieldParams{ p := InputFieldParams{
@ -92,6 +96,7 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u
Value: getValue(f.Name), Value: getValue(f.Name),
})) }))
case field.TypeInt, field.TypeInt8, field.TypeInt16, field.TypeInt32, field.TypeInt64, case field.TypeInt, field.TypeInt8, field.TypeInt16, field.TypeInt32, field.TypeInt64,
field.TypeUint, field.TypeUint8, field.TypeUint16, field.TypeUint32, field.TypeUint64,
field.TypeFloat32, field.TypeFloat64: field.TypeFloat32, field.TypeFloat64:
nodes = append(nodes, InputField(InputFieldParams{ nodes = append(nodes, InputField(InputFieldParams{
Name: f.Name, Name: f.Name,
@ -106,8 +111,25 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u
Checked: getValue(f.Name) == "true", Checked: getValue(f.Name) == "true",
})) }))
case field.TypeEnum: case field.TypeEnum:
// TODO options := make([]Choice, 0, len(f.Enums)+1)
nodes = append(nodes, P(Textf("%s not supported", f.Name))) if f.Nillable {
options = append(options, Choice{
Label: "-",
Value: "",
})
}
for _, enum := range f.Enums {
options = append(options, Choice{
Label: enum.V,
Value: enum.V,
})
}
nodes = append(nodes, SelectList(OptionsParams{
Name: f.Name,
Label: admin.FieldLabel(f.Name),
Value: getValue(f.Name),
Options: options,
}))
default: default:
nodes = append(nodes, P(Textf("%s not supported", f.Name))) nodes = append(nodes, P(Textf("%s not supported", f.Name)))
} }