Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Support section disable label #369

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/docsrc/examples/examples_presets/detailing_inline_edit.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package examples_presets

import (
"fmt"
"log"

"github.com/qor5/admin/v3/media"
"github.com/qor5/admin/v3/presets"
"github.com/qor5/admin/v3/presets/gorm2op"
"github.com/qor5/web/v3"
vx "github.com/qor5/x/v3/ui/vuetifyx"
h "github.com/theplant/htmlgo"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -105,6 +107,58 @@
return
}

func PresetsDetailSectionLabel(b *presets.Builder, db *gorm.DB) (
cust *presets.ModelBuilder,
cl *presets.ListingBuilder,
ce *presets.EditingBuilder,
dp *presets.DetailingBuilder,
) {
err := db.AutoMigrate(&Customer{}, &CreditCard{}, &Note{})
if err != nil {
panic(err)

Check warning on line 118 in docs/docsrc/examples/examples_presets/detailing_inline_edit.go

View check run for this annotation

Codecov / codecov/patch

docs/docsrc/examples/examples_presets/detailing_inline_edit.go#L118

Added line #L118 was not covered by tests
}
b.DataOperator(gorm2op.DataOperator(db))

cust = b.Model(&Customer{})
dp = cust.Detailing("section1", "section2", "CreditCards", "Notes").Drawer(true)
cust.Detailing().WrapFetchFunc(func(in presets.FetchFunc) presets.FetchFunc {
return func(obj interface{}, id string, ctx *web.EventContext) (r interface{}, err error) {
c := obj.(*Customer)
if c.CreditCards == nil {
c.CreditCards = []*CreditCard{{Name: "Only is mock card, can't be save"}}
}
if c.Notes == nil {
c.Notes = []*Note{{Content: "Only is mock note, can't be save"}}
}
return c, nil
}
})
dp.Section("section1").Label("section_with_label").Editing("Name")
dp.Section("section2").Label("section_without_label").DisableLabel().Editing("Email")
dp.Section("CreditCards").Label("section_list_with_label").IsList(&CreditCard{}).
Editing("Name").
ElementShowComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
card := obj.(*CreditCard)
return vx.VXTextField().VField(fmt.Sprintf("%s.Name", field.FormKey), card.Name)
}).
ElementEditComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
card := obj.(*CreditCard)
return vx.VXTextField().Text(card.Name)
})

Check warning on line 147 in docs/docsrc/examples/examples_presets/detailing_inline_edit.go

View check run for this annotation

Codecov / codecov/patch

docs/docsrc/examples/examples_presets/detailing_inline_edit.go#L144-L147

Added lines #L144 - L147 were not covered by tests
dp.Section("Notes").Label("section_list_without_label").IsList(&Note{}).DisableLabel().
Editing("Content").
ElementShowComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
note := obj.(*Note)
return vx.VXTextField().VField(fmt.Sprintf("%s.Name", field.FormKey), note.Content)
}).
ElementEditComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
note := obj.(*Note)
return vx.VXTextField().Text(note.Content)
})

Check warning on line 157 in docs/docsrc/examples/examples_presets/detailing_inline_edit.go

View check run for this annotation

Codecov / codecov/patch

docs/docsrc/examples/examples_presets/detailing_inline_edit.go#L154-L157

Added lines #L154 - L157 were not covered by tests

return
}

func PresetsDetailInlineEditValidate(b *presets.Builder, db *gorm.DB) (
cust *presets.ModelBuilder,
cl *presets.ListingBuilder,
Expand Down
26 changes: 26 additions & 0 deletions docs/docsrc/examples/examples_presets/detailing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,29 @@ func TestPresetsDetailSectionValidate(t *testing.T) {
})
}
}

