1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
use crate::core::MaybeRwSignal;
use default_struct_builder::DefaultBuilder;
use leptos::*;
/// Cycle through a list of items.
///
/// ## Demo
///
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_cycle_list)
///
/// ## Usage
///
/// ```
/// # use leptos::*;
/// # use leptos::logging::log;
/// use leptos_use::{use_cycle_list, UseCycleListReturn};
/// #
/// # #[component]
/// # fn Demo() -> impl IntoView {
/// let UseCycleListReturn { state, next, prev, .. } = use_cycle_list(
/// vec!["Dog", "Cat", "Lizard", "Shark", "Whale", "Dolphin", "Octopus", "Seal"]
/// );
///
/// log!("{}", state.get()); // "Dog"
///
/// prev();
///
/// log!("{}", state.get()); // "Seal"
/// #
/// # view! { }
/// # }
/// ```
pub fn use_cycle_list<T, L>(
list: L,
) -> UseCycleListReturn<
T,
impl Fn(usize) -> T + Clone,
impl Fn() + Clone,
impl Fn() + Clone,
impl Fn(i64) -> T + Clone,
>
where
T: Clone + PartialEq + 'static,
L: Into<MaybeSignal<Vec<T>>>,
{
use_cycle_list_with_options(list, UseCycleListOptions::default())
}
pub fn use_cycle_list_with_options<T, L>(
list: L,
options: UseCycleListOptions<T>,
) -> UseCycleListReturn<
T,
impl Fn(usize) -> T + Clone,
impl Fn() + Clone,
impl Fn() + Clone,
impl Fn(i64) -> T + Clone,
>
where
T: Clone + PartialEq + 'static,
L: Into<MaybeSignal<Vec<T>>>,
{
let UseCycleListOptions {
initial_value,
fallback_index,
get_position,
} = options;
let list = list.into();
let get_initial_value = {
let list = list.get_untracked();
let first = list.first().cloned();
move || {
if let Some(initial_value) = initial_value {
initial_value
} else {
MaybeRwSignal::from(first.expect("The provided list shouldn't be empty"))
}
}
};
let (state, set_state) = get_initial_value().into_signal();
let index = {
let list = list.clone();
Signal::derive(move || {
list.with(|list| {
let index = get_position(&state.get(), list);
if let Some(index) = index {
index
} else {
fallback_index
}
})
})
};
let set = {
let list = list.clone();
move |i: usize| {
list.with(|list| {
let length = list.len();
let index = i % length;
let value = list[index].clone();
set_state.update({
let value = value.clone();
move |v| *v = value
});
value
})
}
};
let shift = {
let list = list.clone();
let set = set.clone();
move |delta: i64| {
let index = list.with(|list| {
let length = list.len() as i64;
let i = index.get_untracked() as i64 + delta;
(i % length) + length
});
set(index as usize)
}
};
let next = {
let shift = shift.clone();
move || {
shift(1);
}
};
let prev = {
let shift = shift.clone();
move || {
shift(-1);
}
};
let _ = {
let set = set.clone();
leptos::watch(move || list.get(), move |_, _, _| set(index.get()), false)
};
UseCycleListReturn {
state,
set_state,
index,
set_index: set,
next,
prev,
shift,
}
}
/// Options for [`use_cycle_list_with_options`].
#[derive(DefaultBuilder)]
pub struct UseCycleListOptions<T>
where
T: Clone + PartialEq + 'static,
{
/// The initial value of the state. Can be a Signal. If none is provided the first entry
/// of the list will be used.
#[builder(keep_type)]
initial_value: Option<MaybeRwSignal<T>>,
/// The default index when the current value is not found in the list.
/// For example when `get_index_of` returns `None`.
fallback_index: usize,
/// Custom function to get the index of the current value. Defaults to `Iterator::position()`
#[builder(keep_type)]
get_position: fn(&T, &Vec<T>) -> Option<usize>,
}
impl<T> Default for UseCycleListOptions<T>
where
T: Clone + PartialEq + 'static,
{
fn default() -> Self {
Self {
initial_value: None,
fallback_index: 0,
get_position: |value: &T, list: &Vec<T>| list.iter().position(|v| v == value),
}
}
}
/// Return type of [`use_cycle_list`].
pub struct UseCycleListReturn<T, SetFn, NextFn, PrevFn, ShiftFn>
where
T: Clone + PartialEq + 'static,
SetFn: Fn(usize) -> T + Clone,
NextFn: Fn() + Clone,
PrevFn: Fn() + Clone,
ShiftFn: Fn(i64) -> T + Clone,
{
/// Current value
pub state: Signal<T>,
/// Set current value
pub set_state: WriteSignal<T>,
/// Current index of current value in list
pub index: Signal<usize>,
/// Set current index of current value in list
pub set_index: SetFn,
/// Go to next value (cyclic)
pub next: NextFn,
/// Go to previous value (cyclic)
pub prev: PrevFn,
/// Move by the specified amount from the current value (cyclic)
pub shift: ShiftFn,
}