ratatui_kit/components/
fragment.rs

1//! Fragment 组件:无额外渲染的透明容器,用于包裹多个子元素,类似 React.Fragment。
2//!
3//! ## 用法
4//! ```rust
5//! element!(Fragment {
6//!     Child1(),
7//!     Child2(),
8//! })
9//! ```
10//! Fragment 不会生成额外的布局节点,常用于返回多个根元素或批量包裹子组件。
11
12use ratatui_kit_macros::Props;
13
14use crate::{AnyElement, Component, ComponentUpdater, Hooks};
15
16#[derive(Default, Props)]
17pub struct FragmentProps<'a> {
18    /// 子元素列表。
19    pub children: Vec<AnyElement<'a>>,
20}
21
22#[derive(Default)]
23/// Fragment 组件实现。
24pub struct Fragment;
25
26impl Component for Fragment {
27    type Props<'a> = FragmentProps<'a>;
28
29    fn new(_props: &Self::Props<'_>) -> Self {
30        Self
31    }
32
33    fn update(
34        &mut self,
35        props: &mut Self::Props<'_>,
36        _hooks: Hooks,
37        updater: &mut ComponentUpdater,
38    ) {
39        updater.set_transparent_layout(true);
40        updater.update_children(props.children.iter_mut(), None);
41    }
42}