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 }}
op.SetNillable{{ fieldName $f.Name }}(payload.{{ fieldName $f.Name }})
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) }}
if payload.{{ fieldName $f.Name }} == nil {

View file

@ -19,16 +19,16 @@ type (
Help string
}
RadiosParams struct {
OptionsParams struct {
Form form.Form
FormField string
Name string
Label string
Value string
Options []Radio
Options []Choice
}
Radio struct {
Choice struct {
Value 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))
for i, opt := range el.Options {
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 {
return Div(
Class("field"),

View file

@ -31,13 +31,13 @@ func (f *Contact) Render(r *ui.Request) Node {
Label: "Email address",
Value: f.Email,
}),
Radios(RadiosParams{
Radios(OptionsParams{
Form: f,
FormField: "Department",
Name: "department",
Label: "Department",
Value: f.Department,
Options: []Radio{
Options: []Choice{
{Value: "sales", Label: "Sales"},
{Value: "marketing", Label: "Marketing"},
{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)),
ControlGroup(
FormButton("is-link", "Delete"),
ButtonLink(r.Path(routenames.AdminEntityList(entityTypeName)), "is-secondary", "Cancel"),
ButtonLink(
r.Path(routenames.AdminEntityList(entityTypeName)),
"is-secondary",
"Cancel",
),
),
CSRF(r),
)
@ -44,7 +48,7 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u
} else {
r.Title = fmt.Sprintf("Edit %s", schema.Name)
}
// TODO inline validation?
nodes := make(Group, 0)
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 {
continue
}
// TODO sensitive edits
switch f.Info.Type {
case field.TypeString:
p := InputFieldParams{
@ -92,6 +96,7 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u
Value: getValue(f.Name),
}))
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:
nodes = append(nodes, InputField(InputFieldParams{
Name: f.Name,
@ -106,8 +111,25 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u
Checked: getValue(f.Name) == "true",
}))
case field.TypeEnum:
// TODO
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
options := make([]Choice, 0, len(f.Enums)+1)
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:
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
}