Skip to content

Commit

Permalink
yeet Callback::reform
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Dec 26, 2021
1 parent a956068 commit 33e7770
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 62 deletions.
12 changes: 8 additions & 4 deletions examples/boids/src/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ impl Component for Slider {
10f64.powi(-(p as i32))
});

let oninput = onchange.reform(|e: InputEvent| {
let input: HtmlInputElement = e.target_unchecked_into();
input.value_as_number()
});
let oninput = {
let onchange = onchange.clone();
Callback::from(move |e: InputEvent| {
let input: HtmlInputElement = e.target_unchecked_into();
let output = input.value_as_number();
onchange.emit(output);
})
};

html! {
<div class="slider">
Expand Down
27 changes: 21 additions & 6 deletions examples/function_todomvc/src/components/entry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::hooks::use_bool_toggle::use_bool_toggle;
use crate::hooks::use_bool_toggle::{use_bool_toggle, UseBoolToggleHandle};
use crate::state::Entry as Item;
use web_sys::{HtmlInputElement, MouseEvent};
use yew::events::{Event, FocusEvent, KeyboardEvent};
Expand Down Expand Up @@ -37,21 +37,36 @@ pub fn entry(props: &EntryProps) -> Html {
class.push("completed");
}

let ontoggle = {
let ontoggle = props.ontoggle.clone();
move |_| ontoggle.emit(id)
};

let onremove = {
let onremove = props.onremove.clone();
move |_| onremove.emit(id)
};

let ondblclick = {
let edit_toggle = UseBoolToggleHandle::clone(&edit_toggle);
move |_| {
edit_toggle.clone().toggle();
}
};

html! {
<li {class}>
<div class="view">
<input
type="checkbox"
class="toggle"
checked={props.entry.completed}
onclick={props.ontoggle.reform(move |_| id)}
onclick={ontoggle}
/>
<label ondblclick={Callback::once(move |_| {
edit_toggle.toggle();
})}>
<label {ondblclick}>
{ &props.entry.description }
</label>
<button class="destroy" onclick={props.onremove.reform(move |_| id)} />
<button class="destroy" onclick={onremove} />
</div>
<EntryEdit entry={props.entry.clone()} onedit={props.onedit.clone()} editing={is_editing} />
</li>
Expand Down
7 changes: 6 additions & 1 deletion examples/function_todomvc/src/components/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ pub fn Filter(props: &FilterProps) -> Html {
"not-selected"
};

let onset_filter = {
let onset_filter = props.onset_filter.clone();
move |_| onset_filter.emit(filter)
};

html! {
<li>
<a class={cls}
href={props.filter.as_href()}
onclick={props.onset_filter.reform(move |_| filter)}
onclick={onset_filter}
>
{ props.filter }
</a>
Expand Down
36 changes: 12 additions & 24 deletions examples/function_todomvc/src/hooks/use_bool_toggle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::ops::Deref;
use std::rc::Rc;
use yew::functional::use_hook;
use yew::{use_state_eq, UseStateHandle};

#[derive(Clone)]
pub struct UseBoolToggleHandle {
value: bool,
value: UseStateHandle<bool>,
toggle: Rc<dyn Fn()>,
}

Expand Down Expand Up @@ -43,28 +44,15 @@ impl Deref for UseBoolToggleHandle {
/// ...
/// ```
pub fn use_bool_toggle(default: bool) -> UseBoolToggleHandle {
use_hook(
|| default,
move |hook, updater| {
updater.post_render(move |state: &mut bool| {
if *state != default {
*state = default;
}
false
});
let state = use_state_eq(|| default);

let toggle = Rc::new(move || {
updater.callback(move |st: &mut bool| {
*st = !*st;
true
})
});
let toggle = {
let state = state.clone();
Rc::new(move || state.set(!*state))
};

UseBoolToggleHandle {
value: *hook,
toggle,
}
},
|_| {},
)
UseBoolToggleHandle {
value: state.clone(),
toggle,
}
}
11 changes: 7 additions & 4 deletions examples/nested_list/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ impl Component for ListHeader {

fn view(&self, ctx: &Context<Self>) -> Html {
let list_link = ctx.props().list_link.borrow().clone().unwrap();
let onmouseover = ctx.props().on_hover.reform(|e: MouseEvent| {
e.stop_propagation();
Hovered::Header
});
let onmouseover = {
let on_hover = ctx.props().on_hover.clone();
move |e: MouseEvent| {
e.stop_propagation();
on_hover.emit(Hovered::Header)
}
};

html! {
<div
Expand Down
7 changes: 4 additions & 3 deletions examples/nested_list/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ impl Component for ListItem {
fn view(&self, ctx: &Context<Self>) -> Html {
let onmouseover = {
let name = ctx.props().name.clone();
ctx.props().on_hover.reform(move |e: MouseEvent| {
let on_hover = ctx.props().on_hover.clone();
move |e: MouseEvent| {
e.stop_propagation();
Hovered::Item(name.clone())
})
on_hover.emit(Hovered::Item(name.clone()))
}
};
html! {
<div class="list-item" {onmouseover}>
Expand Down
11 changes: 7 additions & 4 deletions examples/nested_list/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ impl Component for List {

fn view(&self, ctx: &Context<Self>) -> Html {
let inactive = if self.inactive { "inactive" } else { "" };
let onmouseover = ctx.props().on_hover.reform(|e: MouseEvent| {
e.stop_propagation();
Hovered::List
});
let onmouseover = {
let on_hover = ctx.props().on_hover.clone();
move |e: MouseEvent| {
e.stop_propagation();
on_hover.emit(Hovered::List);
}
};
html! {
<div class="list-container" {onmouseover}>
<div class={classes!("list", inactive)}>
Expand Down
16 changes: 0 additions & 16 deletions packages/yew/src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,4 @@ impl<IN> Default for Callback<IN> {
}
}

impl<IN: 'static, OUT: 'static> Callback<IN, OUT> {
/// Changes the input type of the callback to another.
/// Works like the `map` method but in the opposite direction.
pub fn reform<F, T>(&self, func: F) -> Callback<T>
where
F: Fn(T) -> IN + 'static,
{
let this = self.clone();
let func = move |input| {
let output = func(input);
this.emit(output);
};
Callback::from(func)
}
}

impl<IN, OUT> ImplicitClone for Callback<IN, OUT> {}

0 comments on commit 33e7770

Please sign in to comment.