Skip to content

Commit

Permalink
Add CheckGroup.Remove (#3323)
Browse files Browse the repository at this point in the history
Fixes #3124
  • Loading branch information
rangzen committed Oct 22, 2022
1 parent 0a1325c commit 2afd63a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
23 changes: 23 additions & 0 deletions widget/check_group.go
@@ -1,6 +1,8 @@
package widget

import (
"strings"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/widget"
Expand Down Expand Up @@ -75,6 +77,27 @@ func (r *CheckGroup) Refresh() {
r.BaseWidget.Refresh()
}

// Remove removes the first occurrence of the specified option found from a CheckGroup widget.
// Return true if an option was removed.
//
// Since: 2.3
func (r *CheckGroup) Remove(option string) bool {
for i, o := range r.Options {
if strings.EqualFold(option, o) {
r.Options = append(r.Options[:i], r.Options[i+1:]...)
for j, s := range r.Selected {
if strings.EqualFold(option, s) {
r.Selected = append(r.Selected[:j], r.Selected[j+1:]...)
break
}
}
r.Refresh()
return true
}
}
return false
}

// SetSelected sets the checked options, it can be used to set a default option.
func (r *CheckGroup) SetSelected(options []string) {
//if r.Selected == options {
Expand Down
22 changes: 22 additions & 0 deletions widget/check_group_test.go
Expand Up @@ -197,3 +197,25 @@ func TestCheckGroup_ToggleSelectionWithSpaceKey(t *testing.T) {
test.Type(canvas.Focused(), " ")
assert.Equal(t, []string{"Option B"}, check.Selected, "cannot unselect required check")
}

func TestCheckGroup_ManipulateOptions(t *testing.T) {
check := &widget.CheckGroup{Options: []string{}}
assert.Equal(t, 0, len(check.Options))

check.Append("test1")
assert.Equal(t, 1, len(check.Options))
check.SetSelected([]string{"test1"})
assert.Equal(t, 1, len(check.Selected))

check.Append("test2")
assert.Equal(t, 2, len(check.Options))

removed := check.Remove("nope")
assert.Equal(t, false, removed)
assert.Equal(t, 2, len(check.Options))

removed = check.Remove("test1")
assert.Equal(t, true, removed)
assert.Equal(t, 1, len(check.Options))
assert.Equal(t, 0, len(check.Selected))
}

0 comments on commit 2afd63a

Please sign in to comment.