2023-06-10 00:57:35 +01:00
|
|
|
use leptos::*;
|
|
|
|
use leptos_use::docs::{demo_or_body, Note};
|
2023-10-28 12:16:05 +01:00
|
|
|
use leptos_use::storage::{use_local_storage, JsonCodec};
|
2023-06-10 00:57:35 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2023-10-28 12:16:05 +01:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
2023-06-10 00:57:35 +01:00
|
|
|
pub struct BananaState {
|
|
|
|
pub name: String,
|
2023-10-28 12:16:05 +01:00
|
|
|
pub wearing: String,
|
|
|
|
pub descending: String,
|
2023-06-10 00:57:35 +01:00
|
|
|
pub count: u32,
|
|
|
|
}
|
|
|
|
|
2023-10-28 12:16:05 +01:00
|
|
|
impl Default for BananaState {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
name: "Bananas".to_string(),
|
|
|
|
wearing: "pyjamas".to_string(),
|
|
|
|
descending: "stairs".to_string(),
|
|
|
|
count: 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-10 00:57:35 +01:00
|
|
|
#[component]
|
2023-07-27 18:06:36 +01:00
|
|
|
fn Demo() -> impl IntoView {
|
2023-10-28 12:16:05 +01:00
|
|
|
let (state, set_state, reset) = use_local_storage::<BananaState, JsonCodec>("banana-state");
|
|
|
|
let (state2, _, _) = use_local_storage::<BananaState, JsonCodec>("banana-state");
|
2023-06-10 00:57:35 +01:00
|
|
|
|
2023-07-27 19:48:21 +01:00
|
|
|
view! {
|
|
|
|
<input
|
2023-06-10 00:57:35 +01:00
|
|
|
class="block"
|
2023-06-21 13:09:00 +02:00
|
|
|
prop:value=move || state.get().name
|
2023-06-10 00:57:35 +01:00
|
|
|
on:input=move |e| set_state.update(|s| s.name = event_target_value(&e))
|
|
|
|
type="text"
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
class="block"
|
2023-10-28 12:16:05 +01:00
|
|
|
prop:value=move || state.get().wearing
|
|
|
|
on:input=move |e| set_state.update(|s| s.wearing = event_target_value(&e))
|
2023-06-10 00:57:35 +01:00
|
|
|
type="text"
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
class="block"
|
2023-10-28 12:16:05 +01:00
|
|
|
prop:value=move || state.get().descending
|
|
|
|
on:input=move |e| set_state.update(|s| s.descending = event_target_value(&e))
|
2023-06-10 00:57:35 +01:00
|
|
|
type="text"
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
class="block"
|
2023-06-21 13:09:00 +02:00
|
|
|
prop:value=move || state.get().count
|
|
|
|
value=move || state.get().count
|
2023-07-27 19:48:21 +01:00
|
|
|
on:input=move |e| {
|
|
|
|
set_state
|
|
|
|
.update(|s| s.count = event_target_value(&e).parse::<f64>().unwrap() as u32)
|
|
|
|
}
|
2023-09-30 18:24:06 +01:00
|
|
|
|
2023-06-10 00:57:35 +01:00
|
|
|
type="number"
|
|
|
|
min="0"
|
|
|
|
step="1"
|
|
|
|
max="1000"
|
|
|
|
/>
|
2023-10-28 12:16:05 +01:00
|
|
|
<button on:click=move |_| reset()>"Delete from storage"</button>
|
2023-06-10 00:57:35 +01:00
|
|
|
|
2023-07-27 19:48:21 +01:00
|
|
|
<p>
|
|
|
|
"Second " <b>
|
|
|
|
<code>"use_storage"</code>
|
|
|
|
</b> ":"
|
|
|
|
</p>
|
2023-06-10 00:57:35 +01:00
|
|
|
|
2023-07-27 19:48:21 +01:00
|
|
|
<pre>{move || format!("{:#?}", state2.get())}</pre>
|
2023-06-10 00:57:35 +01:00
|
|
|
|
2023-07-27 19:48:21 +01:00
|
|
|
<Note>
|
|
|
|
"The values are persistent. When you reload the page the values will be the same."
|
|
|
|
</Note>
|
2023-06-10 00:57:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
_ = console_log::init_with_level(log::Level::Debug);
|
|
|
|
console_error_panic_hook::set_once();
|
|
|
|
|
2023-07-27 18:06:36 +01:00
|
|
|
mount_to(demo_or_body(), || {
|
2023-07-27 19:48:21 +01:00
|
|
|
view! { <Demo/> }
|
2023-06-10 00:57:35 +01:00
|
|
|
})
|
|
|
|
}
|