Skip to content
Merged
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
57 changes: 57 additions & 0 deletions rusty/src/views/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ pub trait WidgetData: Send + Sync + Debug + 'static {
fn single_child_mut(&mut self) -> Option<&mut Element> {
None
}

/// Return mutable references to footer elements for recursive tree walking.
/// Card and Dialog override this.
fn footer_mut(&mut self) -> Option<&mut Vec<Element>> {
None
}
}

impl Clone for Box<dyn WidgetData> {
Expand Down Expand Up @@ -103,6 +109,11 @@ impl Element {
if let Some(child) = widget.single_child_mut() {
child.assign_ids(ctx);
}
if let Some(footer) = widget.footer_mut() {
for child in footer {
child.assign_ids(ctx);
}
}
}
Element::Fragment(children) => {
for child in children {
Expand Down Expand Up @@ -572,4 +583,50 @@ mod tests {
"on_click handler should have fired"
);
}

#[test]
fn test_assign_ids_recurses_into_card_footer() {
let mut store = HookStore::default();
let mut ctx = BuildContext::new(&mut store, None);

let mut element: Element = Card::new()
.footer(vec![Button::new("Footer action").into()])
.into();

element.assign_ids(&mut ctx);

if let Element::Widget(card) = &element {
assert_eq!(card.get_id(), Some("w-0"));
if let Some(footer) = card.to_json().get("footer") {
let footer_arr = footer.as_array().unwrap();
let btn_id = footer_arr[0]["id"].as_str().unwrap();
assert_eq!(btn_id, "w-1");
} else {
panic!("Expected Card to have a footer");
}
}
}

#[test]
fn test_assign_ids_recurses_into_dialog_footer() {
let mut store = HookStore::default();
let mut ctx = BuildContext::new(&mut store, None);

let mut element: Element = Dialog::new(true)
.footer(vec![Button::new("OK").into()])
.into();

element.assign_ids(&mut ctx);

if let Element::Widget(dialog) = &element {
assert_eq!(dialog.get_id(), Some("w-0"));
if let Some(footer) = dialog.to_json().get("footer") {
let footer_arr = footer.as_array().unwrap();
let btn_id = footer_arr[0]["id"].as_str().unwrap();
assert_eq!(btn_id, "w-1");
} else {
panic!("Expected Dialog to have a footer");
}
}
}
}
4 changes: 4 additions & 0 deletions rusty/src/widgets/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ impl WidgetData for Card {
fn children_mut(&mut self) -> Option<&mut Vec<Element>> {
Some(&mut self.children)
}

fn footer_mut(&mut self) -> Option<&mut Vec<Element>> {
self.footer.as_mut()
}
}

impl From<Card> for Element {
Expand Down
4 changes: 4 additions & 0 deletions rusty/src/widgets/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl WidgetData for Dialog {
fn children_mut(&mut self) -> Option<&mut Vec<Element>> {
Some(&mut self.children)
}

fn footer_mut(&mut self) -> Option<&mut Vec<Element>> {
self.footer.as_mut()
}
}

impl From<Dialog> for Element {
Expand Down
Loading