Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ideas for DomBuilder::children() API #27

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn main_js() -> Result<(), JsValue> {
html!("div", {
.style("display", "flex")

.children(&mut [
.children(vec![
html!("div", {
.children_signal_vec(state.boxes.signal_vec()
.animated_map(2000.0, |value, t| {
Expand Down
2 changes: 1 addition & 1 deletion examples/counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl State {
html!("div", {
.class(&*ROOT_CLASS)

.children(&mut [
.children(vec![
html!("div", {
.class(&*TEXT_CLASS)
.text_signal(state.counter.signal().map(|x| format!("Counter: {}", x)))
Expand Down
14 changes: 7 additions & 7 deletions examples/todomvc/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl App {
fn render_header(app: Rc<Self>) -> Dom {
html!("header", {
.class("header")
.children(&mut [
.children(vec![
html!("h1", {
.text("todos")
}),
Expand Down Expand Up @@ -136,7 +136,7 @@ impl App {
.len()
.map(|len| len > 0))

.children(&mut [
.children(vec![
html!("input", {
.class("toggle-all")
.attribute("id", "toggle-all")
Expand Down Expand Up @@ -165,7 +165,7 @@ impl App {

fn render_button(text: &str, route: Route) -> Dom {
html!("li", {
.children(&mut [
.children(vec![
link!(route.url(), {
.text(text)
.class_signal("selected", Route::signal().map(move |x| x == route))
Expand All @@ -183,11 +183,11 @@ impl App {
.len()
.map(|len| len > 0))

.children(&mut [
.children(vec![
html!("span", {
.class("todo-count")

.children(&mut [
.children(vec![
html!("strong", {
.text_signal(app.not_completed_len().map(|len| len.to_string()))
}),
Expand All @@ -204,7 +204,7 @@ impl App {

html!("ul", {
.class("filters")
.children(&mut [
.children(vec![
Self::render_button("All", Route::All),
Self::render_button("Active", Route::Active),
Self::render_button("Completed", Route::Completed),
Expand All @@ -231,7 +231,7 @@ impl App {
pub fn render(app: Rc<Self>) -> Dom {
html!("section", {
.class("todoapp")
.children(&mut [
.children(vec![
Self::render_header(app.clone()),
Self::render_main(app.clone()),
Self::render_footer(app.clone()),
Expand Down
4 changes: 2 additions & 2 deletions examples/todomvc/src/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ impl Todo {
)
.dedupe())

.children(&mut [
.children(vec![
html!("div", {
.class("view")
.children(&mut [
.children(vec![
html!("input", {
.attribute("type", "checkbox")
.class("toggle")
Expand Down
12 changes: 10 additions & 2 deletions src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,12 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
self.has_children = true;
}

// TODO figure out how to make this owned rather than &mut
#[inline]
pub fn children<'a, B: IntoIterator<Item = &'a mut Dom>>(mut self, children: B) -> Self {
pub fn children<B, C>(mut self, children: B) -> Self
where
B: IntoIterator<Item = C>,
C: Into<Dom>,
{
self.check_children();
operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children);
self
Expand Down Expand Up @@ -916,6 +919,11 @@ impl<A> DomBuilder<A> where A: AsRef<HtmlElement> {
}
}

impl<A> From<DomBuilder<A>> for Dom where A: Into<Node> {
fn from(builder: DomBuilder<A>) -> Self {
builder.into_dom()
}
}

// TODO better warning message for must_use
#[must_use]
Expand Down
9 changes: 7 additions & 2 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ fn for_each_vec<A, B>(signal: A, mut callback: B) -> CancelableFutureHandle


#[inline]
pub(crate) fn insert_children_iter<'a, A: IntoIterator<Item = &'a mut Dom>>(element: &Node, callbacks: &mut Callbacks, value: A) {
for dom in value.into_iter() {
pub(crate) fn insert_children_iter<A, B>(element: &Node, callbacks: &mut Callbacks, children: A)
where
A: IntoIterator<Item = B>,
B: Into<Dom>,
{
for child in children.into_iter() {
let mut dom: Dom = child.into();
// TODO can this be made more efficient ?
callbacks.after_insert.append(&mut dom.callbacks.after_insert);
callbacks.after_remove.append(&mut dom.callbacks.after_remove);
Expand Down