func TestPresetsDetailSectionLabel(t *testing.T) {
pb := presets.New().DataOperator(gorm2op.DataOperator(TestDB))
PresetsDetailSectionLabel(pb, TestDB)

cases := []multipartestutils.TestCase{
{
Name: "section label",
Debug: true,
ReqFunc: func() *http.Request {
detailData.TruncatePut(SqlDB)
return multipartestutils.NewMultipartBuilder().
PageURL("/customers?__execute_event__=presets_DetailingDrawer&id=12").
BuildEventFuncRequest()
},
ExpectPortalUpdate0ContainsInOrder: []string{"section_with_label", "section_list_with_label"},
ExpectPortalUpdate0NotContains: []string{"section_without_label", "section_list_without_label"},
},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
multipartestutils.RunCase(t, c, pb)
})
}
}
1 change: 1 addition & 0 deletions docs/docsrc/examples/examples_presets/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
addExample(mux, db, PresetsListingCustomizationSearcher)
addExample(mux, db, PresetsDetailInlineEditDetails)
addExample(mux, db, PresetsDetailInlineEditInspectTables)
addExample(mux, db, PresetsDetailSectionLabel)

Check warning on line 42 in docs/docsrc/examples/examples_presets/mux.go

View check run for this annotation

Codecov / codecov/patch

docs/docsrc/examples/examples_presets/mux.go#L42

Added line #L42 was not covered by tests
addExample(mux, db, PresetsDetailNestedMany)
addExample(mux, db, PresetsDetailInlineEditFieldSections)
addExample(mux, db, PresetsDetailInlineEditValidate)
Expand Down
18 changes: 13 additions & 5 deletions presets/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (d *SectionsBuilder) appendNewSection(name string) (r *SectionBuilder) {
elementHover: true,
alwaysShowListLabel: false,
isList: false,
disableLabel: false,
}
r.editingFB.Model(d.mb.model)
r.editingFB.defaults = d.mb.writeFields.defaults
Expand Down Expand Up @@ -178,7 +179,8 @@ type SectionBuilder struct {
componentEditFunc FieldComponentFunc
father *SectionsBuilder

isList bool
isList bool
disableLabel bool
// Only when isList is false, the following param will take effect
// control Delete button in the show component
componentEditBtnFunc ObjectBoolFunc
Expand Down Expand Up @@ -332,6 +334,12 @@ func (b *SectionBuilder) EditComponentFunc(v FieldComponentFunc) (r *SectionBuil

func (b *SectionBuilder) Label(label string) (r *SectionBuilder) {
b.father.Field(b.name).Label(label)
b.label = label
return b
}

func (b *SectionBuilder) DisableLabel() (r *SectionBuilder) {
b.disableLabel = true
return b
}

Expand Down Expand Up @@ -398,7 +406,7 @@ func (b *SectionBuilder) viewComponent(obj interface{}, field *FieldContext, ctx
}
}
content := h.Div()
if b.label != "" {
if b.label != "" && !b.disableLabel {
lb := i18n.PT(ctx.R, ModelsI18nModuleKey, b.father.mb.label, field.Label)
content.AppendChildren(
h.Div(h.Span(lb).Style("fontSize:16px; font-weight:500;")).Class("mb-2"),
Expand Down Expand Up @@ -462,7 +470,7 @@ func (b *SectionBuilder) editComponent(obj interface{}, field *FieldContext, ctx

content := h.Div()

if b.label != "" {
if b.label != "" && !b.disableLabel {
lb := i18n.PT(ctx.R, ModelsI18nModuleKey, b.father.mb.label, field.Label)
label := h.Div(h.Span(lb).Style("fontSize:16px; font-weight:500;")).Class("mb-2")
content.AppendChildren(label)
Expand Down Expand Up @@ -568,7 +576,7 @@ func (b *SectionBuilder) listComponent(obj interface{}, _ *FieldContext, ctx *we
label := h.Div(h.Span(lb).Style("fontSize:16px; font-weight:500;")).Class("mb-2")
rows := h.Div()

if b.alwaysShowListLabel {
if b.alwaysShowListLabel && !b.disableLabel {
rows.AppendChildren(label)
}

Expand All @@ -577,7 +585,7 @@ func (b *SectionBuilder) listComponent(obj interface{}, _ *FieldContext, ctx *we
reflectutils.ForEach(list, func(elementObj interface{}) {
defer func() { i++ }()
if i == 0 {
if b.label != "" && !b.alwaysShowListLabel {
if b.label != "" && !b.alwaysShowListLabel && !b.disableLabel {
rows.AppendChildren(label)
}
}
Expand Down
Loading