You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using a ListState to display selectable items in a TUI, there is no easy way to advance the selection right now requiring a wrapper struct to handle it currently.
Solution
As an example, this is the wrapper code I've been using
#[derive(Debug,Default)]pubstructStatefulList<T>{state:ListState,items:Vec<T>,}impl<T>StatefulList<T>{pubfnwith_items(items:Vec<T>) -> Self{Self{state:ListState::default(),
items,}}pubfnnext(&mutself){let i = matchself.state.selected(){Some(i) => {if i >= self.items.len() - 1{0}else{
i + 1}}None => 0,};self.state.select(Some(i));}pubfnprevious(&mutself){let i = matchself.state.selected(){Some(i) => {if i == 0{self.items.len() - 1}else{
i - 1}}None => 0,};self.state.select(Some(i));}}
But if ListState just had .next()/.prev() implemented on it, I wouldn't need this code at all.
Additional context
It's likely other stateful widgets that are often used to make selectable "lists" would benefit from similar functions, but I'm not sure which ones exist and are missing them as of right now.
The text was updated successfully, but these errors were encountered:
sparky8251
changed the title
Add .next()/.prev() to ListState to make traversing a list easier
Add .next()/.prev() to ListState to make traversing a list of selectable items easier
May 31, 2024
Problem
When using a
ListState
to display selectable items in a TUI, there is no easy way to advance the selection right now requiring a wrapper struct to handle it currently.Solution
As an example, this is the wrapper code I've been using
But if
ListState
just had.next()
/.prev()
implemented on it, I wouldn't need this code at all.Additional context
It's likely other stateful widgets that are often used to make selectable "lists" would benefit from similar functions, but I'm not sure which ones exist and are missing them as of right now.
The text was updated successfully, but these errors were encountered: