Skip to content

Commit

Permalink
Add Hovered() methods to Select and MultiSelect fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kralicky committed Sep 18, 2024
1 parent a9285a0 commit 8fb158f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ func (m *MultiSelect[T]) Blur() tea.Cmd {
return nil
}

// Hovered returns the value of the option under the cursor, and a bool
// indicating whether one was found. If there are no visible options, returns
// a zero-valued T and false.
func (m *MultiSelect[T]) Hovered() (T, bool) {
if len(m.filteredOptions) == 0 || m.cursor >= len(m.filteredOptions) {
var zero T
return zero, false
}
return m.filteredOptions[m.cursor].Value, true
}

// KeyBinds returns the help message for the multi-select field.
func (m *MultiSelect[T]) KeyBinds() []key.Binding {
binds := []key.Binding{
Expand Down
11 changes: 11 additions & 0 deletions field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ func (s *Select[T]) Blur() tea.Cmd {
return nil
}

// Hovered returns the value of the option under the cursor, and a bool
// indicating whether one was found. If there are no visible options, returns
// a zero-valued T and false.
func (m *Select[T]) Hovered() (T, bool) {
if len(m.filteredOptions) == 0 || m.selected >= len(m.filteredOptions) {
var zero T
return zero, false
}
return m.filteredOptions[m.selected].Value, true
}

// KeyBinds returns the help keybindings for the select field.
func (s *Select[T]) KeyBinds() []key.Binding {
return []key.Binding{
Expand Down

0 comments on commit 8fb158f

Please sign in to comment.