Skip to content

Commit

Permalink
Clean up and rename Stateful trait to Control
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Oct 24, 2023
1 parent 4b258e5 commit 4c05c22
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn preview(_attrs: TokenStream, input: TokenStream) -> TokenStream {
#[allow(non_upper_case_globals)]
#vis static #ident: lookbook::Preview = lookbook::Preview::new(#name, |cx| {
use dioxus::prelude::*;
use lookbook::Stateful;
use lookbook::Control;

fn f<'a>(cx: Scope<'a>) -> Element<'a> {
#(#states)*
Expand Down
55 changes: 55 additions & 0 deletions src/control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use dioxus::prelude::*;

/// A controllable property.
pub trait Control<'a>: Sized {
type State;

/// Create the initial state.
fn state(default: Option<Self>) -> Self::State;

/// Convert the current state to `Self`.
fn from_state<T>(cx: Scope<'a, T>, state: &Self::State) -> Self;

/// Render the controller element.
fn control(cx: Scope<'a>, name: &'static str, state: &'a UseState<Self::State>) -> Element<'a>;
}

impl<'a> Control<'a> for &'a str {
type State = String;

fn state(default: Option<Self>) -> Self::State {
default.map(String::from).unwrap_or_default()
}

fn from_state<T>(cx: Scope<'a, T>, state: &Self::State) -> Self {
cx.bump().alloc(state.clone())
}

fn control(cx: Scope<'a>, name: &'static str, state: &'a UseState<Self::State>) -> Element<'a> {
render!(dioxus_material::TextField {
label: name,
value: state,
onchange: move |event: FormEvent| state.set(event.data.value.clone())
})
}
}

impl<'a> Control<'a> for u32 {
type State = u32;

fn state(_default: Option<Self>) -> Self::State {
0
}

fn from_state<T>(_cx: Scope<'a, T>, state: &Self::State) -> Self {
*state
}

fn control(cx: Scope<'a>, name: &'static str, state: &'a UseState<Self::State>) -> Element<'a> {
render!(dioxus_material::TextField {
label: name,
value: "{state}",
onchange: move |event: FormEvent| state.set(event.data.value.parse().unwrap())
})
}
}
53 changes: 3 additions & 50 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use dioxus_router::prelude::*;

pub use lookbook_macros::preview;

mod control;
pub use control::Control;

mod ui;
use ui::Wrap;
pub use ui::{Look, LookBook};
Expand Down Expand Up @@ -61,53 +64,3 @@ fn ComponentScreen(cx: Scope, name: String) -> Element {

render!(Child {})
}

pub trait Stateful<'a>: Sized {
type State;

fn state(default: Option<Self>) -> Self::State;

fn from_state<T>(cx: Scope<'a, T>, state: &Self::State) -> Self;

fn control(cx: Scope<'a>, name: &'static str, state: &'a UseState<Self::State>) -> Element<'a>;
}

impl<'a> Stateful<'a> for &'a str {
type State = String;

fn state(default: Option<Self>) -> Self::State {
default.map(String::from).unwrap_or_default()
}

fn from_state<T>(cx: Scope<'a, T>, state: &Self::State) -> Self {
cx.bump().alloc(state.clone())
}

fn control(cx: Scope<'a>, name: &'static str, state: &'a UseState<Self::State>) -> Element<'a> {
render!(dioxus_material::TextField {
label: name,
value: state,
onchange: move |event: FormEvent| state.set(event.data.value.clone())
})
}
}

impl<'a> Stateful<'a> for u32 {
type State = u32;

fn state(_default: Option<Self>) -> Self::State {
0
}

fn from_state<T>(_cx: Scope<'a, T>, state: &Self::State) -> Self {
*state
}

fn control(cx: Scope<'a>, name: &'static str, state: &'a UseState<Self::State>) -> Element<'a> {
render!(dioxus_material::TextField {
label: name,
value: "{state}",
onchange: move |event: FormEvent| state.set(event.data.value.parse().unwrap())
})
}
}

0 comments on commit 4c05c22

Please sign in to comment.