Skip to content

Commit

Permalink
widget.Form: implement Disableable
Browse files Browse the repository at this point in the history
Allow disabling and re-enabling a form. Resolves fyne-io#2157.
  • Loading branch information
markspolakovs authored and andydotxyz committed Apr 15, 2021
1 parent 2deba2b commit e99b4c0
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 1 deletion.
33 changes: 32 additions & 1 deletion widget/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Form struct {
buttonBox *fyne.Container
cancelButton *Button
submitButton *Button

disabled bool
}

// Append adds a new row to the form, using the text as a label next to the specified Widget
Expand Down Expand Up @@ -89,6 +91,33 @@ func (f *Form) Refresh() {
canvas.Refresh(f.super()) // refresh ourselves for BG color - the above updates the content
}

// Enable enables submitting this form.
//
// Since: 2.1
func (f *Form) Enable() {
f.disabled = false
f.cancelButton.Enable()
f.checkValidation(nil) // as the form may be invalid
}

// Disable disables submitting this form.
//
// Since: 2.1
func (f *Form) Disable() {
f.disabled = true
f.submitButton.Disable()
f.cancelButton.Disable()
}

// Disabled returns whether submitting the form is disabled.
// Note that, if the form fails validation, the submit button may be
// disabled even if this method returns true.
//
// Since: 2.1
func (f *Form) Disabled() bool {
return f.disabled
}

func (f *Form) createInput(item *FormItem) fyne.CanvasObject {
_, ok := item.Widget.(fyne.Validatable)
if item.HintText == "" {
Expand Down Expand Up @@ -155,7 +184,9 @@ func (f *Form) checkValidation(err error) {
}
}

f.submitButton.Enable()
if !f.disabled {
f.submitButton.Enable()
}
}

func (f *Form) setUpValidation(widget fyne.CanvasObject, i int) {
Expand Down
68 changes: 68 additions & 0 deletions widget/form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,71 @@ func TestForm_EntryValidation_FirstTypeValid(t *testing.T) {

test.AssertImageMatches(t, "form/validation_entry_first_type_invalid.png", w.Canvas().Capture())
}

func TestForm_DisableEnable(t *testing.T) {
app := test.NewApp()
defer test.NewApp()
app.Settings().SetTheme(theme.LightTheme())

form := &Form{
Items: []*FormItem{
{Text: "test1", Widget: NewEntry()},
},
OnSubmit: func() {}, OnCancel: func() {}}
w := test.NewWindow(form)
defer w.Close()

if form.Disabled() {
t.Error("form.Disabled() returned true when it should have been false")
}

test.AssertImageMatches(t, "form/disable_initial.png", w.Canvas().Capture())

form.Disable()

if !form.Disabled() {
t.Error("form.Disabled() returned false when it should have been true")
}

test.AssertImageMatches(t, "form/disable_disabled.png", w.Canvas().Capture())

form.Enable()

if form.Disabled() {
t.Error("form.Disabled() returned true when it should have been false")
}

test.AssertImageMatches(t, "form/disable_re_enabled.png", w.Canvas().Capture())
}

func TestForm_Disable_Validation(t *testing.T) {
app := test.NewApp()
defer test.NewApp()
app.Settings().SetTheme(theme.LightTheme())

entry := &Entry{Validator: validation.NewRegexp(`^\d{2}-\w{4}$`, "Input is not valid"), Text: "wrong"}

form := &Form{Items: []*FormItem{{Text: "test", Widget: entry}}, OnSubmit: func() {}, OnCancel: func() {}}
w := test.NewWindow(form)
defer w.Close()

test.AssertImageMatches(t, "form/disable_validation_initial.png", w.Canvas().Capture())

form.Disable()

test.AssertImageMatches(t, "form/disable_validation_disabled_invalid.png", w.Canvas().Capture())

form.Enable()

test.AssertImageMatches(t, "form/disable_validation_enabled_invalid.png", w.Canvas().Capture())

entry.SetText("15-true")
test.AssertImageMatches(t, "form/disable_validation_enabled_valid.png", w.Canvas().Capture())

// ensure we don't re-enable the form when entering something valid
entry.SetText("invalid")
form.Disable()
entry.SetText("15-true")

test.AssertImageMatches(t, "form/disable_validation_disabled_valid.png", w.Canvas().Capture())
}
Binary file added widget/testdata/form/disable_disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added widget/testdata/form/disable_initial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added widget/testdata/form/disable_re_enabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e99b4c0

Please sign in to comment.