Skip to content

Commit

Permalink
Add method to stack/list to add class by idx (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmoulton authored Mar 15, 2024
1 parent 7280fe3 commit da9c744
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/views/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{v_stack_from_iter, Decorators, Stack};
use crate::context::StyleCx;
use crate::reactive::create_effect;
use crate::style::Style;
use crate::style::{Style, StyleClassRef};
use crate::view::View;
use crate::EventPropagation;
use crate::{
Expand Down Expand Up @@ -50,6 +50,10 @@ impl List {
self.onaccept = Some(Box::new(on_accept));
self
}
pub fn add_class_by_idx(mut self, class: impl Fn(usize) -> StyleClassRef) -> Self {
self.child = self.child.add_class_by_idx(class);
self
}
}

/// A list of views built from an iterator which remains static and always contains the same elements in the same order.
Expand Down
12 changes: 11 additions & 1 deletion src/views/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use taffy::style::FlexDirection;
use crate::{
context::UpdateCx,
id::Id,
style::Style,
style::{Style, StyleClassRef},
view::{View, ViewData, Widget},
view_tuple::ViewTuple,
};
Expand Down Expand Up @@ -173,3 +173,13 @@ impl Widget for Stack {
}
}
}

impl Stack {
pub fn add_class_by_idx(self, class: impl Fn(usize) -> StyleClassRef) -> Self {
for (index, child) in self.children.iter().enumerate() {
let style_class = class(index);
child.view_data().id().add_class(style_class);
}
self
}
}

0 comments on commit da9c744

Please sign in to comment.