Skip to content

Commit

Permalink
Fix widget extending for Form
Browse files Browse the repository at this point in the history
Fixes #694
  • Loading branch information
andydotxyz committed Feb 24, 2020
1 parent 3fb5037 commit 91c972f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
15 changes: 9 additions & 6 deletions widget/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package widget

import (
"fyne.io/fyne"
"fyne.io/fyne/internal/cache"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
)
Expand Down Expand Up @@ -51,14 +52,16 @@ func (f *Form) Append(text string, widget fyne.CanvasObject) {

// AppendItem adds the specified row to the end of the Form
func (f *Form) AppendItem(item *FormItem) {
// ensure we have a renderer set up
Renderer(f)
f.ExtendBaseWidget(f) // could be called before render

// ensure we have a renderer set up (that creates itemGrid)...
cache.Renderer(f.super())

f.Items = append(f.Items, item)
f.itemGrid.AddObject(f.createLabel(item.Text))
f.itemGrid.AddObject(item.Widget)

Refresh(f)
f.Refresh()
}

// MinSize returns the size that this widget should not shrink below
Expand All @@ -77,7 +80,7 @@ func (f *Form) CreateRenderer() fyne.WidgetRenderer {
}

if f.OnCancel == nil && f.OnSubmit == nil {
return Renderer(NewVBox(f.itemGrid))
return cache.Renderer(NewVBox(f.itemGrid))
}

buttons := NewHBox(layout.NewSpacer())
Expand All @@ -90,14 +93,14 @@ func (f *Form) CreateRenderer() fyne.WidgetRenderer {

buttons.Append(submit)
}
return Renderer(NewVBox(f.itemGrid, buttons))
return cache.Renderer(NewVBox(f.itemGrid, buttons))
}

// NewForm creates a new form widget with the specified rows of form items
// and (if any of them should be shown) a form controls row at the bottom
func NewForm(items ...*FormItem) *Form {
form := &Form{BaseWidget: BaseWidget{}, Items: items}
form.ExtendBaseWidget(form)

Renderer(form).Layout(form.MinSize())
return form
}
39 changes: 39 additions & 0 deletions widget/form_extend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package widget

import (
"testing"

"fyne.io/fyne/test"

"github.com/stretchr/testify/assert"
)

type extendedForm struct {
Form
}

func TestForm_Extended_CreateRenderer(t *testing.T) {
form := &extendedForm{}
form.ExtendBaseWidget(form)
form.Items = []*FormItem{{Text: "test1", Widget: NewEntry()}}
assert.NotNil(t, test.WidgetRenderer(form))
assert.Equal(t, 2, len(form.itemGrid.Objects))

form.Append("test2", NewEntry())
assert.Equal(t, 4, len(form.itemGrid.Objects))
}

func TestForm_Extended_Append(t *testing.T) {
form := &extendedForm{}
form.ExtendBaseWidget(form)
form.Items = []*FormItem{{Text: "test1", Widget: NewEntry()}}
assert.Equal(t, 1, len(form.Items))

form.Append("test2", NewEntry())
assert.True(t, len(form.Items) == 2)

item := &FormItem{Text: "test3", Widget: NewEntry()}
form.AppendItem(item)
assert.True(t, len(form.Items) == 3)
assert.Equal(t, item, form.Items[2])
}

0 comments on commit 91c972f

Please sign in to comment.