ratatui_toolkit/primitives/tree_view/tree_node/constructors/
with_children.rs

1//! TreeNode::with_children constructor.
2
3use crate::primitives::tree_view::tree_node::TreeNode;
4
5impl<T> TreeNode<T> {
6    /// Creates a new tree node with children.
7    ///
8    /// The node is automatically marked as expandable if children are provided.
9    ///
10    /// # Arguments
11    ///
12    /// * `data` - The data to store in the node.
13    /// * `children` - The child nodes.
14    ///
15    /// # Returns
16    ///
17    /// A new `TreeNode` with the given data and children.
18    ///
19    /// # Example
20    ///
21    /// ```rust
22    /// use ratatui_toolkit::tree_view::TreeNode;
23    ///
24    /// let child = TreeNode::new("Child");
25    /// let parent = TreeNode::with_children("Parent", vec![child]);
26    /// assert!(parent.expandable);
27    /// assert_eq!(parent.children.len(), 1);
28    /// ```
29    pub fn with_children(data: T, children: Vec<TreeNode<T>>) -> Self {
30        let expandable = !children.is_empty();
31        Self {
32            data,
33            children,
34            expandable,
35        }
36    }
37}