Skip to content

Commit

Permalink
fix(select): select all/none (#325)
Browse files Browse the repository at this point in the history
* feat(select): select all

Signed-off-by: Carlos Alexandro Becker <[email protected]>

* fix: mimic previous behavior

* fix: help

* fix: limits

* fix: single keybinding

* fix: filtering

---------

Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 authored Jul 25, 2024
1 parent 06892e6 commit 1926040
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 26 additions & 1 deletion field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func (m *MultiSelect[T]) Filtering(filtering bool) *MultiSelect[T] {
// Limit sets the limit of the multi-select field.
func (m *MultiSelect[T]) Limit(limit int) *MultiSelect[T] {
m.limit = limit
m.keymap.ToggleAll.SetEnabled(limit == 0)
return m
}

Expand Down Expand Up @@ -220,7 +221,7 @@ func (m *MultiSelect[T]) Blur() tea.Cmd {

// KeyBinds returns the help message for the multi-select field.
func (m *MultiSelect[T]) KeyBinds() []key.Binding {
return []key.Binding{
binds := []key.Binding{
m.keymap.Toggle,
m.keymap.Up,
m.keymap.Down,
Expand All @@ -231,6 +232,10 @@ func (m *MultiSelect[T]) KeyBinds() []key.Binding {
m.keymap.Submit,
m.keymap.Next,
}
if m.limit == 0 {
binds = append(binds, m.keymap.ToggleAll)
}
return binds
}

// Init initializes the multi-select field.
Expand Down Expand Up @@ -377,6 +382,26 @@ func (m *MultiSelect[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
m.updateValue()
case key.Matches(msg, m.keymap.ToggleAll) && m.limit == 0:
selected := false

for _, option := range m.filteredOptions {
if !option.selected {
selected = true
break
}
}

for i, option := range m.options.val {
for j := range m.filteredOptions {
if option.Key == m.filteredOptions[j].Key {
m.options.val[i].selected = selected
m.filteredOptions[j].selected = selected
break
}
}
}
m.updateValue()
case key.Matches(msg, m.keymap.Prev):
m.updateValue()
m.err = m.validate(m.accessor.Get())
Expand Down
2 changes: 2 additions & 0 deletions keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type MultiSelectKeyMap struct {
SetFilter key.Binding
ClearFilter key.Binding
Submit key.Binding
ToggleAll key.Binding
}

// FilePickerKey is the keybindings for filepicker fields.
Expand Down Expand Up @@ -164,6 +165,7 @@ func NewDefaultKeyMap() *KeyMap {
HalfPageDown: key.NewBinding(key.WithKeys("ctrl+d"), key.WithHelp("ctrl+d", "½ page down")),
GotoTop: key.NewBinding(key.WithKeys("home", "g"), key.WithHelp("g/home", "go to start")),
GotoBottom: key.NewBinding(key.WithKeys("end", "G"), key.WithHelp("G/end", "go to end")),
ToggleAll: key.NewBinding(key.WithKeys("ctrl+a"), key.WithHelp("ctrl+a", "toggle select all")),
},
Note: NoteKeyMap{
Prev: key.NewBinding(key.WithKeys("shift+tab"), key.WithHelp("shift+tab", "back")),
Expand Down

0 comments on commit 1926040

Please sign in to comment.