Skip to content

Commit

Permalink
Merge pull request #34 from aclueless/new-same-type-child-component
Browse files Browse the repository at this point in the history
Fixed render child component with same type
  • Loading branch information
aclueless authored Sep 21, 2023
2 parents 52a31d6 + 201e392 commit 996c626
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/component/child_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ impl<C: Component> ChildComp<C> {
.expect_throw("Expect no borrowing at the first render");

if instance.is_mounted() {
// nothing to do because the component is already in place.
return None;
// otherwise, the component is new, it will be rendered and returned a
// handle to mount to the DOM
}

instance.mount_status = super::ComponentMountStatus::Mounted;
C::init(&comp);
if instance.root_element.is_empty() {
// In cases that the router not cause any render yet, such as Routes = ()
instance.render(&comp);
Expand Down
4 changes: 3 additions & 1 deletion src/dom/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ impl RefComponentNode {

impl Clone for RefComponentNode {
fn clone(&self) -> Self {
panic!("Spair does not support using component_ref inside a list item");
const MSG: &str = "Spair does not support using component_ref inside a list item";
log::error!("{MSG}");
panic!("{MSG}");
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/dom/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,13 @@ impl Nodes {
.expect_throw("dom::nodes::Nodes::ref_component2 get_mut")
{
Node::RefComponent(rcn) => {
if rcn.comp_ref().type_id() != comp_ref.type_id() {
rcn.unmount(parent);
rcn.replace_comp_ref(comp_ref);
rcn.mount(parent);
}
// if rcn.comp_ref().type_id() != comp_ref.type_id() {
// always replace the component. If the component is mounted then ChildComp::component_ref()
// returns None and this will never be reached
rcn.unmount(parent);
rcn.replace_comp_ref(comp_ref);
rcn.mount(parent);
// }
}
_ => panic!("dom::nodes::Nodes::ref_component2 expected Node::RefComponent2"),
}
Expand Down
2 changes: 1 addition & 1 deletion src/render/base/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ mod tests {

fn get_grouped_nodes_active_index(nodes: &[crate::dom::Node]) -> Option<std::any::TypeId> {
let crate::dom::Node::GroupedNodes(mi) = nodes.first().unwrap() else {
panic!("Expect a GroupNodes for match_if");
panic!("Expect a GroupNodes for match_if");
};
mi.active_index()
}
Expand Down
4 changes: 2 additions & 2 deletions src/render/html/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ impl<'a, C: Component> HtmlMatchIfUpdater<'a, C> {
mod tests {
fn get_selected_value(nodes: &[crate::dom::Node]) -> Option<String> {
let crate::dom::Node::Element(select_element) = nodes.first().unwrap() else {
panic!("Expect an Element for <select>");
};
panic!("Expect an Element for <select>");
};
assert_eq!(
crate::dom::ElementType::Select,
select_element.element_type()
Expand Down

0 comments on commit 996c626

Please sign in to comment.