Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List: Provide Index of SelectedItem in all items #366

Open
dploeger opened this issue Apr 11, 2023 · 1 comment
Open

List: Provide Index of SelectedItem in all items #366

dploeger opened this issue Apr 11, 2023 · 1 comment

Comments

@dploeger
Copy link

With ccmanager I have a backend refresh system that updates list items from time to time. It uses SetItem to update the item when it is changed and uses Index to get the index of the item to replace. However, when filtering is used, Index returns the index of the selected filtered item and not which index the item holds in the slice of all items.

To be backwards compatible, I'd propose a "IndexOfSelectedItem" variable to get the index of the currently selected item in the slice of all items.

Or am I missing something here?

@lorenzo-milicia
Copy link

lorenzo-milicia commented Nov 12, 2023

I've encountered the same annoying issue, but from my debugging journey, it seems that the issue is in the list.go file line 341 and 345.

func (m *Model) SetItems(i []Item) tea.Cmd {
	var cmd tea.Cmd
	m.items = i

	if m.filterState != Unfiltered {
		m.filteredItems = nil
		cmd = filterItems(*m)
	}

	m.updatePagination()
	m.updateKeybindings()
	return cmd
}

When calling the SetItems method while filtering, the current filtered list is set to null, only for the pagination to be updated with the m.updatePagination() call. This then sets the index to 0, since there are no items in the filteredItems.
Commenting line 341 stops the behaviour of the index reset while filtering, but it's definitely not a solution. I can understand the reason behind, as we prefer to asynchronously compute the filter again on a change of items, without showing old matches that might not be accurate anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants