From 1926040ccf0452e438e84613264ce0044f5437a6 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 25 Jul 2024 13:06:54 -0400 Subject: [PATCH] fix(select): select all/none (#325) * feat(select): select all Signed-off-by: Carlos Alexandro Becker * fix: mimic previous behavior * fix: help * fix: limits * fix: single keybinding * fix: filtering --------- Signed-off-by: Carlos Alexandro Becker --- field_multiselect.go | 27 ++++++++++++++++++++++++++- keymap.go | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/field_multiselect.go b/field_multiselect.go index 7a217aa3..4afba3bd 100644 --- a/field_multiselect.go +++ b/field_multiselect.go @@ -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 } @@ -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, @@ -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. @@ -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()) diff --git a/keymap.go b/keymap.go index 354dfefd..23b829d6 100644 --- a/keymap.go +++ b/keymap.go @@ -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. @@ -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")),